michltm michltm - 2 months ago 14
MySQL Question

Python MySQLdb connection to online database

While I have no problems to connect to my localhost database that way:

import MySQLdb

localdb = MySQLdb.connect( host="127.0.0.1",
user="root",
passwd="password",
db="events")


I couldent connect to my online database.

Usually I access manually to this database with phpmyadmin and the adress is something like 212.227.000.000/phpmyadmin

So I tried something like

onlinedb = MySQLdb.connect( host="212.227.000.000" ...


or

onlinedb = MySQLdb.connect( host="212.227.000.000/phpmyadmin" ...


But I get an error such as:

OperationalError: (2003, "Can't connect to MySQL server on '212.227.000.000' (10061)")

Answer

It sounds like 212.227.000.000/phpmyadmin is the URL of PHPMyAdmin (the thing you open in the browser). If so, the database may not be hosted on the machine with IP 212.227.000.000. You should check how PHPMyAdmin connects to the database. If PHPMyAdmin connects to 127.0.0.1, that probably means the database doesn't listen on the external IP address, and can't be reached over the network.

If you have ssh access to 212.227.000.000 you can check that with the netstat command:

$ netstat -pant | grep LISTEN | grep 3306
tcp    0    0 0.0.0.0:3306        0.0.0.0:*    LISTEN      -

The 0.0.0.0 above indicates that MySQL is listening on all IPs, and barring any firewalls, you should be able to connect to the database.

Otherwise, if it says 127.0.0.1:3306, the database can only be accessed from the machine itself and not over the network. In that case you can use an SSH tunnel.

Comments