mysql master-slave replication and read-write separation

Table of contents

1. Understand the principle of master-slave replication

2. Complete mysql master-slave replication

Main library configuration

 Plex configuration

3. Realize mysql read and write separation

3.1 Install mycat2 software

3.2 Use docker to configure a master and 2 slaves

3.3 Create a data source

3.4 Create a cluster

3.5 Create logic library

3.6 Modify the data source of the logic library

3.7 Test whether read-write separation is successful

3.8 Testing


1. Understand the principle of master-slave replication

(1) The master server records data changes in the binary binlog log, and when the data on the master changes, it writes the change into the binary log; (2) The slave
 server detects the master binary log within a certain time interval Whether it changes, if it changes, start an I/O Thread to request the master binary event
 (3) At the same time, the master node starts a dump thread for each I/O thread to send the binary event to it and save it to the slave In the local relay log of the node, the slave node will start the SQL thread to read the binary log from the relay log and replay it locally so that its data is consistent with that of the master node. Finally, I/OThread and SQLThread will go to sleep and wait for wake up next time.
To put it simply:
the slave library will generate two threads, one I/O thread and one SQL thread; the
I/O thread will request the binlog of the main library, and write the obtained binlog to the local relay-log (relay log ) file;
the main library will generate a log dump thread to pass the binlog to the slave library I/O thread; the
SQL thread will read the logs in the relay log file and parse them into SQL statements to execute one by one;

2. Complete mysql master-slave replication

Main library configuration

1.主库需要在配置文件/etc/my.cnf设置server_id的值并开启binglog参数
[mysqld]
log_bin=mysql-bin
server_id=120
2.需要创建一个同步账号,并给与主从同步的权限,只用给主库配置
mysql> grant replication slave on *.* to 'rep'@'192.168.27.%' identified by '123456';
mysql> show grants for 'rep'@'192.168.27.%';
3.主库设置锁表设置保证数据一致性
mysql> flush tables with read lock; 
4.主库备份主库的数据库
[root@localhost backup]# mysqldump -uroot -p'Admin123!' -B chap01  db1  gtid  school > /server/backup/db.sql
5.将备份的数据库发给丛库主机
[root@localhost backup]# scp /server/backup/db.sql 192.168.27.121:/tmp
[root@localhost backup]# scp /server/backup/db.sql 192.168.27.122:/tmp
6.在丛库上还原刚才传送的数据库文件,保证主从库一致
[root@localhost tmp]# mysql -uroot -p'Admin123!' < /tmp/db.sql 
[root@localhost tmp]# mysql -uroot -p'Admin123!' < /tmp/db.sql 
7.主库解锁
mysql> unlock tables;
8.查看主库的binlog日志以及其pos位置
show master status;

 Plex configuration

1.丛库1配置
mysql> change master to
    -> master_host="192.168.27.120",
    -> master_user="rep",
    -> master_password="123456",
    -> master_log_file="mysql-bin.000001",
    -> master_log_pos=450;
2.开启主从复制
mysql> start slave;
3.验证主从复制是否成功,查看以下两条是否都为yes
mysql> show slave status \G
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
4.在另外一个从主机执行一样的配置
5.在主库上创建,删除数据库,在丛库上查找

3. Realize mysql read and write separation

3.1 Install mycat2 software

一.yum list | grep "jdk" java-1.8.0-openjdk.x86_64
1.使用xftp将jdk下载到家目录
2.使用yum install安装
[root@localhost ~]# yum localinstall -y jdk-8u261-linux-x64.rpm
3.查看是否下载好了java
[root@localhost ~]# java -version
1)安装程序包:http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-1.21.zip
2)jar包:http://dl.mycat.org.cn/2.0/1.21-release/mycat2-1.21-release-jar-with-dependencies.jar

二.1.创建/data目录
[root@localhost ~]# mkdir /data
2.安装解压工具
[root@localhost ~]# yum install -y unzip
3.先解压mycat2-install-template-1.21.zip
[root@localhost ~]# unzip mycat2-install-template-1.21.zip -d /data
4.将mycat2-1.21-release-jar-with-dependencies.jar
移动到/data/mycat/lib/目录下
[root@localhost ~]# mv mycat2-1.21-release-jar-with-dependencies.jar /data/mycat/lib/
[root@localhost mycat]# ll
total 8
drwxr-xr-x. 2 root root 4096 Mar  5  2021 bin 	执行操作
drwxr-xr-x. 9 root root  275 Mar  5  2021 conf 	配置文件
drwxr-xr-x. 2 root root 4096 Mar 26 17:55 lib	第三方的库,jar包就在这里
drwxr-xr-x. 2 root root    6 Mar  5  2021 logs	日志文件
5.给/data/mycat/bin/文件给予执行权限
[root@localhost mycat]# chmod +x bin/*


三.1.修改配置文件mycat/conf/datasources/prototypeDs.datasource.json
[root@localhost mycat]# vim /data/mychat/conf/datasources/prototypeDs.datasource.json
{
	"dbType":"mysql",
	"idleTimeout":60000,
	"initSqls":[],
	"initSqlsGetConnection":true,
	"instanceType":"READ_WRITE",
	"maxCon":1000,
	"maxConnectTimeout":3000,
	"maxRetryCount":5,
	"minCon":1,
	"name":"prototypeDs",
	"password":"123456",
	"type":"JDBC",
	"url":"jdbc:mysql://localhost:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
	"user":"root",
	"weight":0
}
四.1.查看mycat状态
[root@localhost mycat]# cd ./bin/
[root@localhost bin]# ./mycat status
mycat2 is not running.
2.启动mycat
[root@localhost bin]# ./mycat start
Starting mycat2...
	"password":"123456",


3.2 Use docker to configure a master and 2 slaves

3.3 Create a data source

在mycat上输入
配置读写数据源
/*+ mycat:createDataSource{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ_WRITE",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"m1",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://127.0.0.1:3307/db1?
useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",
"user":"root",
"weight":0
} */;

配置两个从库只读数据源
/*+ mycat:createDataSource{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"m1s1",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://127.0.0.1:3308/db1?
useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",
"user":"root",
"weight":0
} */;

3.4 Create a cluster

在mycat
/*! mycat:createCluster{
"clusterType":"MASTER_SLAVE",
"heartbeat":{
"heartbeatTimeout":1000,
"maxRetry":3,
"minSwitchTimeInterval":300,
"slaveThreshold":0
},
"masters":[
"m1"
],
"maxCon":2000,
"name":"prototype",
"readBalanceType":"BALANCE_ALL",
"replicas":[
"m1s1","m1s2"
],
"switchType":"SWITCH"
} */;

3.5 Create logic library

在mycat中创建逻辑库
CREATE DATABASE db1 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

3.6 Modify the data source of the logic library

[root@node4 mysqlms]# cat /data/mycat/conf/schemas/db1.schema.json
{
"customTables":{},
"globalTables":{},
"normalProcedures":{},
"normalTables":{},
"schemaName":"db1",
"shardingTables":{},
"targetName":"prototype",
"views":{}
}

3.7 Test whether read-write separation is successful

restart mycat

[root@node4 mysqlms]# cd /data/mycat/bin/
[root@node4 bin]# ./mycat restart

3.8 Testing

Create a table in mycat and insert data, view it in mysq2 and mysql3 respectively

Guess you like

Origin blog.csdn.net/weixin_62173637/article/details/129798544