150

MySQL 5.1.31 running on Windows XP.

From the local MySQL server (192.168.233.142) I can connect as root as follows:

>mysql --host=192.168.233.142 --user=root --password=redacted

From a remote machine (192.168.233.163), I can see that the mysql port is open:

# telnet 192.168.233.142 3306
Trying 192.168.233.142...
Connected to 192.168.233.142 (192.168.233.142).

But when trying to connect to mysql from the remote machine, I receive:

# mysql --host=192.168.233.142 --user=root --password=redacted
ERROR 1045 (28000): Access denied for user 'root'@'192.168.233.163' (using password: YES)

I have only 2 entries in mysql.user:

Host         User     Password
--------------------------------------
localhost    root     *blahblahblah
%            root     [same as above]

What more do I need to do to enable remote access?

EDIT

As suggested by Paulo below, I tried replacing the mysql.user entry for % with an IP specific entry, so my user table now looks like this:

Host             User     Password
------------------------------------------
localhost        root     *blahblahblah
192.168.233.163  root     [same as above]

I then restarted the machine, but the problem persists.

13 Answers 13

224

You have to put this as root:

GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'IP' IDENTIFIED BY 'PASSWORD' with grant option;

;

where IP is the IP you want to allow access, USERNAME is the user you use to connect, and PASSWORD is the relevant password.

If you want to allow access from any IP just put % instead of your IP

and then you only have to put

FLUSH PRIVILEGES;

Or restart mysql server and that's it.

10
  • Is there any way to do this without a local mysql client?
    – cmcginty
    Aug 28, 2013 at 20:04
  • how to remove privileges of a specific ip?
    – Umair Ayub
    May 14, 2014 at 7:11
  • 5
    stackoverflow.com/a/21151255/470749 helped me too because I guess my bind-address setting needed to be commented out. Granting remote privileges was not enough to get it to work.
    – Ryan
    Jun 16, 2014 at 17:41
  • 9
    Not explained in the answer, but replace PASSWORD with the account password. I tried the above changing only USERNAME and IP and it did not work for me until I changed PASSWORD. Sep 30, 2014 at 13:51
  • 1
    You can also WILDCARD an IP by using %. For example: GRANT ALL PRIVILEGES ON . TO 'USERNAME' @ '10.1.1.%' IDENTIFIED BY 'PASSWORD' with grant option;
    – Xebozone
    Apr 30, 2020 at 2:14
94

I was getting the same error after granting remote access until I made this:

From /etc/mysql/my.cnf

In newer versions of mysql the location of the file is /etc/mysql/mysql.conf.d/mysqld.cnf

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1

(comment this line: bind-address = 127.0.0.1)

Then run service mysql restart.

2
  • 8
    Or, change it to bind-address = 0.0.0.0
    – lolski
    Oct 17, 2014 at 2:16
  • 3
    Only commenting out bind-address didn't work for me. Either bind-address = 0.0.0.0 or bind-address = <server_ip> works.
    – chhantyal
    May 24, 2016 at 16:41
60

By default in MySQL server remote access is disabled. The process to provide a remote access to user is.

  1. Go to my sql bin folder or add it to PATH
  2. Login to root by mysql -uroot -proot (or whatever the root password is.)
  3. On success you will get mysql>
  4. Provide grant access all for that user.

GRANT ALL PRIVILEGES ON *.* TO 'username'@'IP' IDENTIFIED BY 'password';

Here IP is IP address for which you want to allow remote access, if we put % any IP address can access remotely.

Example:

C:\Users\UserName> cd C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin

C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin>mysql -uroot -proot

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.27 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.25 sec)

This for a other user.

mysql> GRANT ALL PRIVILEGES ON *.* TO 'testUser'@'%' IDENTIFIED BY 'testUser';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Hope this will help

2
  • +1 for Excellent details, it really helped me to see that 0 rows affected is the norm :) Also, service mysqld restart is recommended at the end. [mysqld is sometimes just mysql] Jul 12, 2014 at 17:32
  • #Note: For the version 5.6+, use "grant all on..." (without the privileges) Dec 9, 2014 at 13:28
22

Paulo's help lead me to the solution. It was a combination of the following:

  • the password contained a dollar sign
  • I was trying to connect from a Linux shell

The bash shell treats the dollar sign as a special character for expansion to an environment variable, so we need to escape it with a backslash. Incidentally, we don't have to do this in the case where the dollar sign is the final character of the password.

As an example, if your password is "pas$word", from Linux bash we must connect as follows:

# mysql --host=192.168.233.142 --user=root --password=pas\$word
5
  • 4
    In addition to the above please check /etc/mysql/my.cnf and comment bind-address = 127.0.0.1
    – Buminda
    Jan 13, 2014 at 7:23
  • 2
    Alternatively one can use single quotes to prevent expansion of the $: mysql --host=192.168.233.142 --user=root --password='pas$word' stackoverflow.com/a/6697781/500942
    – jalanb
    Jan 7, 2016 at 13:33
  • '$' in the password. Priceless. Thanks a ton !! Feb 23, 2018 at 5:25
  • after beating my head against the monitor for a while I came across this and it worked. I had a ! and @ in my password.Changed password, flushed privileges and I am back at it. Thanks!
    – AJM
    Mar 2, 2020 at 14:35
  • Why are you even putting the password on the command line in the first place? That is insane. You need to investigate using my.cnf files.
    – dland
    Jul 17 at 14:09
9

Do you have a firewall ? make sure that port 3306 is open.

On windows , by default mysql root account is created that is permitted to have access from localhost only unless you have selected the option to enable access from remote machines during installation .

creating or update the desired user with '%' as hostname .

example :

CREATE USER 'krish'@'%' IDENTIFIED BY 'password';
3
  • 6
    The server reply with "ERROR 1045 (28000): Access denied for"... if a firewall block will get a timeout...
    – Paulo H.
    Dec 5, 2011 at 4:49
  • just wanted to post first . my bad Dec 5, 2011 at 5:00
  • @Krish - Everything you suggested is already covered in the question Dec 5, 2011 at 5:09
7
  1. Try to flush privileges again.

  2. Try to restart server to reload grants.

  3. Try create a user with host "192.168.233.163". "%" appears to not allow all (it's weird)

3
  • Thanks, I tried that but to no avail. I updated the question with your suggestions. Dec 5, 2011 at 5:03
  • 4
    Try create another user, and dont forget to set all grants: CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass'; GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%' WITH GRANT OPTION;
    – Paulo H.
    Dec 5, 2011 at 5:10
  • 1
    I had a situation today where 'user'@'%' did indeed not grant 'user' remote access from just 'any' host but instead defining the specific host IP did. I then found out after further testing that this was caused by me binding the sql server to "0.0.0.0". When I commented the binding address completelyin the servers config, suddenly it'd work as expected. Not very intuitive indeed.
    – Marian
    Nov 1 at 17:34
4

In my case I was trying to connect to a remote mysql server on cent OS. After going through a lot of solutions (granting all privileges, removing ip bindings,enabling networking) problem was still not getting solved.

As it turned out, while looking into various solutions,I came across iptables, which made me realize mysql port 3306 was not accepting connections.

Here is a small note on how I checked and resolved this issue.

  • Checking if port is accepting connections:

    telnet (mysql server ip) [portNo]

  • Adding ip table rule to allow connections on the port:

    iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT

  • Would not recommend this for production environment, but if your iptables are not configured properly, adding the rules might not still solve the issue. In that case following should be done:

    service iptables stop

Hope this helps.

3

if you are using dynamic ip just grant access to 192.168.2.% so now you dont have to worry about granting access to your ip address every time.

1

I was struggling with remote login to MYSQL for my Amazon EC2 Linux instance. Found the solution was to make sure my security group included an inbound rule for MySQL port 3306 to include my IP address (or 0.0.0.0/0 for anywhere). Immediately could connect remotely as soon as I added this rule.

1

MySQL ODBC 3.51 Driver is that special characters in the password aren't handled.

"Warning – You might have a serious headache with MySQL ODBC 3.51 if the password in your GRANT command contains special characters, such as ! @ # $ % ^ ?. MySQL ODBC 3.51 ODBC Driver does not support these special characters in the password box. The only error message you would receive is “Access denied” (using password: YES)" - from http://www.plaintutorials.com/install-and-create-mysql-odbc-connector-on-windows-7/

0

The user/host combination may have been created without password.

I was assuming that when adding a new host for an existing user (using a GUI app), the existing password would also be used for the new user/host combination.

I could log in with

mysql -u username -p PASSWORD

locally, but not from IPADDRESS with

mysql -u --host=HOST -p PASSWORD

(I could actually log in from IPADDRESS without using a password)

mysql -u --host=HOST

Setting the password allowed access:

set password for '<USER>'@'<IPADDRESS>' = '<PASSWORD>';
1
  • 1
    Thank you, I spent a lot of time trying to get connected remotely to my Mysql server and this line helped a lot. When setting the user's password, I got the error ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number which led me to this article: knowmysql.blogspot.com/2013/09/… After following those steps, I could finally connect to the server.
    – Sophie
    Jun 18, 2017 at 0:14
0

New location for mysql config file is

/etc/mysql/mysql.conf.d/mysqld.cnf
0

My case is absolutely simple.

You may have this problem in case if you type in WRONG password. No create user is needed (user already existed), no other permissions. Basically make sure that the password is correct. So make double-sure the password is correct

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