Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL permission denied from local but can connect remotely

I am trying to connect to my mysql database on a remote server (via ssh) through the command:

mysql -u me -h mydomain.com -p

But it fails with a ERROR 1045 (28000): Access denied for user.. error

While

mysql -u me -h localhost -p

Works

Now this isn't just because I have not setup permissions, because the permissions to this database are set for % or any host for the me user.

This is proved by the fact that I can connect correctly from my local machine to the server, using the same user. i.e. running the following command from my local machine works:

mysql -u me -h mydomain.com -p

So my question why does this happen and how can I fix it? Why can I not connect to my mysql server from my server when I use the domain name instead of localhost, even though the permissions are setup to accept connections from any host.

like image 374
zenna Avatar asked Jul 15 '10 09:07

zenna


People also ask

How do I fix localhost Access Denied?

Use the ALTER USER command and change the authentication method to log into MySQL as root: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password'; This command changes the password for the user root and sets the authentication method to mysql_native_password.

Why can I not connect to localhost MySQL?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

How do I fix MySQL access denied error?

You will get this error when the user user_name does not have the right to access your MySQL database. To resolve the error, you must create a user with the following command: mysql> GRANT ALL ON *. * to user_name@localhost IDENTIFIED BY 'password';


1 Answers

This happens because of the way MySQL handles permission grants.

When you connect from a remote host (or from the local host via an external IP), it will match the me@% entry (if there is no specific grant for the particular host you're using!). But when you connect via the loopback interface (the "localhost" IP) or a socket, it will use the me@localhost grant. So you must have two GRANT PRIVILEGES; one for me@localhost and one for me@%.

like image 174
Borealid Avatar answered Oct 16 '22 23:10

Borealid