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.
& Lt ;?
$ 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 ");
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.