无法连接到远程 MySQL 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3686233/
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
Unable to connect to remote MySQL Server
提问by Julian H. Lam
I have a server attempting to connect to a MySQL database hosted outside of the local network. I am attempting to use a public IP address to connect to it.
我有一台服务器试图连接到本地网络之外托管的 MySQL 数据库。我正在尝试使用公共 IP 地址连接到它。
Testing the connection in the command line gives me this error:
在命令行中测试连接给了我这个错误:
ERROR 2003 (HY000): Can't connect to MySQL server on '[ip_address]' (146)
ERROR 2003 (HY000): Can't connect to MySQL server on '[ip_address]' (146)
PDO gives me the same error. In any case, the connection works fine locally and within the same network, which is what boggles my mind.
PDO 给了我同样的错误。无论如何,连接在本地和同一网络内都可以正常工作,这让我感到困惑。
The MySQL server has had its bind-address modified so it accepts remote connections. The MySQL server also has a user with the proper privileges set. But in any case, it looks as though I can't even start the connection in the first place.
MySQL 服务器已修改其绑定地址,因此它接受远程连接。MySQL 服务器还有一个具有适当权限集的用户。但无论如何,看起来我什至无法首先启动连接。
Is there a my.cnf value I need to add to let MySQL accept requests from outside the local network?
是否需要添加 my.cnf 值才能让 MySQL 接受来自本地网络外部的请求?
Thanks.
谢谢。
回答by Rudu
Things to check:
检查事项:
- MySQL is listening on public IP (sounds like you've done)
- MySQL is listening on standard port / you're connecting the same port it's listening to.
- Is there a firewall running on the remote machine? (They usually are packaged standard in distros) Is the firewall configured to allow connections to that port?
- If the remote machine is within another network, is there network address translation (NAT) going on between your connection and the end machine - if so, is it configured to allow the MySQL port through.
- Is the
my.cnf
file configured to allow connections from anything other thanlocalhost
127.0.0.1
IPs - although you'd more likely get a access denied response, than a cannot connect.
- MySQL 正在监听公共 IP(听起来你已经完成了)
- MySQL 正在侦听标准端口/您正在连接它正在侦听的同一端口。
- 远程机器上是否有防火墙运行?(它们通常在发行版中打包为标准)防火墙是否配置为允许连接到该端口?
- 如果远程机器在另一个网络中,您的连接和终端机器之间是否正在进行网络地址转换 (NAT) - 如果是,它是否配置为允许 MySQL 端口通过。
my.cnf
文件是否配置为允许来自localhost
127.0.0.1
IP以外的任何连接- 尽管您更有可能收到拒绝访问的响应,而不是无法连接。
回答by Sandesh
grant privileges to the user from that particular host. or to access it from any host use % (i.e. wild card) as host.
从该特定主机向用户授予权限。或从任何主机使用 %(即通配符)作为主机访问它。
回答by mr. w
It definitely sounds like the client isn't able to connect. This page has some suggestions on how to narrow down the problem:
听起来肯定是客户端无法连接。这个页面有一些关于如何缩小问题范围的建议:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
What happens if you try to telnet to port 3306 on the server? You should get a prompt from MySQL.
如果您尝试 telnet 到服务器上的 3306 端口会发生什么?你应该从 MySQL 得到一个提示。
回答by ravi ranjan
Grant the privileges for all users using the command
使用命令为所有用户授予权限
GRANT ALL PRIVILEGES ON .TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON .TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES;
授予所有特权。TO 'root'@'localhost' 由 'root' 标识,并带有 GRANT 选项;授予所有特权。TO 'root'@'%' 由 'root' 标识,带有 GRANT 选项;同花顺特权;
Now go to your "my.cnf" file. For ubuntu use (sudo find / -type f -name "my.cnf")
现在转到您的“my.cnf”文件。对于 ubuntu 使用(sudo find / -type f -name "my.cnf")
comment out the line "bind-address = 127.0.0.1"
注释掉“bind-address = 127.0.0.1”这一行
restart mysql using For ubuntu use "sudo service mysql restart"
使用 ubuntu 重新启动 mysql 使用“sudo service mysql restart”