How to connect to a database in my vps from remote machine? I have installed MySQL on my VPS IP, and issued the following command:

mysql&gt; GRANT ALL PRIVILEGES ON . TO 'root'@'%'IDENTIFIED BY 'passwd' WITH GRANT OPTION;
mysql&gt; FLUSH PRIVILEGES;

service mysql restart


Now I want to log in to MySQL on my VPS IP from a remote machine.

mysql -h vpsip -P 3306 -u root -ppasswd

ERROR 2003 (HY000): Can't connect to MySQL server on 'vpsip' (111)


Why can't log in? How to connect to MySQL from a remote machine?
There could be several reasons why you cannot not connect:


The mysql server is not listening on the public interface. Make sure you are listening to the public ip by commenting the following lines (or setting bind-address to the corresponding public interface)

#skip-networking
#bind-address                   = 127.0.0.1

MySQL is not running in the 3306 port. Connect to the right one or reconfigure the port with:

port = 3306


You can check that mysql is listening in the fight port, right interface with:

$ netstat -tl

MySQL could not successfully start- easy to check, try to connect locally (using ssh) or check that it is running with:

$ pgrep mysqld
$ ps aux | grep mysqld

Your VPS or your provider may have a firewall activated on your server or on the datacenter, or running on a NAT, preventing incoming connections from outside of the server/network. You have to either disable the host network, connect to the public ip corresponding to the right private ip of the server or configuring the port in your control panel.

If for some reason you still cannot access mysql directly (or you do not want to expose the port publicly, which I recommend), you can tunnelize it using ssh, like this:

(localhost)$ ssh -N -L 3306:localhost:3306 user@vpsip
(localhost)$ mysql -h localhost -P 3306 -u root -ppasswd