一、准备2台物理机器master-1、master-2,目标虚拟VIP。
VIP:192.168.1.139
master-1:192.168.1.17
master-2:192.168.1.20
二、然后分别在2台物理机器master-1、master-2上使用docker-compose安装mysql8,并配置互为主从。
1)配置master-1
cd /opt
mkdir docker_compose_keep_alive_mysql8_both_master
cd docker_compose_keep_alive_mysql8_both_master
mkdir mysql8data
# master_1_init.sql见文件[master-1]
touch master_1_init.sql
# Dockerfile_master_1见文件[master-1]
touch Dockerfile_master_1
# docker-compose.yml见文件[master-1]
touch docker-compose.yml
[master-1]文件 master_1_init.sql
CREATE USER 'repl'@'%' IDENTIFIED WITH caching_sha2_password BY 'repl';
grant replication slave, replication client on *.* to 'repl'@'%';
flush privileges;
[master-1]文件 Dockerfile_master_1
FROM mysql:8.0.31
MAINTAINER "hsj<2356899074@qq.com>"
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY ./master_1_init.sql /docker-entrypoint-initdb.d
[master-1]文件 docker-compose.yml
version: "3.9"
services:
mysql8-master-1:
build:
context: ./
dockerfile: ./Dockerfile_master_1
image: mysql8-master-1
restart: always
container_name: mysql8-master-1
volumes:
- /opt/docker_compose_keep_alive_mysql8_both_master/mysql8data/conf/my.cnf:/etc/mysql/my.cnf
- /opt/docker_compose_keep_alive_mysql8_both_master/mysql8data/data:/var/lib/mysql
- /opt/docker_compose_keep_alive_mysql8_both_master/mysql8data/logs:/var/log/mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_ROOT_HOST=%
privileged: true
extra_hosts:
# master_1容器可以使用的host访问master_2
- mysql8-master-2:192.168.1.20
command: ['--server-id=1',
'--sync_binlog=1',
'--log-bin=mysql8-master-1-bin',
'--binlog-ignore-db=mysql,information_schema,performance_schema,sys',
'--binlog_cache_size=256M',
'--binlog_format=mixed',
'--relay_log=mysql8-master-1-relay',
'--lower_case_table_names=1',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']
2)配置master-2
cd /opt
mkdir docker_compose_keep_alive_mysql8_both_master
cd docker_compose_keep_alive_mysql8_both_master
mkdir mysql8data
# master_2_init.sql见文件[master-2]
touch master_2_init.sql
# Dockerfile_master_2见文件[master-2]
touch Dockerfile_master_2
# docker-compose.yml见文件[master-2]
touch docker-compose.yml
[master-2]文件 master_2_init.sql
CREATE USER 'repl'@'%' IDENTIFIED WITH caching_sha2_password BY 'repl';
grant replication slave, replication client on *.* to 'repl'@'%';
flush privileges;
[master-2]文件 Dockerfile_master_2
FROM mysql:8.0.31
MAINTAINER "hsj<2356899074@qq.com>"
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY ./master_2_init.sql /docker-entrypoint-initdb.d
[master-2]文件 docker-compose.yml
version: "3.9"
services:
mysql8-master-2:
build:
context: ./
dockerfile: ./Dockerfile_master_2
image: mysql8-master-2
restart: always
container_name: mysql8-master-2
volumes:
- /opt/docker_compose_keep_alive_mysql8_both_master/mysql8data/conf/my.cnf:/etc/mysql/my.cnf
- /opt/docker_compose_keep_alive_mysql8_both_master/mysql8data/data:/var/lib/mysql
- /opt/docker_compose_keep_alive_mysql8_both_master/mysql8data/logs:/var/log/mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_ROOT_HOST=%
privileged: true
extra_hosts:
# master_2容器可以使用的host访问master_1
- mysql8-master-1:192.168.1.17
command: ['--server-id=2',
'--sync_binlog=1',
'--log-bin=mysql8-master-2-bin',
'--binlog-ignore-db=mysql,information_schema,performance_schema,sys',
'--binlog_cache_size=256M',
'--binlog_format=mixed',
'--relay_log=mysql8-master-2-relay',
'--lower_case_table_names=1',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']
3)启动master-1、master-2数据库服务
[master-1] 执行:
# mysql8真正启动需要一些时间,navicat链接成功说明启动成功
docker-compose up -d
[master-2] 执行:
# mysql8真正启动需要一些时间,navicat链接成功说明启动成功
docker-compose up -d
4)启动master-1、master-2互为主从
①查看[master-1]作为主节点 的bin-log-file、bin-log-pos:
show master status
配置及其查看[master-1]作为从节点的slave并启动(需要获取master-2主节点的日志):
change master to master_host='192.168.1.20', master_user='repl',master_password='repl',master_port=3306,GET_MASTER_PUBLIC_KEY=1,master_log_file='mysql8-master-2-bin.000003', master_log_pos= 157, master_connect_retry=60;
start slave;
show slave status;
②查看[master-2] 作为主节点 的bin-log-file、bin-log-pos:
show master status
配置及其查看[master-2]作为从节点的slave并启动(需要获取master-1主节点的日志):
change master to master_host='192.168.1.17', master_user='repl',master_password='repl',master_port=3306,GET_MASTER_PUBLIC_KEY=1,master_log_file='mysql8-master-1-bin.000003', master_log_pos= 157, master_connect_retry=60;
start slave;
show slave status;
三、验证
1)[master-1]执行操作:
#创建一个测试库db_test
create database db_test;
#使用db_test
USE db_test;
#创建一张测试表tb_user
create table tb_user (id int,name varchar(20));
#插入一条数据
insert into tb_user(id,name) values (1, 'tomcat');
#查看数据
select * from tb_user;
2)[master-2]执行操作:
#使用db_test
USE db_test;
#查看数据
select * from tb_user;
四、keep-alive 配置VIP
1)master-1、master-2安装依赖包
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
2)master-1、master-2 使用yum安装keepalived
yum install -y keepalived
3)master-1配置keepalived.conf
(MASTER或BACKUP,MASTER会抢占VIP),
本次原则master-1提供服务,master-1宕机VIP漂移到master-2继续服务
cd /etc/keepalived/
#备份原始的
mv keepalived.conf keepalived.conf.bak
#编辑新的,keepalived.conf见文件
vim keepalived.conf
keepalived.conf文件:
global_defs {
router_id MySQL-Master-1
}
vrrp_instance MAIN {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass hsj
}
virtual_ipaddress {
192.168.1.139
}
}
4)master-2配置keepalived.conf
cd /etc/keepalived/
#备份原始的
mv keepalived.conf keepalived.conf.bak
#编辑新的,keepalived.conf见文件
vim keepalived.conf
keepalived.conf文件:
global_defs {
router_id MySQL-Master-2
}
vrrp_instance MAIN {
state BACKUP
interface ens33
virtual_router_id 1
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass hsj
}
virtual_ipaddress {
192.168.1.139
}
}
5)master-1、master-2同时启动keepalived服务
systemctl start keepalived
————————————————————————————
systemctl start keepalived 启动keepalived服务
systemctl stop keepalived 停止keepalived服务
systemctl enable keepalived 设置keepalived服务开机自启动
systemctl disable keepalived 禁止keepalived服务开机自启动
6)测试master-1宕机ip漂移
关闭master-1宿主机实例,模拟master-1宕机。
通过VIP:192.168.1.139 链接数据库操作任然正常,进行操作insert:
insert into tb_user(id,name) values (9, '听妈妈的话');
7)恢复master-1
重启master-1宿主机实例,master-1、master-2互为主从依然正常,听妈妈的话
这条数据两个库分别存储了。
8)测试master-2宕机ip漂移
关闭master-2宿主机实例,模拟master-2宕机。
通过VIP:192.168.1.139 链接数据库操作任然正常,进行操作insert:
insert into tb_user(id,name) values (10, '听爸爸的话');
9)恢复master-2
重启master-2宿主机实例,master-1、master-2互为主从依然正常,听爸爸的话
这条数据两个库分别存储了。
结语:
1、当sync_binlog=0的时候,表示每次提交事务都只write,不fsync
2、当sync_binlog=1的时候,表示每次提交事务都执行fsync
3、当sync_binlog=N的时候,表示每次提交事务都write,但积累N个事务后才fsync。
一般在公司的大部分应用场景中,我们建议将此参数的值设置为1,因为这样的话能够保证数据的安全性,但是如果出现主从复制的延迟问题,可以考虑将此值设置为100~1000中的某个数值,非常不建议设置为0,因为设置为0的时候没有办法控制丢失日志的数据量,但是如果是对安全性要求比较高的业务系统,这个参数产生的意义就不是那么大了。
-- 查看并行的slave的线程的个数,默认是0.表示单线程
show global variables like 'slave_parallel_workers';
-- 根据实际情况保证开启多少线程
set global slave_parallel_workers = 4;
-- 设置并发复制的方式,默认是一个线程处理一个库,值为database
show global variables like '%slave_parallel_type%';
-- 停止slave
stop slave;
-- 设置属性值
set global slave_parallel_type='logical_check';
-- 开启slave
start slave
-- 查看线程数
show full processlist;