docker pull mysql:5.7
由于我们需要做数据持久化,所以先在本地创建对应四个节点的相应文件夹
#conf文件夹用于存放配置文件
mkdir -p /root/mysql/mysql-master/conf
#data文件夹用于存放mysql数据文件
mkdir -p /root/mysql/mysql-master/data
#log文件夹用于存放mysql日志文件
mkdir -p /root/mysql/mysql-master/log
mkdir -p /root/mysql/mysql-master1/conf
mkdir -p /root/mysql/mysql-master1/data
mkdir -p /root/mysql/mysql-master1/log
mkdir -p /root/mysql/mysql-slave/conf
mkdir -p /root/mysql/mysql-slave/data
mkdir -p /root/mysql/mysql-slave/log
mkdir -p /root/mysql/mysql-slave1/conf
mkdir -p /root/mysql/mysql-slave1/data
mkdir -p /root/mysql/mysql-slave1/log
在/root/mysql/mysql-master/conf下新建主节点1的my.cnf文件
cd /root/mysql/mysql-master/conf
vi my.cnf
新增如下内容
[client]
default_character_set=utf8 #设置字符集为Utf-8
[mysql]
default_character_set=utf8
[mysqld]
character_set_server=utf8
server_id=01 #当前服务的ID,集群中ID不能相同
binlog-ignore-db=mysql #设置Mysql库不进行主从复制
log-bin=master-log-bin #开启二进制日志,设置日志文件名称
binlog_cache_size=1M #设置binlog缓存大小为1M
binlog_format=mixed #设置同步模式为混合
expire_logs_days=7 #设置二进制日志过期清理时间为7天
slave_skip_errors=1062 #设置在主从复制时跳过此类报错
在/root/mysql/mysql-master1/conf下新建主节点2的my.cnf文件
cd /root/mysql/mysql-master1/conf
vi my.cnf
新增如下内容
[client]
default_character_set=utf8
[mysql]
default_character_set=utf8
[mysqld]
character_set_server=utf8
server_id=02
binlog-ignore-db=mysql
log-bin=master1-log-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
在/root/mysql/mysql-slave/conf下新建从节点1的my.cnf文件
cd /root/mysql/mysql-slave/conf
vi my.cnf
新增如下内容
[client]
default_character_set=utf8 #设置字符集为Utf-8
[mysql]
default_character_set=utf8
[mysqld]
character_set_server=utf8
server_id=03
binlog-ignore-db=mysql
log-bin=slave-log-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=relay-log-bin
log_slave_updates=1 #log_slave_updates表示slave将复制时间写进自己的二进制日志
read_only=1
在/root/mysql/mysql-slave1/conf下新建从节点2的my.cnf文件
cd /root/mysql/mysql-slave1/conf
vi my.cnf
新增如下内容
[client]
default_character_set=utf8 #设置字符集为Utf-8
[mysql]
default_character_set=utf8
[mysqld]
character_set_server=utf8
server_id=04
binlog-ignore-db=mysql
log-bin=slave1-log-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=relay1-log-bin
log_slave_updates=1 #log_slave_updates表示slave将复制时间写进自己的二进制日志
read_only=1
主节点1
docker run --name mysqlm \
-e MYSQL_ROOT_PASSWORD=root \
-v /root/mysql_master/data:/var/lib/mysql \
-v /root/mysql_master/log:/var/log/mysql \
-v /root/mysql_master/conf:/etc/mysql/conf.d \
-dp 3306:3306 mysql:5.7
主节点2
docker run --name mysqlm1 \
-e MYSQL_ROOT_PASSWORD=root \
-v /root/mysql_master1/data:/var/lib/mysql \
-v /root/mysql_master1/log:/var/log/mysql \
-v /root/mysql_master1/conf:/etc/mysql/conf.d \
-dp 3307:3306 mysql:5.7
从节点1
docker run --name mysqls \
-e MYSQL_ROOT_PASSWORD=root \
-v /root/mysql_slave/data:/var/lib/mysql \
-v /root/mysql_slave/log:/var/log/mysql \
-v /root/mysql_slave/conf:/etc/mysql/conf.d \
-dp 3308:3306 mysql:5.7
从节点2
docker run --name mysqls1 \
-e MYSQL_ROOT_PASSWORD=root \
-v /root/mysql_slave1/data:/var/lib/mysql \
-v /root/mysql_slave1/log:/var/log/mysql \
-v /root/mysql_slave1/conf:/etc/mysql/conf.d \
-dp 3309:3306 mysql:5.7
启动成功后docker ps -a 查看进程是否启动
如上4个节点的mysql服务全部启动成功
在master中配置slave用户给从节点使用
docker exec -it mysqlm /bin/bash
---
mysql -uroot -proot
登录Mysql后执行
create user 'slave'@'%' identified by 'root';
grant replication slave,replication client on *.* to 'slave'@'%';
然后查看主节点1 的日志文件名称和偏移量用于后面从节点配置使用
show master status;
在slave中配置开启主从复制
docker exec -it mysqls /bin/bash
---
mysql -uroot -proot
#登录mysql后执行
change master to master_host='120.46.95.3',master_user='slave',master_password='root',master_port=3306,master_log_file='master-log-bin.000004',master_log_pos=770,master_connect_retry=30,master_retry_count=3;
#开启主从复制
start slave;
#查看slave状态;
show slave status\G;
docker exec -it mysqlm1 /bin/bash
---
mysql -uroot -proot
#登录mysql后执行
change master to master_host='120.46.95.3',master_user='slave',master_password='root',master_port=3306,master_log_file='master-log-bin.000004',master_log_pos=770,master_connect_retry=30,master_retry_count=3;
#开启主从复制
start slave;
#查看slave状态;
show slave status\G;
当出现如上内容时master1已配置成为master的从节点。
下面在master1中设置slave用户用于master作为从节点进行连接
create user 'slave'@'%' identified by 'root';
grant replication slave,replication client on *.* to 'slave'@'%';
然后查看主节点2 的日志文件名称和偏移量用于后面从节点配置使用
show master status;
docker exec -it mysqlm /bin/bash
---
mysql -uroot -proot
#登录mysql后执行
change master to master_host='120.46.95.3',master_user='slave',master_password='root',master_port=3307,master_log_file='master1-log-bin.000002',master_log_pos=768,master_connect_retry=30,master_retry_count=3;
#开启主从复制
start slave;
#查看slave状态;
show slave status\G;
出现如上配置说明master已配置为master1的从节点。
docker exec -it mysqls1 /bin/bash
---
mysql -uroot -proot
#登录mysql后执行
change master to master_host='120.46.95.3',master_user='slave',master_password='root',master_port=3307,master_log_file='master1-log-bin.000002',master_log_pos=768,master_connect_retry=30,master_retry_count=3;
#开启主从复制
start slave;
#查看slave状态;
show slave status\G;