开发者

Unable to connect to remote MySQL Server

开发者 https://www.devze.com 2023-01-15 09:52 出处:网络
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.

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.

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. In any case, the connection works fine locally and within the same network, which is what boggles my mind.

T开发者_StackOverflow中文版he 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.

Is there a my.cnf value I need to add to let MySQL accept requests from outside the local network?

Thanks.


Things to check:

  1. MySQL is listening on public IP (sounds like you've done)
  2. MySQL is listening on standard port / you're connecting the same port it's listening to.
  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.
  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.


grant privileges to the user from that particular host. or to access it from any host use % (i.e. 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

What happens if you try to telnet to port 3306 on the server? You should get a prompt from MySQL.


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;

Now go to your "my.cnf" file. For ubuntu use (sudo find / -type f -name "my.cnf")

comment out the line "bind-address = 127.0.0.1"

restart mysql using For ubuntu use "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

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)
  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)
0

精彩评论

暂无评论...
验证码 换一张
取 消