安装Docker
yum update
yum install docker -y
systemctl start docker
安装MySQL(一主一从)
主节点
配置
[mysqld]
server-id=1
log-bin=/var/lib/mysql/binlog
binlog-do-db=test_db
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
运行
docker run -d --name mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v /opt/mysql/data/3306:/var/lib/mysql \
-v /opt/mysql/conf/3306/my.cnf:/etc/mysql/my.cnf \
mysql:5.7
从节点
配置
[mysqld]
server-id=2
replicate-do-db=test_db
运行
docker run -d --name mysql-slave-1 \
-p 3307:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v /opt/mysql/data/3307:/var/lib/mysql \
-v /opt/mysql/conf/3307/my.cnf:/etc/mysql/my.cnf \
mysql:5.7
查看运行情况
docker ps
docker logs mysql
docker logs mysql-slave-1
安装mysql-client
rpm -ivh http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
yum install mysql-community-client
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
mysql -V
yum -y install mysql-community-client
mysql -h 127.0.0.1 -P 3307 -u root -p
配置Slave
STOP SLAVE;
CHANGE MASTER TO
MASTER_HOST='172.17.0.2',
MASTER_USER='root',
MASTER_PASSWORD='root',
MASTER_LOG_FILE='binlog.000001',
MASTER_LOG_POS=1;
START SLAVE;
查看状态,排查错误
SELECT @@server_id as SERVER_ID;
#主节点状态
SHOW MASTER STATUS;
#主节点binlog事件
SHOW BINLOG EVENTS;
#从节点状态,Last_Error字段错误信息
SHOW SLAVE STATUS;
#从节点执行非严格,跳过部分错误,如删除不存在的表,避免卡主同步,方便调试
set global sql_slave_skip_counter = 1;
start slave;