我猜我忘记了一些明显的东西,但我似乎无法连接127.0.0.1
使用MySQL.通过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 ''; 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"进行连接有什么建议吗?
通过将其添加到MySQL配置解决了它:
skip-name-resolve = 1
来自MySQL文档:
如果为OFF,则mysqld在检查客户端连接时解析主机名.如果它是ON,mysqld只使用IP号码; 在这种情况下,授权表中的所有主机列值必须是IP地址或localhost.