MySQL 5.7 延迟复制环境搭建和测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 5.7 延迟复制环境搭建和测试相关的知识,希望对你有一定的参考价值。

安装主从 mysql 5.7

# 主 MySQL5.7
cd /usr/local
tar -xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
cd mysql
mkdir data conf
vim conf/my.cnf
-------------------------------------------
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
log-error=/tmp/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
server-id=10
port=3366
gtid_mode=on
enforce_gtid_consistency=on
log_bin
binlog_format=row
-------------------------------------------
export PATH=$PATH:/usr/local/mysql/bin
mysqld --defaults-file=/usr/local/mysql/conf/my.cnf --initialize --user=mysql
mysqld_safe --defaults-file=/usr/local/mysql/conf/my.cnf --user=mysql &
# 从 MySQL5.7
cd /usr/local
tar -xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
cd mysql
mkdir data conf
vim conf/my.cnf
-------------------------------------------
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
log-error=/tmp/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
server-id=20
port=3366
gtid_mode=on
enforce_gtid_consistency=on
log_bin
binlog_format=row
-------------------------------------------
export PATH=$PATH:/usr/local/mysql/bin
mysqld --defaults-file=/usr/local/mysql/conf/my.cnf --initialize --user=mysql
mysqld_safe --defaults-file=/usr/local/mysql/conf/my.cnf --user=mysql &

主从节点更改 [email protected]‘localhost‘ 密码和创建复制账号

set sql_log_bin=0;
alter user [email protected]‘localhost‘ identified by ‘MySQL5.7‘;
create user [email protected]‘%‘ identified by ‘MySQL5.7‘;
grant all on *.* to [email protected]‘%‘;
flush privileges; 
set sql_log_bin=1;

将主节点的数据传到从节点

mysqldump -S /usr/local/mysql/data/mysql.sock -u root -pMySQL5.7 --all-databases --triggers --routines --events --single-transaction --quick | mysql -h 192.168.1.226 -P 3366 -u root -pMySQL5.7

将从服务器指定到主服务器

change master to master_host=‘192.168.1.225‘,master_port=3366,master_user=‘root‘,master_password=‘MySQL5.7‘,master_auto_position=1,MASTER_DELAY=120;
start slave;
show slave statusG

在主服务器使用以下脚本插入数据

shell> cat mysql.sh 
#!/bin/bash  
while true
do
    mysql -S /usr/local/mysql/data/mysql.sock -u root -pMySQL5.7 -e "insert into testdb.dt values(now())";
    sleep 1
done  

结果:从服务器的数据延时120秒生成,主服务器停机后2分钟,从服务器持续更新2分钟

以上是关于MySQL 5.7 延迟复制环境搭建和测试的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 5.7 延迟复制

MySQL 5.7并发复制和mysqldump相互阻塞引起的复制延迟

mysql 5.7 主从复制搭建及原理

MySQL 5.7下主从复制延迟解决方案

MySQL 5.7复制延迟之sync_relay_log

MySQL 5.7 并行复制实现原理与调优