Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Mar 9 '13 at 18:01

This question came from our site for professional and enthusiast programmers.

3  
Please don't use mysql_* functions in new code. They're deprecated. Look here for alternatives, and that might solve your problem.php.net/manual/en/mysqlinfo.api.choosing.php –  Alfo Mar 9 '13 at 17:01

1 Answer 1

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:

 bind-address=ipAddressOfYourDatabaseServer

than restart mysql server.

 sudo service mysql restart

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.

 iptables -A INPUT -s ! yourOtherVPSipAddress -p tcp -dport 222 -j DROP

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.