docker | 搭建mysql 主从复制

2020-08-13  本文已影响0人  炒面Z

docker 安装 mysql

mysql 和 mysql_slave 主从库

#批量创建文件夹
mkdir -p /opt/mysql/data /opt/mysql/logs /opt/mysql/conf 
mkdir -p /opt/mysql_slave/data /opt/mysql_slave/logs /opt/mysql_slave/conf 
#启动主库
docker run -p 3306:3306 --name mysql  \
-v /opt/mysql/conf:/etc/mysql/conf.d  \
-v /opt/mysql/logs:/logs  \
-v /opt/mysql/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

#启动从库
docker run -p 3307:3306 --name mysql_slave  \
-v /opt/mysql_slave/conf:/etc/mysql/conf.d  \
-v /opt/mysql_slave/logs:/logs  \
-v /opt/mysql_slave/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

配置 mysql的my.cnf配置文件

在 主库的/opt/mysql/conf 和 从库 /opt/mysql_slave/conf 目录下分别创建my.cnf文件

主库的配置文件

lower_case_table_names=1
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION


# binlog 配置
log-bin =mysql-bin.log
#expire-logs-days = 14
#max-binlog-size = 500M
server-id = 1

#免密码登录
#skip-grant-tables

从库的配置文件

lower_case_table_names=1
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION


# binlog 配置
log-bin =mysql-bin.log
#expire-logs-days = 14
#max-binlog-size = 500M
server-id = 2

#免密码登录
#skip-grant-tables

配置 主/从 数据库,root 可以通过任何客户端连接

1.进入容器
root@shu:/opt/mysql/conf# docker exec -it mysql bash

2.连接 MySQL

root@5f00ed3ef349:/# mysql -u root -p123456

3.修改 root 可以通过任何客户端连接
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

配置 MySQL 主从复制

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     154  |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> stop slave;


mysql>  change master to
master_host='<主库的IP地址>',
master_user='root',
master_log_file='mysql-bin.000003',
master_log_pos=154,
master_port=3306,
master_password='123456';


mysql> start slave;
mysql> show slave status \G;

执行 show slave status \G; 查看主从复制状态

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 121.43.236.166
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 61282
               Relay_Log_File: 5f00ed3ef349-relay-bin.000002
                Relay_Log_Pos: 13904
        Relay_Master_Log_File: mysql-bin.000011
             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: 61282
              Relay_Log_Space: 14118
              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: 1
                  Master_UUID: 8d67e65d-35a8-11ea-9f66-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)

ERROR: 
No query specified

如果发现下面两个指标项都是YES表示配置成功,进行测试即可
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

上一篇 下一篇

猜你喜欢

热点阅读