Connect to two databases

If you don't specify the database in your connect call, you can write queries against multiple databases at once. The documentation says that db is not required.

db = _mysql.connect('localhost', 'user', 'passwd')

then

SELECT u.*, i.* FROM db1.users u LEFT JOIN db2.items i ON u.id = i.user_id

But it'll only work if the two databases are on the same server.


Just make two separate connections

con1 = mdb.connect (host=MY_HOST, user=MY_USER, passwd=MY_PASS, db1=MY_DB1)
con2 = mdb.connect (host=MY_HOST2, user=MY_USER2, passwd=MY_PASS2, db2=MY_DB2)

and use them independently just as you would when using one database.

Tags:

Python

Mysql