代码改变世界

Docker搭建 MySQL 主从复制

2018-07-12 15:34  掸尘  阅读(805)  评论(1编辑  收藏  举报

  为什么选 Docker

搭建主从复制需要两个以上的MySQL, 使用 Docker 非常方便。如果以前没用过,找个简单的文档看看,熟悉一下命令。

  搭建过程

1.下载镜像

docker pull mysql:5.7.21

2.创建服务器

docker run --name mysql_master -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21

//通过镜像 mysql:5.7.21 启动一个名为 mysql_master 的 MySQL 服务器,端口号是3306,映射的宿主机端口号是3310,root 账号密码是123456

3.创建Slave服务器

docker run --name mysql_slave  -p 3311:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21

通过 docker container ls -a  可以看到创建成功

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d2298154e6d4        mysql:5.7.21        "docker-entrypoint.s…"   3 hours ago         Up 2 hours          0.0.0.0:3311->3306/tcp   mysql_slave
b8d7e74b2d31        mysql:5.7.21        "docker-entrypoint.s…"   3 hours ago         Up About an hour    0.0.0.0:3310->3306/tcp   mysql_master

4.查看IP

docker inspect --format='{{.NetworkSettings.IPAddress}}' b8d
//172.17.0.2
docker inspect --format='{{.NetworkSettings.IPAddress}}' d22
//172.17.0.3

master IP : 172.17.0.2

slave   IP : 172.17.0.3 

  MySQL 配置

1. Master 配置

//进入Master服务器
docker exec -it b8d /bin/bash
//进的入配置
cd /etc/mysql
//修改配置文件--my.cnf增加下面两行
server_id=100 
log-bin=mysql-bin
//配置完成后重启
service mysql restart
//如果容器停止了,要从新启动进入,docker container start b8d
//再次进入容器
mysql -uroot -p
//执行下面命令进行复制授权
CREATE USER 'slave'@'%' IDENTIFIED BY '12345678';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';  
//检查命令
show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     370 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

//File Position 两个数据后面配置 Slave 会用到。

2. Slave 配置

//进入Slave 容器
docker exec -it d22  /bin/bash
//配置mysql,增加下面
server_id=101
log-bin=mysql-bin
//重启进入执行
change master to master_host='172.17.0.2', master_user='slave', master_password='12345678', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos=370, master_connect_retry=30;
//启动
start slave;
//查看主从同步状态
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.000002
          Read_Master_Log_Pos: 370
               Relay_Log_File: d2298154e6d4-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: 370
              Relay_Log_Space: 534
              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: 2cf67843-8583-11e8-bfa5-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)

Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes  表明配置成功。 配置 master_log_file='mysql-bin.000002',  master_log_pos=370, 这两个参数要注意 和 show

master status 输出的值保持一致,不然会出现同步错误。