MYSQL master-slave synchronous replication under Centos6.5 for read-write separation

Suppose:

master server -> 192.168.1.110

Slave -> 192.168.1.111

 

1. Configure the master server Master

 

   A) Modify the master server configuration file

vim /etc/my.cnf
log-bin=mysql-bin #[must] enable binary logging
server-id=1 #[required] server unique ID
sync_binlog=1 #After 1 log write operation, write the log file to the hard disk once (synchronize the log information once). n=1 is the safest practice, but the least efficient. The default setting is n=0.

# After saving, restart the database
service mysqld restart

 

    B) Create an account on the master server and authorize the slave

mysql>GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456'; //The specific client IP can be replaced here, such as 192.168.1.111, to strengthen security.
mysql>flush privileges;

 

      C) Back up the MYSQL data of the main server

mysqldump -u root -p666666 test -l -F > /home/wangkun/test.sql
#Export in read lock mode to prevent someone from modifying the database during the export process, resulting in inconsistency.

 

      D) View MASTER status

   mysql>show master status;
   +------------------+----------+--------------+------------------+
   | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   +------------------+----------+--------------+------------------+
   | mysql-bin.000004 |      308 |              |                  |
   +------------------+----------+--------------+------------------+
   1 row in set (0.00 sec)
   Note: Do not operate the master server MYSQL after this step to prevent the state value of the master server from changing

 

 2. Configure the slave server

 

   A) Modify the slave server configuration file

vim /etc/my.cnf
server-id=2 #[required] server unique ID

# After saving, restart the database
service mysqld restart

 

     B) Configure slave server Slave

mysql>change master to master_host='192.168.1.110',master_user='backup',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=308;
mysql>start slave; #Start the slave server replication function

 

     C) Check the replication function status from the server

   mysql> show slave status\G

   *************************** 1. row ***************************

              Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.2.222 //Master server address
              Master_User: mysync //authorized account name, try to avoid using root
              Master_Port: 3306 //Database port, some versions do not have this line
              Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
              Read_Master_Log_Pos: 600 //#The position of reading the binary log synchronously, greater than or equal to Exec_Master_Log_Pos
              Relay_Log_File: ddte-relay-bin.000003
              Relay_Log_Pos: 251
              Relay_Master_Log_File: mysql-bin.000004
              Slave_IO_Running: Yes //This state must be YES
              Slave_SQL_Running: Yes //This state must be YES
                    ......

Note: The Slave_IO and Slave_SQL processes must be running normally, that is, the YES state, otherwise it is an error state (for example: one of the NO is an error).

 

The above operation process, the master-slave server configuration is completed, you can start the test.

Insert a piece of data on the master server, you can see if the database of the slave server has been updated and it is OK.

 

Master-slave replication synchronization problem
Since my database is version 5.5, I generally use MYSQL master-slave replication, there will be a certain delay in the replication process, and sometimes it will reach 20 minutes or even higher in the case of high peaks.

How to solve the synchronization problem?

Two solutions:
1. Upgrade MYSQL to 5.7, because MYSQL of 5.7+ has completely solved the synchronization problem.
Please refer to http://www.kancloud.cn/thinkphp/mysql-parallel-applier
2. Control the program, write the cache while writing to the database, and read the data from the cache first and then read the database.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326970541&siteId=291194637