基于Docker的Mysql主从复制搭建

参考链接

https://www.cnblogs.com/songwenjie/p/9371422.html

1. 首先拉取docker镜像,我们这里使用5.7版本的mysql

docker pull mysql:5.7

2.然后使用此镜像启动容器,这里需要分别启动主从两个容器

Master(主):

docker run -p 3339:3306  --restart always --name mysql-master -e MYSQL_ROOT_PASSWORD=vsgame12366321 -d mysql:5.7

Slave(从):

docker run -p 3340:3306 --restart always --name mysql-slave1 -e MYSQL_ROOT_PASSWORD=vsgame12366321 -d mysql:5.7

docker run -p 3341:3306 --restart always --name mysql-slave2 -e MYSQL_ROOT_PASSWORD=vsgame12366321 -d mysql:5.7

docker run -p 3342:3306  --restart always --name mysql-slave3 -e MYSQL_ROOT_PASSWORD=vsgame12366321 -d mysql:5.7

配置Master(主)

1.进入mysql-master容器

docker -it mysql-master bash

2.进入mysql目录;配置my.cnf配置文件;

cd etc/mysql

vi my.cnf(这条命令在docker里面会报一个错误:bash: vi: command not found)

2.1 解决办法;就是更新本地vim,从新安装一下vim

apt-get update
apt-get install vim

2.2 mysql-master的my.cnf配置文件

[mysqld]
## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin

2.3 在Master数据库创建数据同步用户,授予用户 slave REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据。

CREATE USER 'slave1'@'%' IDENTIFIED BY 'vsgame12366321';

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave1'@'%';

2.4 使配置生效

docker restart mysql-master

配置Slave(从)

跟mysql-mstar 1和2,2.1步骤一致

2.2 配置mysql-salve的my.cnf配置文件

[mysqld]
## 设置server_id,注意要唯一
server-id=101  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin   
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin

使配置生效

docker restart mysql-slave

进入mysql-master容器,查看是否实现了主从复制

1.进入容器:docker exec -it mysql-master bash
2.连接mysql : mysql -uroot -p 123456
3.查看:show master status

2.1 配置成功如下:

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      617 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注意:File和Position字段的值后面将会用到,在后面的操作完成之前,需要保证Master库不能做任何操作,否则将会引起状态变化,File和Position字段的值变化

2.2 在Slave 中进入 mysql,执行

change master to master_host='172.17.0.2', master_user='slave1', master_password='vsgame12366321', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos= 154, master_connect_retry=30;

2.2.1 命令说明:

master_host :Master的地址,指的是容器的独立ip,可以通过docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称|容器id查询容器的ip

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;用于查看主从同步状态。

容器ip

mysql-master: 172.17.0.2
mysql-slave1: 172.17.0.3
mysql-slave2: 172.17.0.3
mysql-slave3: 172.17.0.6

2.3 在Slave 中的mysql终端执行show slave status \G;用于查看主从同步状态

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 617
               Relay_Log_File: edu-mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 9147af13-961e-11e9-b793-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)

2.4 使用start slave开启主从复制过程,然后再次查询主从同步状态show slave status \G;

1. 运行命令:start slave
2. show slave status \G;
mysql> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.0.2
                      Master_User: slave
                      Master_Port: 3306
                    Connect_Retry: 30
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 617
                   Relay_Log_File: edu-mysql-relay-bin.000002
                    Relay_Log_Pos: 320
            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: 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: 9147af13-961e-11e9-b793-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)

测试主从模式是否成功

在主库中新建数据库为testDb,刷新从库是否也有testDb;如果有,这成功,否则失败

你可能感兴趣的:(基于Docker的Mysql主从复制搭建)