Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Able to connect to localhost but not to 127.0.0.1

Tags:

mysql

mariadb

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"?

like image 258
siment Avatar asked Nov 11 '17 19:11

siment


People also ask

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 connect to a MySQL IP address?

Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed. Note: The IP address of the instance and the mysql client IP address you authorize must be the same IP version: either IPv4 or IPv6. Click Done.


2 Answers

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.

like image 194
siment Avatar answered Sep 28 '22 09:09

siment


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.

like image 28
Simon Fredsted Avatar answered Sep 28 '22 07:09

Simon Fredsted