技术Java技术升华

基于docker配置mysql主从同步

2018-03-04  本文已影响66人  yangziwen

本文参考了http://www.sunhao.win/articles/mysql-replication.html,但对文中的一些步骤进行了优化。

1. 下载mysql镜像

docker pull mysql

可配置如下国内镜像服务器加速下载过程

https://docker.mirrors.ustc.edu.cn/
https://mirror.ccs.tencentyun.com/

2. 启动容器

2.1 创建master和slave两个目录,并在其中分别创建以下目录
2.2 在master/conf中添加my.cnf配置文件,内容如下
[mysqld]
log-bin=mysql-bin    // [必须]启用二进制日志
server-id=1          // [必须]服务器唯一ID,默认是1
replicate-ignore-db=mysql
2.3 在slave/conf中添加my.cnf配置文件,内容如下
[mysqld]
log-bin=mysql-bin    // [必须]启用二进制日志
server-id=2          // [必须]服务器唯一ID,默认是1
replicate-ignore-db=mysql
2.4 进入master目录,启动master的容器
cd master
master_path=`pwd`
docker run --name mysql-master \
-p 3307:3306 \
-v ${master_path}/conf:/etc/mysql/conf.d \
-v ${master_path}/datadir:/var/lib/mysql \
-v ${master_path}/logdir:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=1234 \
-d mysql:latest

执行docker ps -f 'name=mysql-master'查看容器运行状态如下

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
fbb8d410e0e4        mysql:latest        "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:3307->3306/tcp   mysql-master

执行以下命令登录mysql-master,验证mysql可用

mysql -u root -p1234 -h 127.0.0.1 -P 3307
2.5 进入slave目录,启动slave的容器
cd slave
slave_path=`pwd`
docker run --name mysql-slave \
-p 3308:3306 \
--link=mysql-master:mysql-master \
-v ${slave_path}/conf:/etc/mysql/conf.d \
-v ${slave_path}/datadir:/var/lib/mysql \
-v /${slave_path}/logdir:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=1234 \
-d mysql:latest

注意启动slave时,需要通过--link参数,为slave提供访问master的别名
执行docker ps -f 'name=mysql-slave'查看容器运行状态如下

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1c834a26ef57        mysql:latest        "docker-entrypoint.s…"   13 seconds ago      Up 13 seconds       0.0.0.0:3308->3306/tcp   mysql-slave

执行以下命令登录mysql-slave,验证mysql可用

mysql -u root -p1234 -h 127.0.0.1 -P 3308

3. 配置主从

3.1 配置master

登录master

mysql -u root -p1234 -h 127.0.0.1 -P 3307

配置帐号权限

GRANT REPLICATION SLAVE ON *.* to 'replica'@'%' identified by '1234';
FLUSH PRIVILEGES;

执行show master status查看master状态如下

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      584 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
3.2 配置slave

登录slave

mysql -u root -p -h 127.0.0.1 -P3308

配置master的信息,并启动slave

change master to
master_host='mysql-master',
master_user='replica',
master_log_file='mysql-bin.000003',
master_log_pos=584,
master_port=3306,
master_password='1234';

start slave;

执行show slave status查看slave信息如下

               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 584
               Relay_Log_File: 1c834a26ef57-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: mysql.%
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 584
              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: 1
                  Master_UUID: 8f808469-1f40-11e8-9b85-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:

确认Slave_IO_Running: YesSlave_SQL_Running: Yes即可

3.3 如在配置主从同步前master中已有数据,则需提前进行数据同步操作

登录master,执行锁表操作

mysql -u root -p1234 -h 127.0.0.1 -P 3307
FLUSH TABLES WITH READ LOCK;

将master中需要同步的db的数据dump出来

mysqldump -u root -p -h 127.0.0.1 -P 3307  mytest > mytest.dump 

将数据导入slave

mysql -u root -p -h 127.0.0.1 -P 3308 mytest < mytest.dump

按前文所述步骤开启主从同步,然后解锁master

UNLOCK TABLES;
上一篇下一篇

猜你喜欢

热点阅读