MYSQL database master-slave replication
1 Read-write separation architecture
1.1 Introduction
In order to reduce the pressure on the database, the two most basic solutions in our minds are
-
A caching layer is constructed between the application and the database to store commonly used data in the cache. When the application reads the data, it first fetches it from the cache, and then fetches the data from the database when it is missed.
- Deploy two databases with consistent data, one for write operations [primary database] and one for read operations [slave database] to achieve data read-write separation.
What I want to introduce now is the solution for read-write separation [mysql master-slave replication]
For this, we need to meet the following requirements
-
Master-slave database data must be consistent
Database master-slave replication mechanism can be used to achieve data synchronization
-
Write data operation is the main database
Use dynamic data sources in applications to connect write data requests to the main database
-
The read data operation is from the database
Use dynamic data sources in applications to connect read data requests to the main database
2 mysql master-slave replication deployment
2.1 Description
Principle of master-slave replication:
- Update events (update, insert, delete) of the main database db are written to binlog
- The main library creates a binlog dump thread and sends the contents of the binlog to the slave library
- Start from the library and initiate a connection to connect to the main library
- After starting from the library, create an I/O thread, read the binlog content from the main library and write it to the relay log
- After starting from the library, create a SQL thread, read the content from the relay log, execute the read update event from the Exec_Master_Log_Pos position, and write the update content to the slave's db. This MySQL master-slave replication is implemented using the docker container , Need to have certain docker operation ability.
The principle of master-slave replication above is excerpted from [ https://blog.csdn.net/zai_xia/article/details/90379016 ]
2.2 Deploy the main database
- Download percona 5.7.23 image
docker pull percona:5.7.23
-
View mirror
docker images
-
Create host mysql data volume
mkdir -p /data/mysql/master //自行定义数据卷存放目录,要与启动docker容器宿主机的数据卷一致 cd /data/mysql/master //进入数据卷目录 mkdir conf data //创建两个目录,conf 用于存放mysql的配置文件,data用于存放mysql的数据 chmod 777 * -R //给目录授权
-
Create msyql configuration file
cd /data/mysql/master/conf //进入mysql存放配置文件目录 vim my.cnf //创建mysql配置文件
The content of the configuration file [my.conf]
[mysqld] log-bin=mysql-bin #开启二进制日志 server-id=1 #服务id,在集群内不允许重复 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' #解决mysql5.7 版本以上出现 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and错误
-
Create mysql main database container
docker create --name percona-master -v /data/mysql/master/data:/var/lib/mysql -v /data/mysql/master/conf:/etc/my.cnf.d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root percona:5.7.23//创建主数据库容器 docker ps -a //查看是否创建成功
-v specifies the host [/data/mysql/master/data] mapping container [/var/lib/mysql], [/data/mysql/master/conf] mapping container [/etc/my.cnf.d]
-
Start the container and see if it started successfully
docker start percona-master //启动mysql-master容器 docker logs -f percona-master //查看mysql-master容器日志
-
Use mysql client to authorize copy from database
Use Navicat to connect to the main database [Tools can be selected by yourself]
]
Execute SQL statements for creating users and authorization
create user 'pango'@'%' identified by 'pango';//创建用户
grant replication slave on *.* to 'pango'@'%'; //授权复制权限给用户
flush privileges;//刷新权限
View the status of the main database
show master status;
File mysql-bin.000003 and location 741, which are needed to synchronize the master database from the database
View binary file information
show global variables like 'binlog%';
2.3 Deploy the slave database
The steps to create the slave database container are roughly the same as the steps to create the master database container, so I won’t make too many notes here
-
Create a data volume directory from the database on the host
mkdir /data/mysql/slave cd /data/mysql/slave mkdir conf data chmod 777 * -R
-
Create configuration file
cd /data/mysql/slave/conf vim my.cnf
Configuration file [my.cnf] content:
[mysqld] log-bin=mysql-bin #开启二进制日志 server-id=2 #服务id,在集群内不允许重复 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' #解决mysql5.7 版本以上出现 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and错误
-
Create a slave database container
docker create --name percona-slave -v /data/mysql/slave/data:/var/lib/mysql -v /data/mysql/slave/conf:/etc/my.cnf.d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root percona:5.7.23
-
Start the container
docker start percona-slave //启动mysql-slave docker logs -f percona-slave //查看mysql-slave
-
Use the client to execute the sql statement to set the main database configuration
CHANGE MASTER TO master_host='localhost', master_user='pango', master_password='pango', master_port=3306, master_log_file='mysql-bin.000003', master_log_pos=741;
-
Start synchronization from the database
start slave;
-
View the status from the database
show slave status
-
test
Create a new library or table from the master database and check whether the same table and library exist in the slave database. If it exists, it means the master-slave replication deployment is successful