886

This should be dead simple, but I cannot get it to work for the life of me.
I'm just trying to connect remotely to my MySQL server.

  • Connecting as:

    mysql -u root -h localhost -p  
    
  • works fine, but trying:

    mysql -u root -h 'any ip address here' -p
    
  • fails with the error:

    ERROR 1130 (00000): Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server

In the mysql.user table, there is exactly the same entry for user 'root' with host 'localhost' as another with host '%'.

I'm at my wits' end and have no idea how to proceed. Any ideas are welcome.

1
  • 1
    Can't login as root in most circumstances due to security precaution..
    – AO_
    Commented Oct 22, 2018 at 12:38

22 Answers 22

1087

Possibly a security precaution. You could try adding a new administrator account:

mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
    ->     WITH GRANT OPTION;

Although as Pascal and others have noted it's not a great idea to have a user with this kind of access open to any IP. If you need an administrative user, use root, and leave it on localhost. For any other action specify exactly the privileges you need and limit the accessibility of the user as Pascal has suggest below.

From the MySQL FAQ:

If you cannot figure out why you get Access denied, remove from the user table all entries that have Host values containing wildcards (entries that contain '%' or '_' characters). A very common error is to insert a new entry with Host='%' and User='some_user', thinking that this allows you to specify localhost to connect from the same machine. The reason that this does not work is that the default privileges include an entry with Host='localhost' and User=''. Because that entry has a Host value 'localhost' that is more specific than '%', it is used in preference to the new entry when connecting from localhost! The correct procedure is to insert a second entry with Host='localhost' and User='some_user', or to delete the entry with Host='localhost' and User=''. After deleting the entry, remember to issue a FLUSH PRIVILEGES statement to reload the grant tables. See also Section 5.4.4, “Access Control, Stage 1: Connection Verification”.

10
  • 25
    Good catch Yannick, however I would not recommend him granting all privileges to a non-root user. Perhaps a reduced set? Commented Oct 13, 2009 at 12:50
  • 4
    Well, this indeed wouldn't be a good idea, but allowing 'root' to connect from all hosts is exactly the same, since it is at the same privilege level. Commented Oct 13, 2009 at 12:58
  • 3
    I think you miss my point Pascal. The point is that the 'root' user has those rights already, and he wants to let any ip authenticate as that user. So if this is really what he wants, the default example of creating a new administrator user (which has exactly the same rights) is an alternative to what he's trying. Commented Oct 13, 2009 at 13:11
  • 3
    That's right Yannick, I read to fast and will remove my comment. However, AFAIK, permissions are working fine in MySQL so: 1. maybe the OP modified the grant tables manually and then need to flush privileges. 2. maybe he didn't use the proper grant syntax for root. Adding another administrative user might be a workaround but it won't solve the real issue IMHO. Commented Oct 13, 2009 at 13:26
  • 3
    I felt that providing access from all hosts to root was not a proper solution. Instead I created a new user and granted a reduced set of privileges, the set I used is described as 'DBManager' on MySQL Workbench. I also only allowed access from a certain group of hosts in my local network, particularly 192.168.0.% Commented Jul 26, 2014 at 15:20
385

One has to create a new MySQL User and assign privileges as below in Query prompt via phpMyAdmin or command prompt:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

Once done with all four queries, it should connect with username / password

8
  • 9
    Had to restart mysql after completing the above steps for this to work for me.
    – tollbooth
    Commented Sep 26, 2015 at 18:20
  • Do you have to create username@localhost? Is it not enough if you just create username@% ? I mean, if you just create username@%, will you not be able to connect with that user from localhost? Commented Jan 10, 2017 at 16:58
  • 1
    yeah creating a @localhost would make sense if the privileges were different somehow but here they're the same and it's probably going to be more confusing than anything (such as changing passwords on one not realizing the other exists and that your login might be hitting that one).
    – Bratchley
    Commented May 4, 2017 at 1:06
  • 1
    as a beginner it really helped me Commented Feb 7, 2019 at 6:52
  • 1
    thank it solves my "SQLSTATE[HY000] [1130] Host 'DESKTOP-xxx.xx' is not allowed to connect to this MariaDB server" in containerized laravel application in windows
    – Zulqarnain
    Commented Jun 11, 2019 at 16:11
140

My error message was similar and said 'Host XXX is not allowed to connect to this MySQL server' even though I was using root. Here's how to make sure that root has the correct permissions.

My setup:

  • Ubuntu 14.04 LTS
  • MySQL v5.5.37

Solution

  1. Open up the file under etc/mysql/my.cnf

  2. Check for:

    • port (by default this is port = 3306)
    • bind-address (by default this is bind-address = 127.0.0.1; if you want to open to all then just comment out this line. For my example, I'll say the actual server is on 10.1.1.7)
  3. Now access the MySQL Database on your actual server (say your remote address is 123.123.123.123 at port 3306 as user root and I want to change permissions on database 'dataentry'. Remember to change the IP Address, Port, and database name to your settings)

    mysql -u root -p
    Enter password: <enter password>
    mysql>GRANT ALL ON *.* to root@'123.123.123.123' IDENTIFIED BY 'put-your-password';
    mysql>FLUSH PRIVILEGES;
    mysql>exit
    
  4. sudo service mysqld restart

  5. You should now be able to remote connect to your database. For example, I'm using MySQL Workbench and putting in 'Hostname:10.1.1.7', 'Port:3306', 'Username:root'

6
  • 1
    You can skip the use dataentry line (since most people won't have that database created).
    – Jedidja
    Commented May 4, 2015 at 20:24
  • 3
    i was able to do this without restarting the mysql service at the end
    – ryantuck
    Commented Oct 22, 2015 at 19:23
  • 13
    FLUSH PRIVILEGES should allow you to not need to restart.
    – Adam B
    Commented Apr 9, 2016 at 1:04
  • This worked for me for MySQL 5.5 on Windows 10 with FLUSH PRIVILEGES, without restarting the service . On Windows, the service's properties should identify the my.ini file used for configuration (Properties | General | Path to executable)
    – FreeText
    Commented Feb 11, 2019 at 18:08
  • I assumed that by specifying bind-address="0.0.0.0" it would listen on all interfaces and remote connections would work but I was wrong. Yes, netstat showed that mysql was listening on 0.0.0.0 (all interfaces) but I was getting the error in the question title. Once I removed the bind-address line completely, it started working. Weird.
    – Henno
    Commented Jan 11, 2020 at 13:56
100

Just perform the following steps:

  1. Connect to MySQL (via localhost)

    mysql -uroot -p
    
    • If the MySQL server is running in Kubernetes (K8s) and being accessed via a NodePort

      kubectl exec -it [pod-name] -- /bin/bash
      mysql -uroot -p
      
  1. Create user

    CREATE USER 'user'@'%' IDENTIFIED BY 'password';
    
  2. Grant permissions

    GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
    
  3. Flush privileges

    FLUSH PRIVILEGES;
    
6
  • 5
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwoYES)Y
    – Gank
    Commented Dec 1, 2015 at 5:33
  • 12
    Should \*.\* be *.* ?
    – sgarg
    Commented Apr 7, 2016 at 16:48
  • would this also work for similar problems with docker? devops.stackexchange.com/q/12449/23443 Commented Sep 21, 2020 at 13:29
  • 1
    How to do with mysql docker container? Commented Jun 27, 2021 at 17:43
  • 1
    CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password'; is different from CREATE USER 'user'@'%' IDENTIFIED BY 'password'; the % its make it work like a charm. Commented Aug 8, 2022 at 11:13
85

You need to grant access to the user from any hostname.

This is how you add new privilege from phpmyadmin

Goto Privileges > Add a new User

enter image description here

Select Any Host for the desired username

enter image description here

1
  • This needs to be added to the MAMP for Mac help file I think. Still works in 2021
    – vr_driver
    Commented Dec 7, 2021 at 6:17
33

Simple way:

Grant All Privileges ON *.* to 'USER_NAME'@'%' Identified By 'YOUR_PASSWORD'; 

then

FLUSH PRIVILEGES;

done!

4
  • what does the % signify?
    – dataviews
    Commented Jan 3, 2019 at 22:06
  • 1
    it's like everything. You can thing like example: WHERE CustomerName LIKE 'a%' - Finds any values that start with "a"
    – devugur
    Commented Jan 4, 2019 at 18:42
  • gives SQL syntax error
    – Sushil
    Commented Jun 23, 2022 at 6:42
  • @Sushil please write details
    – devugur
    Commented Aug 29, 2023 at 7:20
30

The message *Host ''xxx.xx.xxx.xxx'' is not allowed to connect to this MySQL server is a reply from the MySQL server to the MySQL client. Notice how its returning the IP address and not the hostname.

If you're trying to connect with mysql -h<hostname> -u<somebody> -p and it returns this message with the IP address, then the MySQL server isn't able to do a reverse lookup on the client. This is critical because thats how it maps the MySQL client to the grants.

Make sure you can do an nslookup <mysqlclient> FROM the MySQL server. If that doesn't work, then there's no entry in the DNS server. Alternatively, you can put an entry in the MySQL server's HOSTS file (<ipaddress> <fullyqualifiedhostname> <hostname> <- The order here might matter).

An entry in my server's host file allowing a reverse lookup of the MySQL client solved this very problem.

1
  • 1
    "is a reply from the MySQL server to the MySQL client." Thank you, I was wondering where the error was coming from, my machine or the server. I have error "ERROR 1130 (HY000):" etc.
    – Jay Brunet
    Commented May 31, 2013 at 22:51
29

This working for any future remote mysql connection !

    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Navigate to the line that begins with the bind-address directive. It should look like this:

    bind-address            = 0.0.0.0

Login to your mysql as root terminal

    mysql -u root -p
    -- root password

    CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

    GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;

    CREATE USER 'username'@'%' IDENTIFIED BY 'password';

    GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;

    FLUSH PRIVILEGES;

    EXIT;

finally Grant that machine exclusive permission to connect to the database remotely with the following command.

    sudo ufw allow from remote_IP_address to any port 3306
1
  • Adding 'username'@'%' fixed my issue as 'username'@'localhost' is not enough, thanks a lot.
    – Mina
    Commented May 14, 2023 at 9:39
18

If you modify the grant tables manually (using INSERT, UPDATE, etc.), you should execute a FLUSH PRIVILEGES statement to tell the server to reload the grant tables.

PS: I wouldn't recommend to allow any host to connect for any user (especially not the root use). If you are using mysql for a client/server application, prefer a subnet address. If you are using mysql with a web server or application server, use specific IPs.

0
8

If you are using MySQL WorkBench, you can achieve this easily:

  1. From the menu, select Server -> Users And Privileges enter image description here

  2. On the lower left, click on "Add account" enter image description here

  3. Fill the form with username, host matching (% means every host) and the password enter image description here

  4. Click on "Apply" on the lower right enter image description here

After this you are good to go. Then, if you want to refine your configuration, you can use the "Administrative Roles" tab to set the command that can be used by the user (SELECT, ALTER etc etc) and the "Schema privileges" tab to restrict the user interaction to specific schemas.

7

Just use the interface provided by MySql's GUI Tool (SQLyog):

Click on User manager: enter image description here

Now, if you want to grant access FOR ANY OTHER REMOTE PC, just make sure that, just like in the underneath picture, the Host field value is % (which is the wildcard)

enter image description here

0
5

Most of the answers here show you creating users with two host values: one for localhost, and one for %.

Please note that except for a built-in localhost user like root, you don't need to do this. If you simply want to make a new user that can log in from anywhere, you can use

CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
GRANT <whatever privileges are appropriate> ON <relevant tables> TO myuser;

and it will work just fine. (As others have mentioned, it's a terrible idea to grant administrative privileges to a user from any domain.)

4

Well, nothing of the above answer worked for me. After a lot of research, I found a solution. Though I may be late this may help others in future.

Login to your SQL server from a terminal

 mysql -u root -p
 -- root password
GRANT ALL ON *.* to root@'XX.XXX.XXX.XX' IDENTIFIED BY 'password';

This should solve the permission issue.

Before solving error

After solving issue

Happy coding!!

3

simple way is to login to phpmyadmin with root account , there goto mysql database and select user table , there edit root account and in host field add % wild card . and then through ssh flush privileges

 FLUSH PRIVILEGES;
1
  • This helped me combined with this: GRANT ALL PRIVILEGES ON . TO 'root'@'%' WITH GRANT OPTION;
    – Lanklaas
    Commented Jul 4, 2017 at 17:41
3

You need to allow users from other locations(x.x.x.x in your case) as well from where you are going to connect to the database.

All the above answers do seem correct in the wildcard(%) declaration for allowing hosts from all locations but that opens it to all hosts and hence opening a security risk. Its better to explicitly specify the host as follows:

CREATE USER 'username'@'x.x.x.x' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'x.x.x.x' WITH GRANT OPTION;

FLUSH PRIVILEGES;

Replace x.x.x.x with the host ip that you are connecting from.

2

If this is a recent mysql install, then before changing anything else, try simply to execute this command and then try again:

flush privileges;

This alone fixes the issue for me on Ubuntu 16.04, mysql 5.7.20. YMMV.

2

Just find a better way to do that from your hosting control panel (I'm using DirectAdmin here)

simply go to the target server DB in your control panel, in my case: MySQL management -> select your DB -> you will find: "Access Hosts", simply add your remote host here and its working now! enter image description here

I guess there is a similar option on other C.panels like plesk, etc..

I'm hope it was helpful to you too.

2

I was also facing same issue, It resolved in 2 min for me i just white list ip through cpanel

Suppose you are trying to connect database of server B from server A. Go to Server B Cpanel->Remote MySQL-> enter Server A IP Address and That's it.

1

If you happen to be running on Windows; A simple solution is to run the MySQL server instance configuration wizard. It is in your MYSQL group in the start menu. On the second from last screen click the box that says "allow root access from remote machines".

0
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

this error because no password to the root , and this Maybe occurred with you when you trying to connect from outside .

0
0

If you have WAMP Server + Windows 10 and you are using it for development than Right Click on Wamp Icon => Wamp Settings => Check Allow Virtual Hosts other than 127* enter image description here

0

1. From a terminal, connect you to your MySQL running container

docker exec -it your_container_name_or_id bash

2. In your container, connect you to the MySQL database

mysql -u your_user -p

enter your password to connect to database.

3. execute this SQL script to list all existing database users:

SELECT host, user FROM mysql.user;

The result will be some thing like below:

host user
127.0.0.1 root
::1 root
localhost mysql.sys
localhost root

you should add a new row:

host user
% root
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;

Not the answer you're looking for? Browse other questions tagged or ask your own question.