mysql:尝试使用远程 IP 连接到本地主机时连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13032270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:15:54  来源:igfitidea点击:

mysql: connection refused when trying to connect to localhost using remote IP

mysql

提问by Tiddo

When I try to connect to a local mysql database using it's remote ip-address I get a access denied. When I try to connect to that same database from an external machine, it works without any problems. When I connect to the local database using localhost, it works perfectly as well. E.g., if the database server has the ip 1.2.3.4 then I get the following results:

当我尝试使用它的远程 ip 地址连接到本地 mysql 数据库时,我的访问被拒绝。当我尝试从外部机器连接到同一个数据库时,它可以正常工作。当我使用 localhost 连接到本地数据库时,它也能正常工作。例如,如果数据库服务器的 ip 为 1.2.3.4,那么我会得到以下结果:

# From the db server
mysql -u username -h localhost -p #works perfectly
mysql -u username -h 127.0.0.1 -p #works perfectly
mysql -u username -h 1.2.3.4 -p #Access denied

# From any other machine
mysql -u username -h 1.2.3.4 -p #works perfectly

What can I do to allow local access to my database using its remote ip-address? The OS of the database server is Fedora 15 and the MySQL version is 5.5.23.

我该怎么做才能允许使用其远程 IP 地址本地访问我的数据库?数据库服务器操作系统为Fedora 15,MySQL版本为5.5.23。

采纳答案by Tiddo

I found the solution to my problem myself, but I still don't quite understand why it didn't work:

我自己找到了解决问题的方法,但我仍然不太明白为什么它不起作用:

I granted privileges to that user on the hosts % and localhost:

我在主机 % 和 localhost 上授予该用户权限:

# Before
+-----------------+------------+
| Host            | User       |
+-----------------+------------+
| %               | username   |
| localhost       | username   |
+-----------------+------------+

With these settings I got the results I showed above. When I granted privileges to that user on host it suddenly did work.

通过这些设置,我得到了上面显示的结果。当我在主机上向该用户授予权限时,它突然起作用了。

# After
+-----------------+------------+
| Host            | User       |
+-----------------+------------+
| %               | username   |
| localhost       | username   |
| <myIpAddress>   | username   |
+-----------------+------------+

Apparently % does work for remote connections, but not for local connections.

显然 % 确实适用于远程连接,但不适用于本地连接。

回答by Eduardo Cuomo

Try to edit/add bind-address = 0.0.0.0to your [mysqld]section of your /etc/mysql/mysql.conf.d/mysqld.cnffile and restart MySQL Service.

尝试编辑/添加bind-address = 0.0.0.0到您[mysqld]/etc/mysql/mysql.conf.d/mysqld.cnf文件部分并重新启动MySQL Service

回答by Victor F.

This particular issue can be caused by host name resolution.

此特定问题可能是由主机名解析引起的。

I have resolved it in my particular case by deleting this variable from my my.cnf config file:

我通过从 my.cnf 配置文件中删除这个变量来解决我的特殊情况:

skip-name-resolve

跳过名称解析

Either remark the variable by placing a pound sign # or just delete it from your my.cnf after making sure you back it up, of course.

当然,可以通过放置井号 # 来注释变量,或者在确保备份后从 my.cnf 中删除它。