Teach you how to implement MySQL dual-host data synchronization (1)

Source: Internet
Author: User

Editor's note: many friends may not know where to start when getting started with MySQL's dual-machine synchronization requirements. In fact, this is one of the functions supported by MySQL itself. This article provides a preliminary idea about MySQL master-slave synchronization for your reference.

I. Requirements

Suppose there are currently two MySQL database servers. How can we achieve data synchronization between these two machines? That is, after the database is modified on one machine, the other machine will synchronously update the modified information.

2. Solutions

I found that MySQL supports one-way, asynchronous replication. During the replication process, one server acts as the master server, and the other or multiple other servers act as the slave server.

The principle is as follows:

The master server writes updates to binary log files and maintains an index of the files to track log loops. These logs can record updates sent to the slave server. When a slave server connects to the master server, it notifies the master server of the last successful update location read from the server in the log. The slave server accepts any updates that have occurred since then, blocks them, and waits for the master server to notify the new updates.

2.1 test environment

 
 
  1. Master : 192.168.7.67 (CentOS 5.5  x86_64 )   MySQL Version  :  5.0.77  
  2. Slave: 192.168.56.103 (CentOS 5.3 i386)  MySQL  Version : 5.0.45 

Note:

The MySQL version of Master and slave must be the same, or the version of Master is higher than that of Slave.

2.2 configuration process

2.2.1 Master side settings

Start MySQL and create a test database abc:

 
 
  1. Root @ camlit ~ :/Etc/init. d/mysqld start
  2. Jian. ma @ camlit ~ : Mysql-u root-p
  3. Enter password: xxxx
  4. Welcome to the MySQL monitor. Commands end with; or \ g.
  5. Your MySQL connection id is 3
  6. Server version: 5.0.77 Source distribution
  7.  
  8. Type 'help; 'or' \ H' for help. Type '\ C' to clear the buffer.
  9.  
  10. Mysql> create database abc;
  11. Query OK, 1 row affected (0.31 sec)
  12.  
  13. ### Create a user for synchronization, specifying that the user can only log on at 192.168.56.103
  14. ### Replication slave: Enable replication slaves to read binary log events from the master
  15.  
  16. Mysql> grant replication slave on *. * to 'test1' @ '192. 168.56.103 'identified by 'test1 ';
  17. Query OK, 0 rows affected (0.16 sec)

Modify the configuration file:

 
 
  1. root@camlit ~: vi /etc/my.cnf 

Note: Back up the configuration file before modifying it.

 
 
  1. [Mysqld]
  2. Datadir =/var/lib/mysql
  3. Socket =/var/lib/mysql. sock
  4. User = mysql
  5. Old_passwords = 1
  6.  
  7. # Add the following content
  8. Server_id = 1 ### 1 indicates master, 2 indicates slave binlog-do-db = abc ### database to be synchronized, if there are multiple databases, binlog-ignore-db = mysql ### Database log-bin = mysql-bin that does not need to be synchronized per database line
  9. [Mysqld_safe]
  10. Log-error =/var/log/mysqld. log
  11. Pid-file =/var/run/mysqld. pid

Restart the service:

 
 
  1. root@camlit ~: /etc/init.d/mysqld restart  

2.2.2 Slave client settings

Create the same database as the master: abc

 
 
  1. Enter password:   
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.   
  3. Your MySQL connection id is 5   
  4. Server version: 5.0.45-log Source distribution   
  5.    
  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.   
  7.    
  8. mysql> create database abc;   
  9. Query OK, 1 row affected (0.31 sec) 

Modify the configuration file:

 
 
  1. root@test2 ~: vi /etc/my.cnf 

 
 
  1. [Mysqld]
  2. Datadir =/var/lib/mysql
  3. Socket =/var/lib/mysql. sock
  4. User = mysql
  5. Old_passwords = 1
  6. ### Add the following content
  7. Server_id = 2 log-bin = mysql-bin master-host = 192.168.7.67 master-user = test1 master-password = test1 master-port = 3306 master-connect-retry = 10 ### connection count replicate-do-db = abc ### accepted database name replicate-ignore-db = mysql ### unacceptable Database
  8. [Mysqld_safe]
  9. Log-error =/var/log/mysqld. log
  10. Pid-file =/var/run/mysqld. pid

Restart the service:

 
 
  1. root@test2~: /etc/init.d/mysqld restart  

Note:

After the configuration is successful, the master.info file is generated in the mysql directory (/var/lib/mysql/). To change the slave settings, you must delete the file before it takes effect.

Enter mysql and enter the following command:

 
 
  1. Root @ test2 ~ : Mysql-u root-p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with; or \ g.
  4. Your MySQL connection id is 4
  5. Server version: 5.0.45-log Source distribution
  6. Type 'help; 'or' \ H' for help. Type '\ C' to clear the buffer.
  7. Mysql> slave start;
  8. Query OK, 0 rows affected, 1 warning (0.00 sec)
  9. ### View synchronization conditions
  10. Mysql> show slave status; or show master status;

2.3 Result Test

Perform operations on Database abc on the Master side as follows:

 
 
  1. jian.ma@camlit ~: mysql  -u root -p   
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.   
  4. Your MySQL connection id is 3   
  5. Server version: 5.0.77-log Source distribution   
  6.    
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.   
  8.    
  9. mysql> use abc;   
  10. Database changed   
  11. mysql> create table test1 (IP VARCHAR(20),USER VARCHAR(100), MAIL   
  12. VARCHAR(100));   
  13. Query OK, 0 rows affected (1.20 sec)   
  14. mysql> insert into test1(IP,USER,MAIL) values('192.168.7.66', 'test', 'test@test.com.cn');   
  15. Query OK, 1 row affected (0.06 sec)  

Check whether the Server Load balancer instance can be updated:

 
 
  1. root@test2 ~: mysql -u root -p   
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.   
  4. Your MySQL connection id is 6   
  5. Server version: 5.0.45-log Source distribution   
  6.    
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.   
  8.    
  9. mysql> show databases;   
  10. +--------------------+   
  11. | Database   |   
  12. +--------------------+   
  13. | information_schema |   
  14. | foo|   
  15. | mysql  |   
  16. | test   |   
  17. |abc  |   
  18. +--------------------+   
  19. 5rows in set (0.00 sec)   
  20. mysql> use abc;   
  21. Reading table information for completion of table and column names   
  22. You can turn off this feature to get a quicker startup with -A   
  23. Database changed   
  24. mysql> show tables;   
  25. +---------------+   
  26. | Tables_in_abc |   
  27. +---------------+   
  28. | test1 |   
  29. +---------------+   
  30. 1 row in set (0.03 sec)   
  31.  mysql> select * from test1;   
  32. +--------------+------+------------------+   
  33. | IP   | USER | MAIL |   
  34. +--------------+------+------------------+   
  35. | 192.168.7.66 | test | test@test.com.cn |   
  36. +--------------+------+------------------+   
  37. 1 row in set (0.00 sec)  

From the above results, we can see that the data on the Master side can be synchronized to the Slave side. It indicates that the synchronization problem of the master-slave database has been successfully solved.


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.