当前位置: 首页 > article >正文

docker-compose部署mysql5.7主从

1. 部署环境

系统版本:Centos7.9
服务器ip:192.168.5.152(mysql主),192.168.5.144(mysql从)
mysql版本:5.7

2. 部署mysql

2.1 部署master

2.1.1 创建mysql目录
mkdir –p /data/mysql

cd /data/mysql

mkdir conf

mkdir data

mkdir log
2.1.2 创建mysql配置文件
vim conf/my.cnf

[mysqld]

# [必须]服务器唯一ID, 主库的server-id值必须比从库的小
server-id=104

# [必须]启用二进制日志
log-bin=mysql-bin

# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql

#指定错误日志文件位置
log-error = /var/log/mysql/error.log

#开启慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 60

#开启通用日志
general_log = 1
general_log_file = /var/log/mysql/general.log

# 确保binlog日志写入后与硬盘同步
sync_binlog = 1

# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all

# default
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
2.1.3 编写docker-compose文件
cd /data/mysql

vim mysql.yaml
version: '3'
services:
  mysql:
    container_name: mysql-master
    image: mysql:5.7
    restart: always
    volumes:
      - ./log:/var/log/mysql
      - ./conf/my.cnf:/etc/mysql/my.cnf
      - ./data:/var/lib/mysql
    ports:
      - '3306:3306'
    command: [
        '--explicit_defaults_for_timestamp=true',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_general_ci',
        '--max_connections=3000'
    ]
    environment:
      MYSQL_ROOT_PASSWORD: "shield"
2.1.4 启动mysql容器
cd /data/mysql
docker-compose -f mysql.yaml up –d
2.1.5 进入容器,创建同步账号,并开启权限
docker exec -it mysql-master bash

mysql -uroot -pshield

# 查看配置的服务ID,显示的应该是配置的ID

mysql> show variables like '%server_id%';
 

# 看master信息 File 和 Position 从服务上要用

mysql> show master status;
 

# 创建同步账户并开启权限,只是同步账号
mysql> create user 'sync'@'%' identified by '123456';
mysql> grant replication slave,replication client on *.* to 'sync'@'%';

# 刷新信息生效
mysql> flush privileges;

2.2 部署slave

2.2.1 创建mysql目录
mkdir –p /data/mysql

cd /data/mysql

mkdir conf

mkdir data

mkdir log
2.2.2 创建mysql配置文件
vim conf/my.cnf
[mysqld]
# [必须]服务器唯一ID
server-id=106

# [必须]启用二进制日志
log-bin=mysql-bin

# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql

#指定错误日志文件位置
log-error = /var/log/mysql/error.log

#开启慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 60

#开启通用日志
general_log = 1
general_log_file = /var/log/mysql/general.log

# 确保binlog日志写入后与硬盘同步
sync_binlog = 1

# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all

# relay_log配置中继日志
relay_log=mysql-relay-bin

# log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1

# default 
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
2.2.3 编写docker-compose文件
cd /data/mysql

vim mysql.yaml
version: '3'
services:
  mysql:
    container_name: mysql-slave
    image: mysql:5.7
    restart: always
    volumes:
      - ./log:/var/log/mysql
      - ./conf/my.cnf:/etc/mysql/my.cnf
      - ./data:/var/lib/mysql
    ports:
      - '3306:3306'
    command: [
        '--explicit_defaults_for_timestamp=true',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_general_ci',
        '--max_connections=3000'
    ]
    environment:
      MYSQL_ROOT_PASSWORD: "shield"
2.2.4 启动mysql容器
cd /data/mysql
docker-compose -f mysql.yaml up –d
2.2.5 进入容器,连接mysql主服务

docker exec -it mysql-slave bash

mysql -uroot -pshield

#查看server_id是否生效
mysql> show variables like '%server_id%';
 

# 连接主mysql服务 master_log_file 和 master_log_pos的值要填写主master里查出来的值 注意这里使用的docker-compose 内部服务的端口和ip,实际应该为真实的master_host,参数看后面的介绍
mysql> change master to master_host='192.168.5.152',master_user='sync',master_password='123456',master_port=3306,master_log_file='mysql-bin.000004', master_log_pos=313,master_connect_retry=30;

# 开启slave
mysql> start slave;

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.152
                  Master_User: sync
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 313
               Relay_Log_File: mysql-relay-bin.000007
                Relay_Log_Pos: 526
        Relay_Master_Log_File: mysql-bin.000004
             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: 313
              Relay_Log_Space: 899
              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: 104
                  Master_UUID: 5f25a087-8379-11ee-8a89-0242ac120002
             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_Running: Yes
Slave_SQL_Running: Yes

至此docker-compose部署mysql5.7主从完毕。

3. 主从切换

3.1停止原主mysql

docker stop mysql-master

3.2停止从数据库的复制线程

mysql> stop slave;
mysql> reset slave all;

3.3 将从数据库升为主数据库

mysql> reset master;

3.4 原主数据库降为从数据库

mysql> CHANGE MASTER TO MASTER_HOST = '192.168.5.144', MASTER_USER = 'sync', MASTER_PASSWORD = '123456';

mysql> start slave;

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.144
                  Master_User: sync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 304
               Relay_Log_File: e75962e1de15-relay-bin.000007
                Relay_Log_Pos: 517
        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: 304
              Relay_Log_Space: 731
              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: 106
                  Master_UUID: 616899ae-837e-11ee-b615-0242ac120002
             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)

至此手动切换mysql主从完成。


http://www.kler.cn/a/135303.html

相关文章:

  • 火车车厢重排问题,C++详解
  • Oracle ADB 导入 BANK_GRAPH 的学习数据
  • Vue2:组件
  • Windows C++ TCP/IP 两台电脑上互相传输字符串数据
  • HTML之表单学习记录
  • 实时渲染技术如何助力3D虚拟展厅?
  • 广州一母婴店因设置0元购导致关店
  • SpringBoot中日志的使用log4j2
  • iOS学习 --- Xcode 15 下载iOS_17.0.1_Simulator失败解决方法
  • Eclipse切换中文环境
  • 什么是单域名SSL安全证书?
  • 刷题笔记day27-回溯算法1
  • Linux CentOS 8(MariaDB的数据类型)
  • GEE21:基于MODIS数据获取逐年GPP、EVI和FVC数据
  • 数据结构——栈与队列
  • 将Agent技术的灵活性引入RPA,清华等发布自动化智能体ProAgent
  • 【Qt开发流程之】布局管理
  • Linux安装RabbitMQ详细教程
  • mysql使用--子查询
  • 使用量子玻尔兹曼机推进机器学习:新范式
  • 自然语言处理:Transformer和GPT
  • 数智赋能,众创众治|易知微为“浙江省数字监管应用建模技能竞赛”提供技术支撑!
  • git使用记录
  • 大数据架构
  • pipeline + node +jenkins+kubernetes部署yarn前端项目
  • Qt给控件添加图片