简体   繁体   English

无法连接到远程 MySQL 服务器

[英]Unable to connect to remote MySQL Server

I have a server attempting to connect to a MySQL database hosted outside of the local network.我有一台服务器试图连接到本地网络之外托管的 MySQL 数据库。 I am attempting to use a public IP address to connect to it.我正在尝试使用公共 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)

PDO gives me the same error. PDO 给了我同样的错误。 In any case, the connection works fine locally and within the same network, which is what boggles my mind.无论如何,连接在本地和同一网络内都可以正常工作,这让我感到困惑。

The MySQL server has had its bind-address modified so it accepts remote connections. MySQL 服务器已修改其绑定地址,因此它接受远程连接。 The MySQL server also has a user with the proper privileges set. MySQL 服务器还有一个具有适当权限集的用户。 But in any case, it looks as though I can't even start the connection in the first place.但无论如何,看起来我什至无法首先启动连接。

Is there a my.cnf value I need to add to let MySQL accept requests from outside the local network?是否需要添加 my.cnf 值才能让 MySQL 接受来自本地网络外部的请求?

Thanks.谢谢。

Things to check:检查事项:

  1. MySQL is listening on public IP (sounds like you've done) MySQL 正在监听公共 IP(听起来你已经完成了)
  2. MySQL is listening on standard port / you're connecting the same port it's listening to. MySQL 正在侦听标准端口/您正在连接它正在侦听的同一端口。
  3. 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? (它们通常在发行版中打包为标准)防火墙是否配置为允许连接到该端口?
  4. 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.如果远程机器在另一个网络中,您的连接和终端机器之间是否正在进行网络地址转换 (NAT) - 如果是,它是否配置为允许 MySQL 端口通过。
  5. Is the my.cnf file configured to allow connections from anything other than localhost 127.0.0.1 IPs - although you'd more likely get a access denied response, than a cannot connect. my.cnf文件是否配置为允许来自除localhost 127.0.0.1 IP 以外的任何内容的连接 - 尽管您更有可能得到访问被拒绝的响应,而不是无法连接。

grant privileges to the user from that particular host.从该特定主机向用户授予权限。 or to access it from any host use % (ie wild card) as host.或从任何主机使用 %(即通配符)作为主机访问它。

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?如果您尝试 telnet 到服务器上的 3306 端口会发生什么? You should get a prompt from MySQL.你应该从 MySQL 得到一个提示。

Grant the privileges for all users using the command使用命令为所有用户授予权限

GRANT ALL PRIVILEGES ON .授予所有特权 TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION; TO 'root'@'localhost' 由 'root' 标识,并带有 GRANT 选项; GRANT ALL PRIVILEGES ON .授予所有特权 TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; TO 'root'@'%' 由 'root' 标识,带有 GRANT 选项; FLUSH PRIVILEGES;同花顺特权;

Now go to your "my.cnf" file.现在转到您的“my.cnf”文件。 For ubuntu use (sudo find / -type f -name "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”

I had the same issue and most of the solutions given are:我遇到了同样的问题,给出的大多数解决方案是:

  1. Change config file to allow port/address更改配置文件以允许端口/地址
  2. Allow program/port through firewall允许程序/端口通过防火墙
  3. Check the IP/URL for the remote connection检查远程连接的 IP/URL

But in my case, the database was hosted on a remote server and the router required port forwarding.但就我而言,数据库托管在远程服务器上,路由器需要端口转发。

  1. Go to your router settings (Open browser and type in 192.168.0.1 or similar)转到您的路由器设置(打开浏览器并输入 192.168.0.1 或类似内容)
  2. Enter credentials (mostly admin/admin or admin/password)输入凭据(主要是管理员/管理员或管理员/密码)
  3. Find "forwarding" or "port forwarding" in menu在菜单中找到“转发”或“端口转发”
  4. Create new entry (In my case I used postgres so 5432 port with TCP)创建新条目(在我的情况下,我使用了 postgres 所以 5432 端口与 TCP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM