How to synchronize Mysql Data

Source: Internet
Author: User

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 the php program.

<?

$ 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.