Mysql
 sql >> Database >  >> RDS >> Mysql

Sincronizza 2 tabelle di database diversi - MySQL

Potresti voler usare i comandi 'SELECT ... INTO OUTFILE' e 'LOAD DATA INFILE INTO TABLE'.

Modifica:elaborazione...

Date le strutture delle tabelle:

CREATE TABLE my_local_table (
    id int NOT NULL auto_increment PRIMARY KEY,
    data varchar(20),
    created_on datetime);

CREATE TABLE server_table (
    id int NOT NULL auto_increment PRIMARY KEY,
    data varchar(20),
    created_on datetime,
    local_id int);

E alcuni dati fasulli:

INSERT INTO my_local_table (data, created_on) VALUES ('test', now()), ('test2', now());

Dovresti usare i seguenti comandi:

SELECT id, data, created_on 
    FROM my_local_table
    WHERE created_on >= '2011-08-18'
    INTO OUTFILE '/tmp/t.txt';

-- (and on the server)
LOAD DATA LOCAL INFILE '/tmp/t.txt'
    INTO TABLE server_table
    (local_id, data, created_on);

Per automatizzare i due, puoi usare uno script bash / file batch chiamando mysql connettendoti prima al server locale usando la prima istruzione, poi al server remoto che esegue la seconda.

mysql -e 'SELECT....';
mysql -h remote_server -e 'LOAD DATA...';