MySQL容器部署及数据持久化(主从复制)「终于解决」

MySQL容器部署及数据持久化(主从复制)「终于解决」作者:王少鹏(老A),中国开源协会(oscna.org)数据库分会成员,现就职于上海某MySQL数据库厂商。 本文来源:鸡书之家(dbawsp.com)出品,转载请注明来源,谢谢。 运维经常要和数据…

MySQL容器部署及数据持久化(主从复制)

作者:王少鹏(老A),中国开源协会(oscna.org)数据库分会成员,现就职于上海某MySQL数据库厂商。

本文来源:鸡书之家(dbawsp.com)出品,转载请注明来源,谢谢。


运维经常要和数据库打交道,尤其是MySQL,以前的方式是在本机装个MySQL或者MySQL主从复制架构用于本地调试,但有了docker以后就不需要再去繁琐的安装MySQL啦(虽然安装MySQL也不是很麻烦),直接在docker中启几个MySQL容器就可以自动建立主从复制关系了。

1. 安装docker与docker-compose

# 卸载老版本docker
[root@docker ~]# yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

[root@docker ~]# yum -y install epel-release wget
[root@docker ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@docker ~]# sed -i "s#download.docker.com#mirrors.tuna.tsinghua.edu.cn/docker-ce#g" /etc/yum.repos.d/docker-ce.repo
[root@docker ~]# yum -y install docker-ce

# 启动并配置镜像加速
[root@docker ~]# systemctl start docker.service && systemctl enable docker.service

[root@docker ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

或者

{
  "registry-mirrors": ["https://dockerhub.mirrors.nwafu.edu.cn/"],
  "storage-driver":"devicemapper"
}

[root@docker ~]# systemctl restart docker.service

 

  • 安装docker-compose
[root@docker ~]# curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

 

2. 先决条件

[root@docker ~]# docker pull mysql:5.7.30

[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7.30              a4fdfd462add        10 hours ago        448MB

[root@docker ~]# mkdir -p /data/mysql/master/{conf,data}
[root@docker ~]# mkdir -p /data/mysql/slave/conf
[root@docker ~]# mkdir -p /data/mysql/{init-db-m,init-db-s}
[root@docker ~]# chown -R mysql.mysql /data/mysql

# 主库的创建用户SQL脚本
[root@docker ~]# cat /data/mysql/init-db-m/create_user_1.sql
grant all on *.* to "dumpuser"@"%" identified by "123456";
grant replication slave on *.* to "repl"@"%" identified by "123456";

# 从库远程备份主库及创建主从通道Bash脚本
[root@docker ~]# cat /data/mysql/init-db-s/dump-repl_1.sh
#!/bin/bash
while ! mysql -uping -p123456 -hmysql_m_compose -P3306 -e "select 1"
do
    sleep 1
done
sleep 3
    mysqldump -udumpuser -p123456 -hmysql_m_compose -P3306 --single-transaction --default-character-set=utf8mb4 --set-gtid-purged=on --master-data=2 --flush-logs --hex-blob --triggers --routines --events --all-databases > /tmp/full.sql
    mysql -uroot -p123456 -e "reset master;"
    mysql -uroot -p123456 -e "source /tmp/full.sql;"
    mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST="mysql_m_compose",MASTER_USER="repl",MASTER_PASSWORD="123456",MASTER_PORT=3306,MASTER_CONNECT_RETRY=10,MASTER_AUTO_POSITION=1;"
    mysql -uroot -p123456 -e "start slave;"

 

3. 创建主从配置文件

# mysql_m_compose节点
[root@docker ~]# cat /data/mysql/master/conf/my.cnf
[mysqld]
server_id = 33060000
port = 3306
log_timestamps=SYSTEM
max_allowed_packet = 16M
read_only = 0
character_set_server = utf8mb4
secure_file_priv = ""
max_connect_errors = 100000
interactive_timeout = 1800
wait_timeout = 1800

# BINLOG
log_bin = mysql-bin
binlog_format = row
log_slave_updates = 1
max_binlog_size = 200M
relay_log = relay-bin
sync_binlog = 1

# GTID
gtid_mode = ON
enforce_gtid_consistency = 1
binlog_gtid_simple_recovery = 1

# ENGINE
default_storage_engine = InnoDB
innodb_flush_log_at_trx_commit=1

# ----------------------------------------------------------------------------------------------------------------------

# mysql_s_compose节点
[root@docker ~]# cat /data/mysql/slave/conf/my.cnf
[mysqld]
server_id = 33060001
port = 3306
log_timestamps=SYSTEM
max_allowed_packet = 16M
read_only = 0
character_set_server = utf8mb4
secure_file_priv = ""
max_connect_errors = 100000
interactive_timeout = 1800
wait_timeout = 1800

# BINLOG
log_bin = mysql-bin
binlog_format = row
log_slave_updates = 1
max_binlog_size = 200M
relay_log = relay-bin
sync_binlog = 1

# GTID
gtid_mode = ON
enforce_gtid_consistency = 1
binlog_gtid_simple_recovery = 1

# ENGINE
default_storage_engine = InnoDB
innodb_flush_log_at_trx_commit=1

 

4. 创建docker-compose.yml文件

  • 将init_sql下的文件映射到/docker-entrypoint-initdb.d目录下(注:/docker-entrypoint-initdb.d下以sqlsh结尾的文件会在数据库初始化完成后自动执行)
[root@docker ~]# cat /data/mysql/docker-compose.yml
version: "3"
services:
  mysql_m_compose:
    image: mysql:5.7.30
    container_name: mysql_m
    restart: always
    ports:
      - 33061:3306
    environment:
      - MYSQL_USER=ping
      - MYSQL_PASSWORD=123456
      - MYSQL_ROOT_PASSWORD=123456
    volumes:
      - ./master/conf/my.cnf:/etc/my.cnf
      - ./master/data:/var/lib/mysql
      - ./init-db-m:/docker-entrypoint-initdb.d

  mysql_s_compose:
    image: mysql:5.7.30
    container_name: mysql_s
    restart: always
    ports:
      - 33062:3306
    depends_on:
      - mysql_m_compose
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    volumes:
      - ./slave/conf/my.cnf:/etc/my.cnf
      - ./init-db-s:/docker-entrypoint-initdb.d

 

5. docker-compose启动mysql容器

[root@docker mysql]# docker-compose up -d
Creating network "mysql_default" with the default driver
Creating mysql_m ... done
Creating mysql_s ... done

[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
35cc9c6fbf52        mysql:5.7.30        "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        33060/tcp, 0.0.0.0:33062->3306/tcp   mysql_s
53a04e176ecc        mysql:5.7.30        "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        33060/tcp, 0.0.0.0:33061->3306/tcp   mysql_m

# 检查主从复制关系
# 主库
mysql_m> use db
Database changed
mysql_m> create table t1(id int,name varchar(32));
Query OK, 0 rows affected (0.01 sec)

mysql_m> insert into t1 values(1,"aa"),(2,"bb"),(3,"cc");
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql_m> select  * from db.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | cc   |
+------+------+
3 rows in set (0.00 sec)

# 从库
mysql_s> select  * from db.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | cc   |
+------+------+
3 rows in set (0.00 sec)

# 查看主从复制状态信息
mysql_s> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql_m_compose
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 194
               Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 367
        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: 194
              Relay_Log_Space: 568
              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: 33060000
                  Master_UUID: f93b936b-9b6b-11ea-9bda-0242c0a82002
             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:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set: f93b936b-9b6b-11ea-9bda-0242c0a82002:1-9
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

 

6. 销毁两个容器

[root@docker mysql]# docker-compose down
Stopping mysql_s ... done
Stopping mysql_m ... done
Removing mysql_s ... done
Removing mysql_m ... done
Removing network mysql_default
[root@docker mysql]# ll -sh /data/mysql/master/data/
total 192M
4.0K -rw-r----- 1 polkitd input 1.2K May 21 14:12 53a04e176ecc-slow.log
4.0K -rw-r----- 1 polkitd input   56 May 21 14:04 auto.cnf
4.0K -rw------- 1 polkitd input 1.7K May 21 14:04 ca-key.pem
4.0K -rw-r--r-- 1 polkitd input 1.1K May 21 14:04 ca.pem
4.0K -rw-r--r-- 1 polkitd input 1.1K May 21 14:04 client-cert.pem
4.0K -rw------- 1 polkitd input 1.7K May 21 14:04 client-key.pem
   0 drwxr-x--- 2 polkitd input   48 May 21 14:05 db
4.0K -rw-r----- 1 polkitd input 1.4K May 21 14:04 ib_buffer_pool
 76M -rw-r----- 1 polkitd input  76M May 21 14:12 ibdata1
 48M -rw-r----- 1 polkitd input  48M May 21 14:12 ib_logfile0
 48M -rw-r----- 1 polkitd input  48M May 21 14:04 ib_logfile1
 12M -rw-r----- 1 polkitd input  12M May 21 14:04 ibtmp1
4.0K drwxr-x--- 2 polkitd input 4.0K May 21 14:04 mysql
4.0K -rw-r----- 1 polkitd input  177 May 21 14:04 mysql-bin.000001
7.9M -rw-r----- 1 polkitd input 7.9M May 21 14:04 mysql-bin.000002
4.0K -rw-r----- 1 polkitd input  241 May 21 14:04 mysql-bin.000003
4.0K -rw-r----- 1 polkitd input  887 May 21 14:12 mysql-bin.000004
4.0K -rw-r----- 1 polkitd input   76 May 21 14:04 mysql-bin.index
 12K drwxr-x--- 2 polkitd input 8.0K May 21 14:04 performance_schema
4.0K -rw------- 1 polkitd input 1.7K May 21 14:04 private_key.pem
4.0K -rw-r--r-- 1 polkitd input  452 May 21 14:04 public_key.pem
4.0K -rw-r--r-- 1 polkitd input 1.1K May 21 14:04 server-cert.pem
4.0K -rw------- 1 polkitd input 1.7K May 21 14:04 server-key.pem
 12K drwxr-x--- 2 polkitd input 8.0K May 21 14:04 sys

[root@docker mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

7. 重建MySQL容器

[root@docker mysql]# docker-compose up -d
Creating network "mysql_default" with the default driver
Creating mysql_m ... done
Creating mysql_s ... done

[root@docker mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
7cd912fa9721        mysql:5.7.30        "docker-entrypoint.s…"   8 seconds ago       Up 8 seconds        33060/tcp, 0.0.0.0:33062->3306/tcp   mysql_s
d9e3514a1e19        mysql:5.7.30        "docker-entrypoint.s…"   9 seconds ago       Up 8 seconds        33060/tcp, 0.0.0.0:33061->3306/tcp   mysql_m

# 主库
mysql_m> select * from db.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | cc   |
+------+------+
3 rows in set (0.00 sec)

# 从库
mysql_s> select * from db.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | cc   |
+------+------+
3 rows in set (0.01 sec)

mysql_s> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql_m_compose
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 194
               Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000006
             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: 194
              Relay_Log_Space: 568
              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: 33060000
                  Master_UUID: f93b936b-9b6b-11ea-9bda-0242c0a82002
             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:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set: f93b936b-9b6b-11ea-9bda-0242c0a82002:1-12
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/6815.html

(0)
上一篇 2023-04-01
下一篇 2023-04-01

相关推荐

  • 简化框架开发:Python Inter Place

    简化框架开发:Python Inter PlacePython Inter Place是一个轻量级的框架,它可以帮助Python工程师们更方便地进行项目开发,提高开发效率。该框架专注于简化Web应用开发,具有快速、简单、灵活等特点。

    2024-01-08
    113
  • Mongo 备份数据库和恢复数据库「终于解决」

    Mongo 备份数据库和恢复数据库「终于解决」1. 备份 2. 恢复 3. 传输到另一个服务器恢复数据库 "scp" r /home/test.tar root@host:/home/others/ 4. Linux &quo

    2023-02-17
    182
  • 数据库学习之九:mysql 存储引擎「建议收藏」

    数据库学习之九:mysql 存储引擎「建议收藏」九、mysql 存储引擎 1、课程大纲 2、mysql存储引擎介绍 3、mysql存储引擎种类 4、数据库的存储引擎 存储引擎查询 存储引擎的配置: 配置存储引擎: 5、innodb体系结构: 表空间

    2023-02-26
    135
  • oracle查杀连接会话「建议收藏」

    oracle查杀连接会话「建议收藏」由于频繁强制启停tomcat不清理连接等情况时可能导致oracle连接爆满,此时可以使用这个方式清理 –查询 select sess.sid,sess.serial#,sess.machine, …

    2022-12-27
    143
  • [20200223]关于latch and mutext的优化.txt

    [20200223]关于latch and mutext的优化.txt[20200223]关于latch and mutext的优化.txt–//前一段时间一直在测试使用DBMS_SHARED_POOL.MARKHOT标识热对象以及sql语句的优化。–//有别人问感

    2023-01-31
    128
  • 安装whl的完整步骤

    安装whl的完整步骤Python是一门非常流行的编程语言,得益于其广泛的使用和社区支持,Python能够快速获得各类开发库和工具的支持。其中,应用最为广泛的是Python的第三方库,比如NumPy、Pandas、TensorFlow等。Python的第三方库提供了非常多的功能,比如图像处理、机器学习等领域,而且大多数都是开源的,充满活力。而这些第三方库的安装需要使用whl文件。

    2024-06-01
    57
  • 在Python中声明数组

    在Python中声明数组在编程中,数组是一种非常常见的数据结构。数组可以存储多个相同类型的数据,并可以通过下标索引来访问数组中的元素。在Python中,声明数组并不像C语言一样需要事先指定数组的大小,这是因为Python中的数组是一种动态的数据类型,可以自动调整大小以适应存储的元素。

    2024-05-10
    63
  • hdfs的ha机制_redis集群工作原理

    hdfs的ha机制_redis集群工作原理1.简单hdfs集群中存在的问题 不能存在两个NameNode 单节点问题 单节点故障转移 2.解决单节点问题 找额外一个NameNode备份原有的数据 会出现脑裂 脑裂:一个集群中多个管理者数据不一

    2022-12-30
    177

发表回复

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