I have 2 VPS.
I would like to connect from the frist to the second's mysql db like this :
$db = mysql_connect("94.xxx.xxx.xxx:222", "user", "pass");
I got this error:
Warning: mysql_connect(): Lost connection to MySQL server at 'reading
initial communication packet', system error: 111 in
/var/www/pcsart/test.php on line 4
My server is works on first server is works on port 222.
In the first server I don't have a mysql.
On the second is running on port 3306
First and second server's firewall got these rules :
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
ACCEPT tcp -- anywhere anywhere tcp dpt:222
ACCEPT tcp -- anywhere anywhere tcp dpt:222
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
ACCEPT tcp -- anywhere anywhere tcp dpt:222
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
ACCEPT tcp -- anywhere anywhere tcp dpt:222
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
What i'm doing wrong?
Best Answer
By default mysql server accept connections only from localhost, to change this behaviour you should change in mysql config the bind-address variable in /etc/mysql/my.cnf:
than restart mysql server.
The explain: http://www.howtogeek.com/howto/mysql/switch-mysql-to-listen-on-tcp/
After this i recommend you, that change your iptables, that your 222 port should be reachable only from your other VPS.
The other thing: the mysql_ funcrions are deprecated, as the other commenter wrote. If you do not want to make a lot of work with your existing code, you should use mysqli. See the php manual, only a minimal work with sintax if you use mysqli as function library.