docker run --name mysql-master -e MYSQL_ROOT_PASSWORD=root -d mysql:8
docker run --name mysql-salve -e MYSQL_ROOT_PASSWORD=root -d mysql:8
docker ps
[root@iZbp1dpxwusntf023pd9rxZ slave]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19b551c223d0 mysql:8 "docker-entrypoint.s…" About an hour ago Up 25 minutes 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp, 0.0.0.0:33070->33060/tcp, :::33070->33060/tcp mysql-slave
3ad9ac61b3b7 mysql:8 "docker-entrypoint.s…" About an hour ago Up 29 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 0.0.0.0:33060->33060/tcp, :::33060->33060/tcp mysql-master
之前配置的端口映射,感觉也不需要所以前面命令就没加-p
使用docker inspect 容器名称 查看下两个容器的ip,由于创建两个容器时没有制定网络,默认两个容器应该是互通的
docker inspect mysql-master
#master网络信息如下:
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
docker inspect mysql-slave
#slave网络信息如下:
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
master的ip为172.17.0.2,slave的IP为172.17.0.3
接下来在其中一个容器内部使用mysql连接另一个容器试试,使用docker exec进入制定容器,比如从slave中使用mysql命令连接master。
docker exec -it mysql-slave bash
#进入slave后使用mysql命令连接master
mysql -uroot -proot -h 172.17.0.2
#操作记录
bash-4.4# mysql -uroot -proot -h 172.17.0.2
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 10
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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>
配置文件的默认位置在容器中/etc/my.cnf,开始我按dockerhub中mysql页面中将/etc/mysql/conf.d这个目录挂载出来了,然后再宿主机的挂载目录中创建my.cnf配置文件写上配置信息,重启容器配置没有生效,暂时没有寻找原因,也许是我挂载的命令或者配置的方式不对。所以后续我就直接把容器内的/etc/my.cnf直接拷贝出来了,修改完了再拷贝回容器内,才正常生效了。
docker cp mysql-master:/etc/my.cnf ./
#在[mysqld]下添加下列配置项
[mysqld]
server_id=1
#binlog文件名
log-bin=mysql-bin
#忽略库
binlog-ignore-db=mysql
#同步库,不要先创建
binlog-do-db=syncdb
#binlog格式
binlog_format=STATEMENT
docker cp my.cnf mysql-master:/etc/
docker restart mysql-master
docker cp mysql-slave:/etc/my.cnf ./
#在[mysqld]下添加下列配置项
[mysqld]
server_id=2
#中继日志名
relay_log=mysql-relay
docker cp my.cnf mysql-salve:/etc/
docker restart mysql-slave
创建一个“slave”的用户(密码:root)用来同步
先进入master内部
docker exec -it mysql-master bash
#登录mysql
mysql -uroot -proot
#创建用户
create user 'slave'@'%' identified by 'root';
#授权
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
#mysql8需要执行额外的操作
alter user 'slave'@'%' identified with mysql_native_password by 'root';
flush privileges;
操作记录如下
mysql> create user 'slave'@'%' identified by 'root';
Query OK, 0 rows affected (0.15 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'slave'@'%';
+-----------------------------------------------+
| Grants for slave@% |
+-----------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `slave`@`%` |
+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> alter user 'slave'@'%' identified with mysql_native_password by 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
操作完成后查看master的状态,注意状态信息是否和前面配置文件一致,注意File和position列,后续需要在salve中设置
#show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1142 | syncdb | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
进入slave容器,容器内部登录MySQL
docker exec -it mysql-slave bash
mysql -uroot -proot
执行配置master的相关命令,注意master_host是master的ip,master_user和master_password是在master中创建的用户,master_log_file和master_log_pos是master中使用show master status命令查看的内容
change master to master_host='172.17.0.2',master_user='slave',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=1142;
配置完成后启动slave
#mysql下
start slave;
查看slave状态
#mysql下
show slave status\G;
操作截图:
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 172.17.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1142
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
如果
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
配置成功。
当前master和slave中都没有配置文件中的syncdb。
master:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
slave:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
在master中创建syncdb
mysql> create database syncdb;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| syncdb |
| sys |
+--------------------+
5 rows in set (0.01 sec)
在salve中查看,也自动创建了。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| syncdb |
| sys |
+--------------------+
5 rows in set (0.00 sec)
接下来在master创建一些表,插入一些数据测试下
master:
mysql> use syncdb;
Database changed
mysql> create table user(id int,name varchar(10),age int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into user values(1,'tom',20),(2,'jack',22);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>
在slave中查询下
mysql> use syncdb;
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> select * from user;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | tom | 20 |
| 2 | jack | 22 |
+------+------+------+
2 rows in set (0.00 sec)
mysql>
ok,至此在docker中配置mysql主从复制完成!
停止主从复制和重置主从连接
stop slave;
reset master;