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

database

作者:王少鹏(老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)

 

以上是 MySQL容器部署及数据持久化(主从复制) 的全部内容, 来源链接: utcz.com/z/535107.html

回到顶部