How to synchronize Mysql Data
Assume that there are host a and host B (Linux system). The IP address of host a is 1.2.3.4 (of course, it can also be dynamic), and the IP address of host B is 5.6.7.8. Both hosts are installed with PHP + MySQL. Now we are operating on the data on host a. If another host B wants to synchronize data with host a, what should we do?
OK. Now let's get started.
First, if you want to synchronize data between two hosts, one method is to send data from host a to host B, and the other method is to send data from host B to host, because the IP address of a is dynamic (assuming), we have to send data from host a to host B.
Create a MySQL account on host B.
# Grant all on test. * to user @ % identified by "password"; // create a user who can access the test database on host B from any machine.
If an error is displayed, change % to an IP address first, and then use phpMyAdmin to change the IP address to %. After testing, you can write PHPProgram.
<?
$ Link = mysql_pconnect ("localhost", "user", "password ");
Mysql_pconnect ("localhost", "user", "password"); // connect to the database of the local host (host)
Mysql_select_db ("test"); // select the database Test
$ Re = mysql_query ("select * from Table order by id desc ");
$ Num = mysql_numrows ($ re );
If (! Empty ($ num )){
$ Id = mysql_result ($ re, 0, "ID"); // obtain the maximum ID of the local table
}
Mysql_close ($ link); // close the connection to the local database
$ Link = mysql_pconnect ("5.6.7.8", "test", "test ");
Mysql_pconnect ("5.6.7.8", "test", "test"); // connect to the database of host B
Mysql_select_db ("test"); // select database test, which should have the same structure as the test database on host.
$ Re = mysql_query ("select * from Table order by id desc ");
$ Num = mysql_numrows ($ re );
If (! Empty ($ num )){
$ Remote_id = mysql_result ($ re, 0, "ID"); // obtain the maximum ID of the host btable
}
If ($ id> $ remote_id ){
$ Result_id = $ id-$ remote_id; // if the maximum table ID in host A is greater than the maximum table ID in host B, two
} Different materials
Mysql_close ($ link); // close the database connection of host B
$ Link = mysql_pconnect ("localhost", "user", "password ");
Mysql_pconnect ("localhost", "user", "password ");
Mysql_select_db ("test ");
If (empty ($ result_id) $ result_id = 0;
If (empty ($ remote_id) $ remote_id = 0; // if the maximum table ID in host B is null (no data in it), then wait for 0
$ Re = mysql_query ("select * from Table limit $ remote_id, $ result_id"); // retrieve the table in host a and table in host B.
$ Num = mysql_numrows ($ re );
If (! Empty ($ num )){
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Test [$ I] = mysql_result ($ re, $ I, "test"); // put different materials into an array
}
}
Mysql_close ($ link); // close the database connection of host
$ Link = mysql_pconnect ("5.6.7.8", "user", "password ");
Mysql_pconnect ("5.6.7.8", "user", "pasword ");
Mysql_select_db ("test ");
For ($ J = 0; $ j <$ I; $ J ++) {// The I here is equal to the number of different data items in the table of host a and host B.
Mysql_query ("insert into table (TEST) values (\ '$ test [$ J] \')");
}
Mysql_close ($ link); // close the database connection of host B
?>
At this time, data synchronization between host a and host B is initially realized, but now you still need to manually activate this program each time. Is there a way to use it as a script, in crontab, how long does it automatically execute at the specified time?
When installing PHP, an executable file named PHP is automatically generated. Generally, it is under the/your installed PHP Directory/bin, but the earlier version does not seem to exist. If this file is not available, you have to upgrade your php.
# PHP-q test. php
PHP is originally used for webpage applications, so it will send HTML headers, but here we want to use PHP as a shell script, "-Q" means not to send headers.
Finally, edit the file in/etc/crontab and add the following sentence.
0 0 * root/home/httpd/html/test // run the/home/httpd/html/test file at every night (for details about how to use it, see cron)
OK. Mysql Data Synchronization is almost completed here. If you are still interested, you can think of a better way to synchronize and edit and delete the data.