我猜我忘记了一些明显的东西,但我似乎无法使用MySQL连接到 127.0.0.1
. 通过 localhost
连接工作 .
MySQL / Linux版本: Server version: 10.0.31-MariaDB-0ubuntu0.16.04.2 Ubuntu 16.04
这有效:
$ 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)]>
这不起作用:
$ mysql -h 127.0.0.1 -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
在我看来,MySQL正在收听 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
我甚至可以使用Telnet连接到端口3306:
$ 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
为了以防万一,我还为主机127.0.0.1创建了一个具有grant选项的用户:
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;
以下是有关用户的一些信息(@ brian-ecker建议的查询):
MariaDB [(none)]> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-------------+
| User | Host | plugin |
+------------------+-----------+-------------+
| root | localhost | unix_socket |
| root | 127.0.0.1 | |
+------------------+-----------+-------------+
我还尝试创建一个由unix_socket标识的root用户,而不帮助它:
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 |
+------------------+-----------+-------------+
您对我为什么可以通过“localhost”而不是“127.0.0.1”进行连接有什么建议吗?
2 回答
通过将其添加到MySQL配置解决了它:
来自MySQL documentation:
我在Debian 9上的MariaDB 10.1.26上与此作斗争 . 我发现使用
root
作为用户名根本不起作用,如果你想使用TCP连接,在127.0.0.1
或任何地方 .最后,我创建了另一个具有全局权限的用户 - 没有问题 .