简体   繁体   中英

MySQL: Able to connect to localhost but not to 127.0.0.1

I am guessing I am forgetting something obvious, but I can't seem to connect to 127.0.0.1 using MySQL. Connecting via localhost works.

MySQL / Linux version: Server version: 10.0.31-MariaDB-0ubuntu0.16.04.2 Ubuntu 16.04

This works:

$ mysql -h localhost -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 71982
Server version: 10.0.31-MariaDB-0ubuntu0.16.04.2 Ubuntu 16.04

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

This does not work:

$ mysql -h 127.0.0.1 -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

It seems to me that MySQL is listening to 127.0.0.1:3306 :

$ netstat -plnt
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1841/mysqld 

I can even connect to port 3306 using Telnet:

$ telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
j
5.5.5-10.0.31-MariaDB-0ubuntu0.16.04.2TGYk-=,d5-??wz}'Hr*s+u24mysql_native_password

Just in case, I have also created a user with grant option for host 127.0.0.1:

mysql > CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '<redacted>';
mysql > GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;

Here is some information about users (query suggested by @brian-ecker):

MariaDB [(none)]> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-------------+
| User             | Host      | plugin      |
+------------------+-----------+-------------+
| root             | localhost | unix_socket |
| root             | 127.0.0.1 |             |
+------------------+-----------+-------------+

I have also tried creating a root user that is identified by unix_socket without it helping:

MariaDB [(none)]> CREATE USER 'root'@'127.0.0.1' IDENTIFIED VIA unix_socket;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
MariaDB [(none)]> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-------------+
| User             | Host      | plugin      |
+------------------+-----------+-------------+
| root             | localhost | unix_socket |
| root             | 127.0.0.1 | unix_socket |
+------------------+-----------+-------------+

Do you have any suggestions as to why I can connect via "localhost" but not "127.0.0.1"?

Solved it by adding this to MySQL configuration:

skip-name-resolve       = 1

From MySQL documentation :

If it is OFF, mysqld resolves host names when checking client connections. If it is ON, mysqld uses only IP numbers; in this case, all Host column values in the grant tables must be IP addresses or localhost.

I've battled with this on MariaDB 10.1.26 on Debian 9. I found that using root as username simply doesn't work if you want to connect using TCP, on 127.0.0.1 , or wherever.

In the end, I created another user with global privileges – no issues.

Maybe you wanted to use unix sockets in the case of localhost. However, connecting to 127.0.0.1 works again if you just don't use the "unix_socket" plugin. Eg do the following:

UPDATE user SET plugin = '' 
WHERE user = 'root' AND host = 'localhost';
FLUSH PRIVILEGES;

A drawback of this solution is that you can no longer use MySQL root login from the system's root account; ie accessing MySQL root without entering a password; eg mysql -u root -h localhost will no longer work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM