MySQL master-slave replication realizes read-write separation

MySQL master-slave replication realizes read-write separation

1. Principle of read-write separation

1.1 What is master-slave synchronization

Refers to one server acting as the master database server, and another or more servers acting as the slave database server. The data in the master server is automatically copied to the slave server. In fact, the slave server replays the operation steps of the master server to achieve the consistency of master and slave data.

1.2 Why implement master-slave synchronization

(1) Achieve server load balancing.
Through the division of the master and slave servers before processing customer query compliance, so as to obtain better customer response time. The main programs are:

  • The master handles the functions of adding, modifying, and deleting records, and delegates the query to the slave.
  • The master handles the functions of adding, modifying, and deleting records, and the master also has to undertake some query tasks, while the slave is only responsible for querying data. When the master server is busy, some query requests will be automatically sent to the slave server to reduce the workload of the master server.

(2) Realize remote backup of data.
Periodically copy data from the master server to the slave server, realizing remote backup. Compared with backing up the data locally, this method can avoid reducing the performance of the master server, and at the same time reduce the risk of unrecoverable data due to damage to the master server's hard disk and server abnormalities.

(3) Improve the availability of the database system.
When a problem occurs on the master server, the slave server can be used as the master server immediately for data update and query services.

1.3 How to achieve master-slave synchronization

Replication types supported by mysql :

  • Statement-based replication: SQL statements executed on the master server execute the same statements on the slave server. MySQL uses statement-based replication by default, which is more efficient. Once it is found that it is impossible to copy accurately, it will automatically select line-based copy.
  • Row-based replication: copy the changed content instead of executing the command on the slave server. Supported from mysql5.0
  • Mixed types of replication: Statement-based replication is adopted by default. Once it is found that statement-based replication cannot be accurate, row-based replication will be adopted.
    Msyql master-slave replication principle

Analysis of the working principle of master-slave replication

1. As long as there is a change in the Master database, it is immediately recorded in the Binary log log file 2. The
Slave database starts an I/O thread to connect to the Master database and requests the binary log of the Master change 3. The binary log
obtained by Slave I/O is saved to In its own Relay log log file.
4. Slave has a SQL thread to periodically check whether the Realy log changes, and then update the data

2. Database environment preparation

Here I have prepared two servers (centos7), mysql5.7.30 is installed on them, and the network of the two servers is smooth:

Master server (master) : 192.168.0.101
Slave server (slave) : 192.168.0.105

Synchronize the database data to be synchronized by the master database to the slave database first.

3. Master configuration

3.1 Modify the mysql configuration file

Add the following configuration in the msyql configuration file (mainly configure server-id, log-bin and binlog-do-db):

vim /etc/my.cnf
# 主从设置
server-id=1                 # 节点ID,确保唯一

# log config
log-bin= mysql-bin          #开启mysql的binlog日志功能
binlog-do-db=oyc            #二进制日志记录的数据库(多数据库用逗号,隔开)
#控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
sync_binlog=1               
binlog_format=mixed         #binlog日志格式,mysql默认采用statement,建议使用mixed
expire_logs_days=7          #binlog过期清理时间
max_binlog_size=100m        #binlog每个日志文件大小
binlog_cache_size=4m        #binlog缓存大小
max_binlog_cache_size=512m  #最大binlog缓存大
binlog-ignore-db=mysql      #不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
auto-increment-offset=1     # 自增值的偏移量
auto-increment-increment=1  # 自增值的自增量
slave-skip-errors=all       #跳过从库错误

3.2 Restart mysql

systemctl restart mysql 
或者 service mysqld restart  

3.3 Create synchronization user and authorize

1. Enter the master database and create a replication user for the master

create user oyc identified by '123456';

2. Give the user the right to copy

grant replication slave on *.* to 'oyc'@'192.168.0.105'  identified by '123456';
flush privieges;

3. View the status of the master

show master status;

master status

4. Slave configuration

4.1 Modify the configuration file from the library

Synchronization table configuration instructions:

  • replicate-do-db set the database to be replicated (multiple databases use commas, separated)
  • replicate-ignore-db set the replicate database to be ignored (multi-database use comma, separated)
  • replicate-do-table sets the table to be replicated
  • replicate-ignore-table set the replicate table to be ignored
  • replicate-wild-do-table has the same function as replication-do-table, but wildcards can be used
  • replicate-wild-ignore-table has the same function as replication-ignore-table, but wildcards can be added
[mysqld]
server-id = 2 # server_id是必须的,而且唯一。
log-bin=mysql-bin  
relay-log = mysql-relay-bin
replicate-do-db=oyc
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

4.2 Configure slave library connection information

Let the slave connect to the master and start redoing the events in the master's binary log.

CHANGE MASTER TO MASTER_HOST='192.168.0.100',MASTER_USER='oyc',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=0;

4.3 Start Salve and view slave library status

START SLAVE;

SHOW SLAVE STATUS;

slave status
See Slave_IO_Running=Yes Slave_SQL_Running=Yes, indicating success.

5. Test results

5.1 Data before synchronization

Data before synchronization

5.2 Insert a piece of data in the master, and the slave also inserts the data synchronously

master inserts data
Synchronization effect:
Synchronization effect
The addition, modification, and deletion of data in the master table will be synchronized to the slave. The addition, modification, and deletion of the master table, changes in views, and stored procedures will all be synchronized.

6. Synchronization error resolution

6.1 Ignore the error and continue to sync

This method is suitable for situations where there is little difference between the master and slave database data, or the data requirements may not be completely unified, and the data requirements are not strict.

stop slave; 

# 表示跳过一步错误,后面的数字可变
set global sql_slave_skip_counter =1;

start slave;

# 查询同步情况
show slave status\G

6.2 Ignore unnecessary tables

replicate-ignore-table=table1,table2,oyc_%

6.3 Resynchronize data and then synchronize

1. Enter the main library first, lock the table to prevent data from being written
. Command:

mysql> flush oyc with read lock;

Note: This place is locked as read-only, and the statement is not case sensitive

2. Perform data backup, back up
the data to the mysql.bak.sql file

[root@master mysql]#mysqldump -uroot -p -hlocalhost > mysql.bak.sql

One thing to note here: database backups must be performed regularly, shell scripts or python scripts can be used, which is more convenient to ensure that the data is foolproof

3. View master status

mysql> show master status;
+——————-+———-+————–+——————————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————-+———-+————–+——————————-+
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema |
+——————-+———-+————–+——————————-+
1 row in set (0.00 sec)

4. Transfer the mysql backup file to the slave machine for data recovery

Use scp command

[root@server01 mysql]# scp mysql.bak.sql [email protected]:/tmp/

5. Stop the state from the library

mysql> stop slave;

6. Then execute the mysql command from the library to import the data backup

mysql> source /tmp/mysql.bak.sql

7. Set the synchronization of the slave library, pay attention to the synchronization point there, which is the | File| Position in the show master status information of the master library

change master to master_host = ‘192.168.1.101’, master_user = ‘oyc’, master_port=3306, master_password='123456', master_log_file = ‘mysqld-bin.000002’, master_log_pos=1002;

8. Restart slave synchronization

mysql> start slave;

9. Unlock master

unlock table;

10. View synchronization status

mysql> show slave status\G 查看同步情况

Guess you like

Origin blog.csdn.net/u014553029/article/details/106222188