签到成功

知道了

CNDBA社区CNDBA社区

基于Docker 环境的 MySQL 8.0 主从复制 搭建手册

2020-03-29 14:46 3040 0 原创 Docker
作者: dave

在之前的博客我们看了Docker 安装Mysql 容器,如下:

Redhat 7.7 系统上 Docker 安装 MySQL
https://www.cndba.cn/dave/article/4102

http://www.cndba.cn/cndba/dave/article/4106

本篇博客我们了解下基于Docker 的MySQL主从环境搭建。

1 安装MySQL 容器

我们这里使用2台物理主机,分别安装好MySQL容器。http://www.cndba.cn/cndba/dave/article/4106

http://www.cndba.cn/cndba/dave/article/4106

主库:192.168.74.202
从库:192.168.74.203http://www.cndba.cn/cndba/dave/article/4106

具体过程参考之前的博客,这里不再描述。

Redhat 7.7 系统上 Docker 安装 MySQL
https://www.cndba.cn/dave/article/4102

主库:

[root@www.cndba.cn_master ~]# docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                   PORTS                    NAMES
4e327b82cd18        192.168.74.203:5000/tomcat   "catalina.sh run"        2 hours ago         Up 2 hours               0.0.0.0:8080->8080/tcp   tomcat
34ad82852c64        mysql:latest                 "docker-entrypoint.s…"   13 hours ago        Exited (0) 2 hours ago                            mysqlserver
[root@www.cndba.cn_master ~]#

从库:

[root@www.cndba.cn_slave tomcat]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              9b51d9275906        3 weeks ago         547MB
registry            latest              708bc6af7e5e        2 months ago        25.8MB
[root@www.cndba.cn_slave tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
f42c919cd98b        mysql:latest        "docker-entrypoint.s…"   18 seconds ago      Up 16 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlserver
6ae59667de8a        registry            "/entrypoint.sh /etc…"   3 hours ago         Up 2 hours          0.0.0.0:5000->5000/tcp              registry
[root@www.cndba.cn_slave tomcat]#

2 配置主从复制

2.1 修改mysql参数

docker 默认没有安装vim命令,需要提前安装,具体参考如下博客:

Docker 更改debian 源 并安装 vim 工具
https://www.cndba.cn/dave/article/4105

分别连接主从库容器,修改my.conf 参数文件。

主库添加如下内容:http://www.cndba.cn/cndba/dave/article/4106

[mysqld]
server_id = 1
log-bin= mysql-bin
read-only=0

root@34ad82852c64:/# cat /etc/mysql/my.cnf|grep -v "^#"


[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
symbolic-links=0

server_id = 1
log-bin= mysql-bin
read-only=0

!includedir /etc/mysql/conf.d/
root@34ad82852c64:/#

从库添加如下内容:

[mysqld]
server_id = 2
log-bin= mysql-bin
read-only=1

root@f42c919cd98b:/etc/apt# cat /etc/mysql/my.cnf|grep -v "^#"


[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
symbolic-links=0

server_id = 2
log-bin= mysql-bin
read-only=1

!includedir /etc/mysql/conf.d/
root@f42c919cd98b:/etc/apt#

分别重启主从库容器:

[root@www.cndba.cn_master ~]# docker restart mysqlserver
mysqlserver
[root@www.cndba.cn_master ~]#

[root@www.cndba.cn_slave ~]# docker restart mysqlserver
mysqlserver
[root@www.cndba.cn_slave ~]#

当然,在初始化容器的时候也可以将mysql的参数映射到本地路径,如下:

http://www.cndba.cn/cndba/dave/article/4106
http://www.cndba.cn/cndba/dave/article/4106
http://www.cndba.cn/cndba/dave/article/4106
http://www.cndba.cn/cndba/dave/article/4106

docker run —name slavemysql -d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=root -v ~/test/mysql_test/slave/data:/var/lib/mysql -v ~/test/mysql_test/slave/conf/my.cnf:/etc/mysql/my.cnf mysql:5.7

2.2 主库创建同步用户并授权

[root@www.cndba.cn_master conf]# docker exec -it mysqlserver bash
root@34ad82852c64:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.

mysql>CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

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

记住File、Position的值,在从库中会用到。

2.3 从库配置同步信息并启动slave

[root@www.cndba.cn_slave tomcat]# docker exec -it mysqlserver bash
root@f42c919cd98b:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.

//设置主库链接
mysql> change master to master_host='192.168.74.202',master_user='slave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=856,master_port=3306;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

//启动从库同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)


//查看状态
mysql> show slave status/G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.74.202
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 856
               Relay_Log_File: f42c919cd98b-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            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: 856
              Relay_Log_Space: 155
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2061
                Last_IO_Error: error connecting to master 'slave@192.168.74.202:3306' - retry-time: 60 retries: 3 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             Master_Info_File: mysql.slave_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: 200329 06:18:50
     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:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

ERROR:
No query specified

如果 show slave status/G命令结果中出现: Slave_IO_Running: Yes Slave_SQL_Running: Yes 以上两项都为Yes,那说明没问题了。 但我们这里Slave_IO_Running状态不对,查看docker 日志,如下:

[root@www.cndba.cn_slave ~]# docker logs --tail 5 mysqlserver
2020-03-29T06:31:50.714902Z 10 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'slave@192.168.74.202:3306' - retry-time: 60 retries: 16 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
2020-03-29T06:32:50.717568Z 10 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'slave@192.168.74.202:3306' - retry-time: 60 retries: 17 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
2020-03-29T06:33:50.729106Z 10 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'slave@192.168.74.202:3306' - retry-time: 60 retries: 18 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
2020-03-29T06:34:50.730994Z 10 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'slave@192.168.74.202:3306' - retry-time: 60 retries: 19 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
2020-03-29T06:35:50.739934Z 10 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'slave@192.168.74.202:3306' - retry-time: 60 retries: 20 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
[root@www.cndba.cn_slave ~]#

因为我们这里测试使用的是MySQL 8.0,在mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password, 解决问题方法有两种,一种是升级navicat驱动,一种是把mysql用户登录密码加密规则还原成mysql_native_password. 我们这里使用旧的加密规则,在主库修改:http://www.cndba.cn/cndba/dave/article/4106

mysql> alter user 'slave'@'%' identified by '123456' password expire never;
Query OK, 0 rows affected (0.01 sec)

mysql> alter user 'slave'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

再次查看从库slave正常:

mysql>  show slave status/G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.74.202
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2742
               Relay_Log_File: f42c919cd98b-relay-bin.000002
                Relay_Log_Pos: 2208
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

2.4 验证同步

主库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database cndba;
Query OK, 1 row affected (0.00 sec)

mysql> use cndba;
Database changed
mysql> create table dave(username varchar(50),age int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into dave values('Dave',18);
Query OK, 1 row affected (0.01 sec)

mysql> select * from dave;
+----------+------+
| username | age  |
+----------+------+
| Dave     |   18 |
+----------+------+
1 row in set (0.00 sec)

mysql>

从库查询:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| cndba              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use cndba;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from dave;
+----------+------+
| username | age  |
+----------+------+
| Dave     |   18 |
+----------+------+
1 row in set (0.00 sec)

mysql>

主从同步正常。

我们这里演示的是新数据的同步,如果源库里已经有数据,那么就需要先将主库备份并恢复到从库,在配置同步。 至此基于Docker 的MySQL 8.0 主从同步环境搭建结束。

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2296
    原创
  • 3
    翻译
  • 582
    转载
  • 198
    评论
  • 访问:8508373次
  • 积分:4477
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ