MySQL master-slave Database Synchronization enhances System Security

Source: Internet
Author: User

Test environment:

Master server: CentOS 5.2 Mysql 5.1.35 source code IP: 192.168.1.22

Slave server: CentOS 5.2 Mysql 5.1.35 source code IP: 192.168.1.33

Configuration:

I. master server

1.1 create a replication user with the replication slave permission.

 
 
  1. mysql>grant replication slave on *.* to ‘repl’@’192.168.1.22′ identified by ‘repl’; 

1.2 edit the my. cnf File

Vi/etc/my. cnf

Add

Server-id = 1

And enable the log-bin binary log file.

Log-bin = mysql-bin

Note: remove the default server-id = 1.

1.3 start the mysql database

 
 
  1. mysqld_safe –user=mysql & 

1.4 set the read lock

 
 
  1. mysql>flush tables with read lock; 

1.5. Get the binlog Log File Name and offset.

 
 
  1. mysql>show master status;  
  2. +——————+———-+————–+——————+  
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  4. +——————+———-+————–+——————+  
  5. | mysql-bin.0000010 | 106| | |  
  6. +——————+———-+————–+——————+ 

1.6 back up the database to be synchronized

 
 
  1. mysqldump test > test.sql 

1.7 unlock

 
 
  1. mysql>unlock tables; 

Ii. slave server

2.1 edit the my. cnf File

Vi/etc/my. cnf

Add

Server-id = 2

Note: remove the default server-id = 1.

2.2 start slave Database

 
 
  1. mysqld_safe –user=mysql & 

2.3 configure slave Database

 
 
  1. mysql> change master to  
  2. -> master_host=’192.168.1.22′  
  3. -> master_user=’repl’  
  4. -> master_password=’repl’  
  5. -> master_log_file=’mysql-bin.0000010′  
  6. -> master_log_pos=106; 

2.4 start the slave server slave thread

 
 
  1. mysql>start slave; 

Run the show processlist command to display the following processes:

 
 
  1. mysql>show processlist\G  
  2.  
  3. *************************** 2. row ***************************  
  4. Id: 2  
  5. User: system user  
  6. Host:  
  7. db: NULL  
  8. Command: Connect  
  9. Time: 2579  
  10. State: Has read all relay log; waiting for the slave I/O thread to update it 

Info: NULL indicates that the slave has been connected to the master, and the log is accepted and executed.

2.5 view the slave thread status

 
 
  1. mysql>show slave status;  
  2. *************************** 1. row ***************************  
  3. Slave_IO_State: Waiting for master to send event  
  4. Master_Host: 192.168.1.22  
  5. Master_User: repl  
  6. Master_Port: 3306  
  7. Connect_Retry: 60  
  8. Master_Log_File: mysql-bin.0000010  
  9. Read_Master_Log_Pos: 106  
  10. Relay_Log_File: centos-relay-bin.000002  
  11. Relay_Log_Pos: 529  
  12. Relay_Master_Log_File: mysql-bin.0000010  
  13. Slave_IO_Running: Yes  
  14. Slave_SQL_Running: Yes  
  15. Replicate_Do_DB:  
  16. Replicate_Ignore_DB:  
  17. Replicate_Do_Table:  
  18. Replicate_Ignore_Table:  
  19. Replicate_Wild_Do_Table:  
  20. Replicate_Wild_Ignore_Table:  
  21. Last_Errno: 0  
  22. Last_Error:  
  23. Skip_Counter: 0  
  24. Exec_Master_Log_Pos: 106  
  25. Relay_Log_Space: 830  
  26. Until_Condition: None  
  27. Until_Log_File:  
  28. Until_Log_Pos: 0  
  29. Master_SSL_Allowed: No  
  30. Master_SSL_CA_File:  
  31. Master_SSL_CA_Path:  
  32. Master_SSL_Cert:  
  33. Master_SSL_Cipher:  
  34. Master_SSL_Key:  
  35. Seconds_Behind_Master: 0  
  36. Master_SSL_Verify_Server_Cert: No  
  37. Last_IO_Errno: 0  
  38. Last_IO_Error:  
  39. Last_SQL_Errno: 0  
  40. Last_SQL_Error:  
  41. 1 row in set (0.00 sec) 

Verify that the configuration is correct

Run

 
 
  1. show slave status\G;  
  2. Waiting for master to send event  
  3. Slave_IO_Running: Yes  
  4. Slave_SQL_Running: Yes 

If the preceding two rows are both "Yes", the configuration is successful.

Test:

1. Create a user table in the master server test database

 
 
  1. mysql>use test;  
  2. mysql>create table user(id int); 

2. view the user table on the slave server

 
 
  1. mysql>use test;  
  2. mysql> show tables like ‘user’;  
  3. +———————-+  
  4. | Tables_in_test(user) |  
  5. +———————-+  
  6. | user |  
  7. +———————-+  
  8. 1 row in set (0.00 sec) 

The Master/Slave Data is successfully synchronized.

FAQs:

1. It appears when you view the slave status from the database

The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the-replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it)

The server-id in my. cnf is the same from the server.

Solution:

Modify the server-id in my. cnf and restart the database service. The my. cnf file has server-id = 1 by default.

Other Instructions

Master server my. cnf

# Binlog-do-db = Name of the database to be backed up. Multiple rows can be written.

# Binlog-ignore-db = Name of the database that does not need to be backed up. Multiple rows can be written.

Slave server my. cnf

# Replicate-do-db = Name of the database to be backed up by test

# Replicate-ignore-db = databases ignored by mysql

# Master-connect-retry = 60 if the master server is disconnected from the server, the time difference between the reconnection (seconds)

You can also directly modify the my. cnf configuration file using the following settings.

 
 
  1. log-bin=mysql-bin  
  2. master-host=192.168.1.22  
  3. master-user=repl 
  4. master-password=repl 
  5. master-port=3306 

Synchronous maintenance of Master/Slave servers

Due to various reasons, the Master/Slave Data is inconsistent. When the load is low, manual synchronization is performed.

Run

 
 
  1. mysql>flush tables with read lock;  
  2. Query OK,rows affected (0.01 sec)  
  3. mysql>show master status;  
  4. +——————+———-+————–+——————+  
  5. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  6. +——————+———-+————–+——————+  
  7. | mysql-bin.0000011 | 260| | |  
  8. +——————+———-+————–+——————+ 

Run

First obtain the binary file name and offset of the current master server, and execute the command to synchronize the slave server with the master server.

 
 
  1. mysql>select master_pos_wait(‘mysql-bin.0000011′,’260′);  
  2. +————————————————–+  
  3. | master_pos_wait(‘mysql-bin.0000011′,’260′) |  
  4. +————————————————–+  
  5. | 0 |  
  6. +————————————————–+  
  7. 1 row in set (0.01 sec) 

After synchronization, unlock the master server

 
 
  1. mysql>unlock tables; 

Switch Master/Slave servers

When the master server fails, use the slave server as the master server. The steps are as follows:

1. Ensure that all updates in the relay log have been executed in all slave databases and executed on the slave server.

Stop slave io_thread, use show processlist to check whether the status is Has read all relay log, indicating that the update is complete.

 
 
  1. mysql>stop slave io_thread;  
  2. Query OK,0 affected (0.00 sec)  
  3. mysql>show processlist\G;  
  4. *************************** 2. row ***************************  
  5. Id: 2  
  6. User: system user  
  7. Host:  
  8. db: NULL  
  9. Command: Connect  
  10. Time: 4757  
  11. State: Has read all relay log; waiting for the slave I/O thread to update it  
  12. Info: NULL 

2. Execute the stop slave and reset master commands on the slave server and reset them to the master database.

 
 
  1. mysql>stop slave;  
  2. Query OK,0 affected (0.00 sec)  
  3. mysql>reset master;  
  4. Query OK,0 affected (0.00 sec) 

3. Delete the master.info and relay-log.info files in the new master server database directory, otherwise it will start according to the slave server next restart.

Edit recommendations]

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.