利用docker制作mysql8.0 1主3从
拉取镜像
docker pull mysql:8.0.23
启动容器(master)
docker run -d -p 3340:3306 --restart always --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql-master/conf:/etc/mysql -v /data/mysql-master/data:/var/lib/mysql -v /data/mysql-master/log:/var/log/mysql -v /data/mysql-master/mysql-files:/var/lib/mysql-files mysql:8.0.23
启动容器(slave)
docker run -d -p 3341:3306 --restart always --name mysql-slave1 -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql-slave1/conf:/etc/mysql -v /data/mysql-slave1/data:/var/lib/mysql -v /data/mysql-slave1/log:/var/log/mysql -v /data/mysql-slave1/mysql-files:/var/lib/mysql-files mysql:8.0.23
docker run -d -p 3342:3306 --restart always --name mysql-slave2 -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql-slave2/conf:/etc/mysql -v /data/mysql-slave2/data:/var/lib/mysql -v /data/mysql-slave2/log:/var/log/mysql -v /data/mysql-slave2/mysql-files:/var/lib/mysql-files mysql:8.0.23
docker run -d -p 3343:3306 --restart always --name mysql-slave3 -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql-slave3/conf:/etc/mysql -v /data/mysql-slave3/data:/var/lib/mysql -v /data/mysql-slave3/log:/var/log/mysql -v /data/mysql-slave3/mysql-files:/var/lib/mysql-files mysql:8.0.23
配置Master(主)
1.进入mysql-master容器
docker exec -it mysql-master bash
1.1.进入/etc/mysql目录;配置my.cnf配置文件;
注意:vi my.cnf(这条命令在docker里面会报一个错误:bash: vi: command not found) 解决办法就是:就是更新本地vim,从新安装一下vim;分别执行以下命令
apt-get update
apt-get install vim
1.2.开始配置my.cnf文件
[mysqld]
## 同一局域网内注意要唯一
server-id=1
## 开启二进制日志功能
log-bin=mysql-bin
1.3进入mysql
mysql -uroot -p 123456
# 在主节点创建slave账号;密码为:123456
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
# 给账号slave赋予权限
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
# 修改mysql密码认证方式;8.0以前的加密方式mysql_native_password;8.0之后的加密方式:caching_sha2_password
ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
2 重启主节点容器
docker restart mysql-master
配置Slave(从)
1. 进入slave从节点容器
docker exec -it mysql-slave1 bash
1.1.进入/etc/mysql目录;配置my.cnf配置文件;
注意:vi my.cnf(这条命令在docker里面会报一个错误:bash: vi: command not found) 解决办法就是:就是更新本地vim,从新安装一下vim;分别执行以下命令
apt-get update
apt-get install vim
1.2.开始配置my.cnf文件
[mysqld]
## 同一局域网内注意要唯一
server-id=2
## 开启二进制日志功能
log-bin=mysql-bin
1.3进入mysql
# 在主节点创建slave账号;密码为:123456
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
# 给账号slave赋予权限
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
# 修改mysql密码认证方式;8.0以前的加密方式mysql_native_password;8.0之后的加密方式:caching_sha2_password
ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
2.重启从节点容器
docker restart mysql-slave
配置主从同步
1.进入主节点容器,进入mysql数据库,执行如下命令
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1011| | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注意:File和Position字段的值后面将会用到,在后面的操作完成之前,需要保证Master库不能做任何操作,否则将会引起状态变化,File和Position字段的值变化
2.进入从节点容器,进入mysql数据库,执行如下命名
change master to master_host='172.17.0.4', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 1011, master_connect_retry=30;
2.1命令参数说明
master_host :Master的地址,指的是容器的独立ip,可以通过docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master
master_port:Master的端口号,指的是容器的端口号
master_user:用于数据同步的用户
master_password:用于同步的用户的密码
master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒
在Slave 中的mysql终端执行show slave status \G;用于查看主从同步状态。
2.2 开始启动同步
start slave;
2.3 查看slave节点状态
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.4
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 146913
Relay_Log_File: edu-mysql-relay-bin.000004
Relay_Log_Pos: 146226
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 146913
Relay_Log_Space: 146607
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 100
Master_UUID: 280f389e-2154-11ee-ba18-0242ac110004
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
ERROR:
No query specified
2.3 节点同步成功标识
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明:节点slave2和节点slave3 操作步骤都和节点slave1一样,唯一不同就是my.cnf里面的server-id=2的值更改就行
转载自:https://juejin.cn/post/7255334045077078071