简体   繁体   中英

Mysql - connect to remote server using IP address

I am using PHP and MySQL to make a database connection, the only change is that I am trying to access my remote server from my local computer using my server IP address.

mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());

and the error I am getting is Unable to connect to the given host . The user has all privileges and rights.

Most default installs of MySQL only listen to the local machine.

Look for the bind-address setting in my.cnf on the server, and make sure it's listening on the IP address you're targetting. You may also need to ensure skip-networking isn't switched on!

Alternatively (and less securely!) the following will set it up to listen on all addresses - local and remote:

bind-address = 0.0.0.0

Try to add a user (USER-NAME) allowed to connect from the IP:

mysql> GRANT ALL PRIVILEGES ON *.* TO USER-NAME@IP IDENTIFIED BY "PASSWORD";

USER-NAME is the username that you would like to create (like 'widor') IP is the public IP address of your remote connection (like '195.xyz')

Of course, limit the privileges you want to grant (ALL PRIVILEGES is probably not your choice)

1) Check which interface MySQL listen for connections, local (localhost, 127.0.0.1) or remote (0.0.0.0 or IP address). It's in my.cnf. http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

2) Check does your user have corresponding privileges. User that logs in from localhost can have different priveleges from remote one. For example 'user'@'localhost' and 'user'@'%'

Before you try using PHP todo this. Open up your terminal or console and type the following:

$ mysql -u username -h x.xx.xx.x -p

then enter your password

Or on windows then type:

\\path\\to\\mysql.exe -u username -h x.xx.xx.x -p

then enter your password

This will give you a more detailed response as to the authentication issue that's coming up. And you can be 100% sure that your remote login from your IP address works. If it works fine then its something in your PHP code that you're missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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