MySQL:能够连接到本地主机但不能连接到 127.0.0.1

标签 mysql mariadb

我猜我忘记了一些明显的东西,但我似乎无法使用 MySQL 连接到 127.0.0.1。通过 localhost 连接有效。

MySQL/Linux 版本:服务器版本: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 创建了一个具有授权选项的用户:

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 标识的根用户,但没有它的帮助:

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 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.

关于MySQL:能够连接到本地主机但不能连接到 127.0.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47242111/

相关文章:

mysql - 安装后找不到common_schema函数get_option

mysql - MariaDB 列存储 : Filtering SubQuery by Computed Value

mysql - MariaDB help_topic.* 文件

java - 将数据写入 MySQL 数据库时 map reduce 作业中的类转换异常

php - Opencart在类别页面显示产品类别

php - 一般错误 : 1364 Field 'manufacturer_name' doesn't have a default value

php - 获取日期并插入字段 - 表单中的隐藏字段

mysql - 创建新过程时,rds 上的 mariadb 会创建新用户@ ip.ec2.host

mysql - 存储长十六进制值

json - 如何将 MySQL 数据读取为 JSON