目录

 

1、拉取mysql 。

2、创建容器并运行:

  创建主数据库:

创建从数据库:


1、拉取mysql 。

我这拉取的是mysql 5.7

docker pull mysql:5.7

2、创建容器并运行:

  创建主数据库:

docker run -p 3307:3306 --name mysql-master \
         -v /work/mysql-master/data:/var/lib/mysql \
         -v /work/mysql-master/conf:/etc/mysql/conf.d \
         -v /work/mysql-master/logs:/var/log/mysql \
         -e MYSQL_ROOT_PASSWORD=123456 \
         -d 9cfcce23593a

命令解释:
 -p : 端口映射 即把宿主机的3307端口映射到容器的3306端口
 --name 容器名称
 -v 挂载文件 这里主要吧 配置文件、数据、日志挂载在宿主机上。因为容器内的数据会随着容器的删除而删除,如果挂载在宿主机上。容器删除,数据还在。
 -e 指定密码
 -d 容器后台运行,并返回容器id

 

 然后配置mysql配置文件,因为我们已经把配置文件映射到/work/mysql-master/conf目录。那么我们直接在这里修改即可:

创建文件my.cnf内容如下:

[mysqld]
server-id=1 ## 唯一server id
log-bin=mysql-bin ## 开启二进制日志
lower_case_table_names =1
character-set-server=utf8
sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

然后重启容器。容器启动会 ,进入容器: 

docker exec -it 容器id /bin/bash

登录mysql 密码为创建容器时指定密码。然后执行:

grant replication slave on *.* to 'master'@'%' identified by '123456';
flush privileges;

 

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

这里需要记住这个File和Position属性的值,因为下面在配置slave的时候需要用到。 

创建从数据库:

 基本和创建主数据库一致.

docker run -p 3308:3306
     --name mysql-slave1
     -v /work/mysql-slave1/data:/var/lib/mysql
     -v /work/mysql-slave1/conf:/etc/mysql/conf.d
     -v /work/mysql-slave1/logs:/var/log/mysql
     --link mysql-master:master
     -e MYSQL_ROOT_PASSWORD=123456
     -d 9cfcce23593a

区别:
  --link --link参数,其后指定了当前容器所要连接的容器,mysql-master表示所要连接的容器的名称,master表示为该容器起的一个别名,通俗来讲,就是slave容器通过这两个名称都可以访问到master容器。这么做的原因在于,如果master与slave不在同一个docker network中,那么这两个容器相互之间是没法访问的。

 

配置mysql文件:

[mysqld]
server-id=2
log-bin=mysql-slave1-bin
relay_log=slave1-mysql-relay-bin
lower_case_table_names =1
character-set-server=utf8
sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

然后重启容器,进入容器,连接主数据库。执行以下命令: 

change master to master_host='master', master_user='master', master_password='123456', master_port=3306, master_log_file='mysql-bin.00002', master_log_pos=591, master_connect_retry=30;

 

这里需要注意:master_port 是指容器mysql的端口,并不是映射端口。

然后:

start slave;

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

 

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 878
               Relay_Log_File: slave2-mysql-relay-bin.000004
                Relay_Log_Pos: 1091
        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: 878
              Relay_Log_Space: 2677
              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: 41b38f79-d952-11ea-b298-0242ac110003
             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_RunningSlave_SQL_Running都为true,则表示复制是正常进行的。

如果报错:

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file n

 那么执行:

stop slave;
reset slave;
start slave;

 然后执行 :

show slave status\G;

测试

最后在主数据库中新增一条数据可以看到,在从数据库中也有相同的一条数据,即说明成功开启主从复制!