MySQL — Joins Between Databases On Different Servers Using Python?

Question:

In MySQL, I have two different databases — let’s call them A and B.

Database A resides on server server1, while database B resides on server server2.

Both servers {A, B} are physically close to each other, but are on different machines and have different connection parameters (different username, different password etc).

In such a case, is it possible to perform a join between a table that is in database A, to a table that is in database B?

If so, how do I go about it, programatically, in python? (I am using python‘s MySQLDB to separately interact with each one of the databases).

Asked By: user3262424

||

Answers:

It is very simple – select data from one server, select data from another server and aggregate using Python. If you would like to have SQL query with JOIN – put result from both servers into separate tables in local SQLite database and write SELECT with JOIN.

Answered By: Anatolij

No. It is not possible to do the join as you would like. But you may be able to sort something out by replicating one of the servers to the other for the individual database.

One data set is under the control of one copy of MySQL and the other dataset is under the control of the other copy of MySQL. The query can only be processed by one of the (MySQL) servers.

If you create a copy of the second database on the first server or vice versa (the one that gets the fewest updates is best) you can set up replication to keep the copy up to date. You will then be able to run the query as you want.

Answered By: Jaydee

Try to use FEDERATED Storage Engine.

Workaround: it is possible to use another DBMS to retrieve data between two databases, for example you could do it using linked servers in MS SQL Server (see sp_addlinkedserver stored procedure). From the documentation:

A linked server allows for access to distributed, heterogeneous queries against OLE DB data sources.

Answered By: Devart
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.