Join tables from two different server

Yes, it is possible in MySQL.

There are similar questions asked previously too. You have to use FEDERATED ENGINE to do this. The idea goes like this:

You have to have a federated table based on the table at another remote location to use the way you want. The structure of the table have to exactly same.

CREATE TABLE federated_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

[Source Answer]


Replication will be alternate and suitable solution.

server1 - db1 -> replicate to server2. (now db1 and db2 will be in same server server2. join will be easy).

NOTE: If the server2 is enough capable of take the load of db1 in terms of store/process etc., then wen can do the replication. As @brilliand mentioned yes Federated will make the much manual work and slow in process.


It's kind of a hack, and it's not a join, but I use bash functions to make it feel like I'm doing cross-server queries:

The explicit version:

tb2lst(){
    echo -n "("
    tail -n +2 - | paste -sd, | tr -d "\n"
    echo ")"
}

id_list=$(mysql -h'db_a.hostname' -ume -p'ass' -e "SELECT id FROM foo;" | tb2lst)
mysql -h'db_b.hostname' -ume -p'ass' -e "SELECT * FROM bar WHERE foo_id IN $id_list"

+--------|-----+
| foo_id | val |
+--------|-----+
| 1      | 3   |
| 2      | 4   |
+--------|-----+

I wrote some wrapper functions which I keep in my bashrc, so my perspective it's just one command:

db_b "SELECT * FROM bar WHERE foo_id IN $(db_a "SELECT id FROM foo;" | tb2lst);"

+--------|-----+
| foo_id | val |
+--------|-----+
| 1      | 3   |
| 2      | 4   |
+--------|-----+

At least for my use case, this stitches the two queries together quickly enough that the output is equivalent to the join, and then I can pipe the output into whatever tool needs it.

Keep in mind that the id list from one query ends up as query text in the other query. If you "join" too much data this way, your OS might limit the length of query (https://serverfault.com/a/163390). So be aware that this is a poor solution for very large datasets. I have found that doing the same thing with a mysql library like pymysql works around this limitation.