docker部署MySQL主从同步 原创
docker部署MySQL主从同步
下载MySQL5.7镜像
[root@localhost ~]# docker pull mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 years ago 448MB
一、新建主服务器容器实例 3307端口
格式:
docker run -d -p 3307:3306 --name=mysql-master \
--privileged=true \
-v /docker-volume/mysql-master/log:/var/log/mysql \
-v /docker-volume/mysql-master/data:/var/lib/mysql \
-v /docker-volume/mysql-master/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7d217996fc2 mysql:5.7 "docker-entrypoint.s…" 2 seconds ago Up 1 second 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
-d 后台运行
-p 指定端口映射 宿主机端口:容器内端口
--name 容器名称
--privileged=true 特权
-v 容器数据卷
-e 环境,这里设置的是数据库密码
1. 修改MySQL配置文件
进入数据卷的目录
[root@localhost ~]# cd /docker-volume/mysql-master/conf/
[root@localhost conf]# ls
[root@localhost conf]# vi my.cnf
[root@localhost conf]# cat my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
重启MySQL容器,让配置文件生效
[root@localhost conf]# docker restart mysql-master
mysql-master
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7d217996fc2 mysql:5.7 "docker-entrypoint.s…" 6 minutes ago Up 44 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
2. 进入容器实例,登录MySQL
[root@localhost conf]# docker exec -it mysql-master /bin/bash
root@e7d217996fc2:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3. master容器内创建数据同步的用户
1. 创建slave用户,密码123456
create user 'slave'@'%' identified by '123456';
2. 授权salve用户做相应的读写权限
grant replication slave,replication client on *.* to 'slave'@'%';
二、新建从服务器实例 3308端口
docker run -d -p 3308:3306 --name=mysql-slave \
--privileged=true \
-v /docker-volume/mysql-slave/log:/var/log/mysql \
-v /docker-volume/mysql-slave/data:/var/lib/mysql \
-v /docker-volume/mysql-slave/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0d3b830bf96 mysql:5.7 "docker-entrypoint.s…" 2 seconds ago Up 1 second 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql-slave
e7d217996fc2 mysql:5.7 "docker-entrypoint.s…" 36 minutes ago Up 30 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
-d 后台运行
-p 指定端口映射 宿主机端口:容器内端口
--name 容器名称
--privileged=true 特权
-v 容器数据卷
-e 环境,这里设置的是数据库密码
1. 修改配置文件
进入数据卷目录添加my.cnf配置文件
[root@localhost ~]# cd /docker-volume/
[root@localhost docker-volume]# ls
mysql-master mysql-slave
[root@localhost docker-volume]# cd mysql-slave/conf/
[root@localhost conf]# ls
[root@localhost conf]# vi my.cnf
[root@localhost conf]# cat my.cnf
[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-slave1-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
## relay_log配置中继日志
relay_log=mall-mysql-relay-bin
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1
## slave设置为只读(具有super权限的用户除外)
read_only=1
重启docker-slave容器让配置文件生效
[root@localhost conf]# docker restart mysql-slave
mysql-slave
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0d3b830bf96 mysql:5.7 "docker-entrypoint.s…" 6 minutes ago Up 1 second 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql-slave
e7d217996fc2 mysql:5.7 "docker-entrypoint.s…" 42 minutes ago Up 37 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
三、配置主从
master节点
[root@localhost conf]# docker exec -it mysql-master /bin/bash
root@e7d217996fc2:/# mysql -uroot -p123456
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mall-mysql-bin.000001 | 778 | | mysql | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
slave节点
[root@localhost conf]# docker exec -it mysql-slave /bin/bash
root@c0d3b830bf96:/# mysql -uroot -p123456
mysql> change master to master_host='192.168.174.200', master_user='slave', master_password='123456',
master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=778, master_connect_retry=3;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
解析:
change master to master_host='宿主机ip地址',
master_user='用户', master_password='密码',
master_port=master的端口,
master_log_file='master节点输入show master status查看FIle字段', master_log_pos=778,
master_connect_retry=3;
开启主从复制并查看
slave节点
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.174.200
Master_User: slave
Master_Port: 3307
Connect_Retry: 30
Master_Log_File: mall-mysql-bin.000001
Read_Master_Log_Pos: 778
Relay_Log_File: mall-mysql-relay-bin.000002
Relay_Log_Pos: 325
Relay_Master_Log_File: mall-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: 778
Relay_Log_Space: 537
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: 101
Master_UUID: 48d3a1b9-0cd2-11ef-a449-0242ac110002
Master_Info_File: /var/lib/mysql/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:
1 row in set (0.00 sec)
四、验证
master节点
创建数据库、创建数据表、插入数据
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table demo(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| demo |
+----------------+
1 row in set (0.00 sec)
mysql> insert into demo values (1,'zhangsan');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
ERROR 1146 (42S02): Table 'test.test' doesn't exist
mysql> select * from demo;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)
slave节点
查询数据,发现同步过来了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| demo |
+----------------+
1 row in set (0.00 sec)
mysql> select * from demo;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了