docker 搭建mysql5.7.35主从同步

一、安装docker
1、下载dockeryum源文件

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

2、安装docker

yum install -y docker-ce

3、启动docker并加入自启

systemctl enable docker
systemctl start docker

4、查看版本

docker --version

5、修改docker仓库地址

cat > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": [
        "https://1nj0zren.mirror.aliyuncs.com",
        "https://kfwkfulq.mirror.aliyuncs.com",
        "https://2lqq34jg.mirror.aliyuncs.com",
        "https://pee6w651.mirror.aliyuncs.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com"
    ]
}
EOF
systemctl restart docker

二、部署mysql
1、首先拉取docker镜像,我们这里使用5.7。35版本的mysql

docker pull mysql:5.7.35

2、启动容器,分别启动两个容器

Master(主):
docker run -p 3306:3306 --name mysql-master -v /data/docker/mysql-master/data:/var/lib/mysql -v  /data/docker/mysql-master/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.35

Slave(从):
docker run -p 3309:3306 --name mysql-slave -v /data/docker/mysql-slave/data:/var/lib/mysql -v /data/docker/mysql-slave/conf:/etc/mysql/conf.d -e  MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.35

Master对外映射的端口是3306,Slave对外映射的端口是3309。因为docker容器是相互独立的,每个容器有其独立的ip,所以不同容器使用相同的端口并不会冲突。这里我们应该尽量使用mysql默认的3306端口,否则可能会出现无法通过ip连接docker容器内mysql的问题。

2、可以直接通过Navicat直接连接

三、配置主从
1、配置Master(主)

#通过命令进入到Master容器内部
docker exec -it 45ad853bc33d /bin/bash
或者
docker exec -it mysql-master /bin/bash

2、创建mysql配置文件my.cnf

#切换到/data/docker/mysql-master/conf目录下
vim /data/docker/mysql-master/conf/my.cnf
主节点:
[mysqld]
## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
## 开启gtid
gtid_mode=on                   ## (必选)
enforce-gtid-consistency=1     ##(必选)
log_bin=mysql-bin              ##(可选)高可用切换,最好开启该功能
log-slave-updates=1            ##(可选)高可用切换,最好打开该功能

从节点:
vim /data/docker/mysql-slave/conf/my.cnf
[mysqld]
## 设置server_id,注意要唯一
server-id=101  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin   
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
## 开启gtid
gtid_mode=on                   ## (必选)
enforce-gtid-consistency=1     ##(必选)
log_bin=mysql-bin              ##(可选)高可用切换,最好开启该功能
log-slave-updates=1            ##(可选)高可用切换,最好打开该功能

重启mysql容器
docker restart mysql-master
docker restart mysql-slave

3、进入mysql主库,查看gitid开启情况

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

mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | ON        |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
8 rows in set (0.01 sec)
mysql> grant Replication slave, Replication client on *.* to 'repl'@'%' identified by '123456';  创建主从复制的用户

4、进入从节点配置主从

#配置主库信息
mysql> change master to master_host='192.168.112.138',master_port=3306,master_user='repl',master_password='123456', master_auto_position = 1;
#启动主动
mysql> start slave; 
#查看状态
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.112.138
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 454
               Relay_Log_File: edu-mysql-relay-bin.000002
                Relay_Log_Pos: 667
        Relay_Master_Log_File: mysql-bin.000001
             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: 454
              Relay_Log_Space: 878
              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: 48460b95-0b9a-11ec-96db-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: 48460b95-0b9a-11ec-96db-0242ac110002:1
            Executed_Gtid_Set: 48460b95-0b9a-11ec-96db-0242ac110002:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
# 查看只读状态
mysql> show variables like '%read_only%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_read_only      | OFF   |
| read_only             | OFF   |
| super_read_only       | OFF   |
| transaction_read_only | OFF   |
| tx_read_only          | OFF   |
+-----------------------+-------+
5 rows in set (0.01 sec)
# 从库设置只读
mysql> set global read_only=1; 
Query OK, 0 rows affected (0.00 sec)

5、测试

mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| repl          | %         |
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)

可以看到主库上repl这个用于主从同步的用户已经同步过来了。

#主库创建数据库
mysql> create database nacos;
Query OK, 1 row affected (0.00 sec)
#从库查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nacos              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

已经存在nacos的数据库了,到这里mysql主从同步就配置完成了。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注