# 修改配置文件
# mysql-master配置文件修改,因为我们做了docker文件卷处理,所以我们直接在宿主机/usr/local/docker/mysql/master/conf下操作即可
[root@localhost ~]# cd /usr/local/docker/mysql/master/conf/
[root@localhost conf]# vi my.cnf
# 在my.cnf里面输入如下信息
[mysqld]
## 同一局域网内注意要唯一
server-id=100
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
# 进入mysql-master容器里面重启mysql服务
# docker exec -it mysql-master /bin/bash 进入mysql-master容器
# service mysql restart重启mysql服务器
# 这里有一点要注意,重启mysql服务之后,mysql-master容器被关掉了,所以我们需要docker start mysql-master在启动下容器
[root@localhost /]# docker exec -it mysql-master /bin/bash
root@cdcc14b2b2a2:/# service mysql restart
[root@localhost /]# docker start mysql-master
# 再次进入mysql-master容器授予slave用户 REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据[slave用户是用来做数据同步的]
# 1. CREATE USER slave@% IDENTIFIED BY 123456;
# 2. GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slave@%;
# 3. show master status; 查看主库状态,请记住File和Position对应的值
[root@localhost /]# docker exec -it mysql-master /bin/bash
root@205cd234c29b:/# mysql -u root -p
Enter password:
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> CREATE USER slave@% IDENTIFIED BY 123456;
Query OK, 0 rows affected (0.04 sec)
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slave@%;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+——————+———-+————–+——————+——————-+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+——————+———-+————–+——————+——————-+
| mysql-bin.000002 | 617 | | | |
+——————+———-+————–+——————+——————-+
1 row in set (0.01 sec)
mysql>
# mysql-slave配置文件修改,因为我们做了docker文件卷处理,所以我们直接在宿主机/usr/local/docker/mysql/slave/conf下操作即可
[root@localhost conf]# cd /usr/local/docker/mysql/slave/conf/
[root@localhost conf]# vi my.cnf
# 在my.cnf里面输入如下信息
[mysqld]
## 设置server_id,注意要唯一
server-id=101
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
# 进入mysql-slave容器里面重启mysql服务
# docker exec -it mysql-slave /bin/bash 进入mysql-master容器
# service mysql restart重启mysql服务器
# 这里又一点要注意,重启mysql服务之后,mysql-slave容器被关掉了,所以我们需要docker start mysql-slave在启动下容器
[root@localhost /]# docker exec -it mysql-slave /bin/bash
root@205cd234c29b:/# service mysql restart
[root@localhost /]# docker start mysql-slave
# 再次进入mysql-slave容器,连上mysql-slave数据库
[root@localhost /]# docker exec -it mysql-slave /bin/bash
[root@localhost conf]# docker exec -it mysql-slave /bin/bash
root@205cd234c29b:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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> change master to master_host=172.88.0.11, master_port=3306, master_user=slave,master_password=123456,master_log_file=mysql-bin.000002, master_log_pos=617;
Query OK, 0 rows affected, 2 warnings (0.06 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.88.0.11
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 617
Relay_Log_File: edu-mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
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: 617
Relay_Log_Space: 531
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: 46cc703d-5b0e-11ec-a43c-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: