Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Cross Server Select Query

Is it possible to write a cross server select query using MySQL Client. Basically the setup is like follows.

Server IP       Database
---------       --------
1.2.3.4       Test
a.b.c.d       Test

I want to write a query that will select rows from a table in the Test Database on 1.2.3.4 and insert the result in a table into the Test Database on a.b.c.d
My servers are located miles apart so I will be opening a SSH tunnel to connect the two.

Any pointers?

like image 844
Anand Shah Avatar asked May 01 '09 06:05

Anand Shah


People also ask

Why is select * used in MySQL?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

What is %s and %D in MySQL?

%d – the argument is treated as an integer, and presented as a (signed) decimal number. %s – the argument is treated as and presented as a string. in your examples, $slug is a string and $this->id is an integer.


1 Answers

how about using federated tables on one of the servers? create the federated tables based on the remote tables you will use in the query and just run your query as if your database was all local. example below from MySQL site

The procedure for using FEDERATED tables is very simple. Normally, you have two servers running, either both on the same host or on different hosts. (It is possible for a FEDERATED table to use another table that is managed by the same server, although there is little point in doing so.)

First, you must have a table on the remote server that you want to access by using a FEDERATED table. Suppose that the remote table is in the federated database and is defined like this:

CREATE TABLE test_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=MyISAM  CHARSET=latin1; 

The example uses a MyISAM table, but the table could use any storage engine.

Next, create a FEDERATED table on the local server for accessing the remote table:

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'; 

(Before MySQL 5.0.13, use COMMENT rather than CONNECTION.)

like image 117
Peter Carrero Avatar answered Sep 21 '22 17:09

Peter Carrero