Can't connect to mysql
on localhost
over TCP on a freshly installed server version: 5.7.18-0ubuntu0.17.04.1 (Ubuntu)
This works:
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.18-0ubuntu0.17.04.1 (Ubuntu)
This doesn't:
# mysql -h 127.0.0.1
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
From within client I see that mysql
is configured to listen on 127.0.0.1:
mysql> show variables like '%bind%';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| bind_address | 127.0.0.1 |
+---------------+-----------+
1 row in set (0.00 sec)
And it is indeed listening:
# netstat -tl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:mysql 0.0.0.0:* LISTEN
And because of that, PHP also fails with:
Warning: mysqli_connect(): (HY000/1698): Access denied for user 'root'@'localhost'
How to fix that? Maybe there is a setting the disables password based login over TCP?
UPDATE: It appeared that localhost
means local socket, so I've created another record for 'root'@'127.0.0.1' for TCP connections.
CREATE USER 'root'@'127.0.0.1';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
FLUSH PRIVILEGES;
But it still doesn't work:
# mysql --protocol=TCP --host=127.0.0.1
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Password is not set.
mysql> SELECT host, user, authentication_string FROM mysql.user;
+-----------+------------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+------------------+-------------------------------------------+
| localhost | root | |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | debian-sys-maint | *4CF7B81390C295E6AF38067F3B621BBF736D3366 |
| 127.0.0.1 | root | |
+-----------+------------------+-------------------------------------------+
4 rows in set (0.01 sec)
UPDATE 2: I started from scratch, so this is the test that fails:
mysql -h 127.0.0.1
But after recreating user, it starts to work:
DROP USER 'root'@'localhost';
CREATE USER 'root'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Why it doesn't work with the same user out of the box?