Loading
0

Docker容器中Mysql数据库主从同步搭建方法
被墙跳转TG:@qianhenetwork QQ 851617266

301免备案跳转微信公众号
腾讯云服务器大促销。
华为服务器
前言:Docker容器中如何搭建mysql数据库主从,本教程将介绍在docker容器中搭建mysql数据库的主从方法,希望对大家有帮助 。
容器中mysql主从搭建:
更多docker容器基础知识可访问:https://www.zfcdn.xyz/showinfo-3-36227-0.html
1、新建主服务器容器实例:端口为3307

docker run -p 3307:3306 --name=mysql-master \
-v /mydata/mysql-master/log:/var/log/mysql \
-v /mydata/mysql-master/data:/var/lib/mysql \
-v /mydata/mysql-master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=blogtagggpw  \
-d mysql:5.7

2、进入/mydata/mysql-master/conf目录,创建my.cnf文件,并写入如下内容。

[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql  
## 开启二进制日志功能
log-bin=mall-mysql-bin  
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M  
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed  
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7  
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062

重启mysql-master容器。

docker restart mysql-master

重启后一定要执行如下命令看下是否有启动成功,若启动成功,则说明配置正常,容器正常。

docker ps -a

[root@blog-tag-gg conf]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
a1308309a139   mysql:5.7   "docker-entrypoint.s…"   6 minutes ago   Up 4 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master

3、进入mysql-master容器内部:

docker exec -it mysql-master /bin/bash


输入如下命令回车并输入数据库root密码看是否可进入数据库,若能进入,则正常。

mysql -uroot -p

4、mysql-master容器实例内部创建数据同步用户
创建slave用户并设置密码,以及设置连接权限为%

CREATE USER 'slave'@'%' IDENTIFIED BY 'blogtagggpw';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

创建后:

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

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql>

5、新建从服务器容器实例 端口为:3308

docker run -p 3308:3306 --name mysql-slave \
-v /mydata/mysql-slave/log:/var/log/mysql \
-v /mydata/mysql-slave/data:/var/lib/mysql \
-v /mydata/mysql-slave/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=blogtagggpw  \
-d mysql:5.7

[root@blog-tag-gg conf]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
738d0b3cc403   mysql:5.7   "docker-entrypoint.s…"   25 seconds ago   Up 23 seconds   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
a1308309a139   mysql:5.7   "docker-entrypoint.s…"   17 minutes ago   Up 11 minutes   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master

6、进入/mydata/mysql-slave/conf目录,创建my.cnf文件并写入如下代码

[mysqld]
## 设置server_id,同一局域网中需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql  
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysql-slave1-bin  
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M  
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed  
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7  
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062  
## relay_log配置中继日志
relay_log=mall-mysql-relay-bin  
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1  
## slave设置为只读(具有super权限的用户除外)
read_only=1

7、重启从服务器容器。

docker restart mysql-slave

重启后执行如下命令看是否有启动成功。(有up表示启动成功)

docker ps -a

8、在主数据库中查看主从同步状态。
登录主服务器容器进入数据库,执行如下命令查看同步状态

show master status;

结果如下:表示设置成功。

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

9、进入从容器服务器 mysql-slave

docker exec -it mysql-slave

10、在从数据库中配置主从复制:
通过数据库密码进入数据库后执行如下命令配置

change master to master_host='192.168.16.4',master_user='slave',master_password='blogtagggpw',master_port=3307,master_log_file='mall-mysql-bin.000001',master_log_pos=617,master_connect_retry=30;

各项说明可参考:
master_host:主数据库的IP地址;可以通过ifconfig 命令查看。
master_port:主数据库的运行端口;
master_user:在主数据库创建的用于同步数据的用户账号;
master_password:在主数据库创建的用于同步数据的用户密码;
master_log_file:指定从数据库要复制数据的日志文件,通过查看主数据的状态,获取File参数;
master_log_pos:指定从数据库从哪个位置开始复制数据,通过查看主数据的状态,获取Position参数;
master_connect_retry:连接失败重试的时间间隔,单位为秒。
11、在从数据库中查看主从同步状态

show slave status \G;

结果:

mysql> show slave status \G;
*************************** 1. row ***************************

               Slave_IO_State:
                  Master_Host: 192.168.16.4
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 30
              Master_Log_File: mall-mysql-bin.000001
          Read_Master_Log_Pos: 617
               Relay_Log_File: mall-mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mall-mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No

              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: 617
              Relay_Log_Space: 154
              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: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           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

显示如下则表示还没开始同步

             Slave_IO_Running: No
            Slave_SQL_Running: No

12、在从数据库中开启主从同步:

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

13、查看从数据库中状态,此时发现已同步
执行命令

show slave status \G;

返回结果:发现编程了yes,则表示正常了。

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.16.4
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 30
              Master_Log_File: mall-mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mall-mysql-relay-bin.000004
                Relay_Log_Pos: 377
        Relay_Master_Log_File: mall-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:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 812
              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: 101
                  Master_UUID: 5a4efa74-552b-11ed-a3c5-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.13 sec)

ERROR:
No query specified

14、主从复制测试:
在主数据库中创建数据库及表以及写入信息

mysql> create database blogtaggg_db;
Query OK, 1 row affected (0.11 sec)

mysql> use blogtaggg_db;
Database changed
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.09 sec)

mysql> insert into t1 values(1,'www.zfcdn.xyz');
Query OK, 1 row affected (0.02 sec)

mysql> select * from t1;
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | www.zfcdn.xyz |
+------+-------------+
1 row in set (0.00 sec)

登录从数据库,看是否可以直接打开 blogtaggg_db 这个数据库并查看数据。

mysql> use blogtaggg_db;
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 t1;
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | www.zfcdn.xyz |
+------+-------------+
1 row in set (0.03 sec)

查看数据已经同步到从数据库了,大工搞成。
301免备案跳转微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://www.zfcdn.xyz/showinfo-3-36244-0.html
亲爱的:被墙域名跳转TG:@qianhenetwork QQ:851617266,可否收藏+评论+分享呢?
上一篇:解决docker报错:Cannot connect to the Docker daemon Is the docker daemon running?
下一篇:Linux系统常见的CPU/Load/IO负载高负载异常分析方法