Master-slave synchronization of mysql database, realize read-write separation, master-slave synchronization of mysql database, realize read-write separation

The master-slave synchronization of mysql database realizes the separation of read and write

Preface

1 Install mysql 5.7 on two centos 7 systems respectively

2 configuration of the master server

2.1 Modification of the configuration file my.cnf

2.2 Create users and permissions from the server

2.3 Restart mysql service

2.4 View the status of the main server

3 Slave configuration

3.1 Modification of the configuration file my.cnf

3.2 restart mysql service

3.3 Connect to the master server

3.4 Start slave data synchronization

3.5 View slave information

4 test

5 Resolve errors

6 Summary


Preface

In order to soften the large number of concurrent visits, large-scale websites are far from enough in addition to implementing distributed load balancing on the website. When it comes to the data business layer and data access layer, if it is still a traditional data structure, or just rely on a single server to handle so many database connection operations, the database will inevitably crash, especially if the data is lost, the consequences will be even more disastrous. At this time, we will consider how to reduce database connections, let's enter our topic today.

Use the master-slave database to realize the separation of reading and writing, thereby sharing the pressure of the master database. Deploy mysql on multiple servers, consider one of them as the master database and the others as slave databases to achieve master-slave synchronization. The master database is responsible for active write operations, while the slave database is only responsible for active read operations (slave slave databases will still passively write operations in order to maintain data consistency), which can largely avoid data loss It can also reduce database connections and reduce the load on the main database.

Let us look at the next picture:

 

In the above model, Mysql-A is the master server, or master, and Mysql-B is the slave server, or slave.

Database events in Mysql-A (such as modifying the sql operation statement of the database) will be stored in the log system A and sent to Mysql-B through the network on the corresponding port (default 3306). After Mysql-B receives it, it writes it to the local log system B, and then completes the database events in the database Mysql-B one by one.

Logging system A is the binary log in the MYSQL log type, which is specifically used to save all actions that modify the database table, namely the bin log. Note that MYSQL will write to the binary log after executing the statement and before releasing the lock to ensure the transaction Safety.

Logging system B is not a binary log. Because it is copied from MYSQL-A's binary log, it is not generated by its own database changes. It feels a bit of a relay. It is called a relay log, or relay log.

Through the above mechanism, it can be ensured that the database data of Mysql-A and Mysql-B are consistent, but there must be a delay in time, that is, the data of Mysql-B is lagging. Therefore, there will be such a problem. Mysql-A database operations can be executed concurrently, but Mysql-B can only read and execute one by one from the relay log. If Mysql-A writes frequently, Mysql-B may not be able to keep up.

There are several ways of master-slave synchronous replication:

(1) Synchronous replication, master changes must wait for slave-1, slave-2,...,slave-n to complete before returning.

(2) Asynchronous replication, the master only needs to complete its own database operations. As for whether slaves have received the binary log and completed the operation, don't care. The default setting of MYSQL.

(3) Semi-synchronous replication, the master only guarantees that one operation in slaves is successful, and then returns, regardless of other slaves. This function was introduced by Google for MYSQL.

This article is talking about the master-slave synchronization configuration of the mysql5.7 database implemented on the centos 7 system to achieve read-write separation.

 

1 Install mysql 5.7 on two centos 7 systems respectively

The specific installation steps can be found in this link, https://blog.csdn.net/qq_15092079/article/details/81629238 .

The IP addresses of the two servers in this article are the master server (192.168.17.130) and the slave server (192.168.17.132).

Create a test database on these two servers respectively for later testing.

 

2 configuration of the master server

2.1 Modification of the configuration file my.cnf

#根据上一篇文章,编辑my.cnf文件
[root@localhost mysql]# vim /etc/my.cnf
 
#在[mysqld]中添加:
server-id=1
log_bin=master-bin
log_bin_index=master-bin.index
binlog_do_db=test
#备注:
#server-id 服务器唯一标识。
#log_bin 启动MySQL二进制日志,即数据同步语句,从数据库会一条一条的执行这些语句。
#binlog_do_db 指定记录二进制日志的数据库,即需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可。
#binlog_ignore_db 指定不记录二进制日志的数据库,即不需要复制的数据库名,如果有多个数据库,重复设置这个选项即可。
#其中需要注意的是,binlog_do_db和binlog_ignore_db为互斥选项,一般只需要一个即可。

2.2 Create users and permissions from the server

#进入mysql数据库
[root@localhost mysql]# mysql -uroot -p
Enter password:
 
#创建从数据库的masterbackup用户和权限
mysql> grant replication slave on *.* to masterbackup@'192.168.17.%' identified by '123456';
#备注
#192.168.17.%通配符,表示0-255的IP都可访问主服务器,正式环境请配置指定从服务器IP
#若将 192.168.17.% 改为 %,则任何ip均可作为其从数据库来访问主服务器
 
#退出mysql
mysql> exit;
[root@localhost mysql]# service mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 

2.4 View the status of the main server

#进入mysql数据库
[root@localhost mysql]# mysql -uroot -p
Enter password:
 
#查看主服务器状态
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      154 | test         |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3 Slave configuration

3.1 Modification of the configuration file my.cnf

#根据上一篇文章,编辑my.cnf文件
[root@localhost mysql]# vim /etc/my.cnf
 
#在[mysqld]中添加:
server-id=2
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
#replicate-do-db=test
#备注:
#server-id 服务器唯一标识,如果有多个从服务器,每个服务器的server-id不能重复,跟IP一样是唯一标识,如果你没设置server-id或者设置为0,则从服务器不会连接到主服务器。
#relay-log 启动MySQL二进制日志,可以用来做数据备份和崩溃恢复,或主服务器挂掉了,将此从服务器作为其他从服务器的主服务器。
#replicate-do-db 指定同步的数据库,如果复制多个数据库,重复设置这个选项即可。若在master端不指定binlog-do-db,则在slave端可用replication-do-db来过滤。
#replicate-ignore-db 不需要同步的数据库,如果有多个数据库,重复设置这个选项即可。
#其中需要注意的是,replicate-do-db和replicate-ignore-db为互斥选项,一般只需要一个即可。
[root@localhost mysql]# service mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 

3.3 Connect to the master server

#进入mysql数据库
[root@localhost mysql]# mysql -uroot -p
Enter password:
 
#连接master主服务器
mysql> change master to master_host='192.168.17.130',master_port=3306,master_user='masterbackup',master_password='123456',master_log_file='master-bin.000001',master_log_pos=154;
#备注:
#master_host对应主服务器的IP地址。
#master_port对应主服务器的端口。
#master_log_file对应show master status显示的File列:master-bin.000001。
#master_log_pos对应show master status显示的Position列:154。

3.4 Start slave data synchronization

#启动slave数据同步
mysql> start slave;
#停止slave数据同步(若有需要)
mysql> stop slave;

3.5 View slave information

mysql> show slave status\G;

Slave_IO_Running and Slave_SQL_Running are both yes, it means the synchronization is successful.

 

4 test

(1) Log in to mysql on the main server, enter the test database, create a test table, and insert a piece of data

Tip: Here it is best to use database management tools (such as nacicat) to operate.

create table hi_tb(id int(3),name char(10));
insert into hi_tb values(001,'bobu');

(2) Log in to mysql on the slave server and enter the test database

You will find that from the database, the hi_tb table also appears, and there is 1 bobu data in the table, which proves that the data synchronization is successful.

Insert picture description here
Insert picture description here

5 Resolve errors

If in the process of master-slave synchronization, one of the statements fails to synchronize and an error is reported, then the subsequent statements must not be synchronized successfully. For example, the master database has a piece of data, but the slave database does not have this piece of data. However, if the operation of deleting this piece of data is executed in the master database, then the slave database will definitely not be able to delete it without such a piece of data, and an error will be reported. At this time, the data synchronization from the database fails, so the subsequent synchronization statement cannot continue to execute.

There are two solutions provided here:

(1) In the slave database, use SET global sql_slave_skip_counter to skip events, skip this error, and then execute from the next event group.

#在从数据库上操作
mysql > stop slave;
mysql > set global sql_slave_skip_counter=1;
mysql > start slave;

(2) In the slave database, reconnect to the master database. This kind of operation will directly skip the synchronization statements in the middle, which may cause some data not to be synchronized in the past, but this kind of operation is also the last trick. The best thing is to make the data structure and data of the slave database and the master database consistent, and then resume the operation of master-slave synchronization.

#在从数据库上操作
mysql > stop slave;
mysql> change master to master_host='192.168.17.130',master_port=3306,master_user='masterbackup',master_password='123456',master_log_file='master-bin.000001',master_log_pos=2050;
mysql > start slave;
 
#备注
#master_log_file和master_log_pos可能会不同,需要在主数据库中show master status来查看

6 Summary

At this point, the master-slave synchronization of the mysql database is completed. As for the separation of reading and writing, we can implement it through the program. Here is a brief explanation of the realization idea.

We can create a database user on the main server (for security, according to the needs of the corresponding permissions) mainly used for write operations, in the program through this user to connect to the main database is only used for write operations and not read operations.

Create a database user on the slave server (for security, only the permission to read select) is mainly used for read operations, and you can connect to the slave database through this user in the program.

Of course, you can also find a component to complete the proxy of MYSQL and implement the routing of SQL statements, so that we don't need to pay attention to which database is written and which database is read in the program.

Original link: https://blog.csdn.net/qq_15092079/article/details/81672920

 

Guess you like

Origin blog.csdn.net/weixin_43614067/article/details/108347664