备注:阅读此文需要对Docker有基本的了解
master 10.222.32.122
slave 10.222.32.10
[root@10-222-32-122 ~]# docker pull mariadb:10.3.11
[root@10-222-32-10 ~]# docker pull mariadb:10.3.11
1 将容器里的配置文件拷贝出来(两台机器都操作)
docker run --name master -d \
--network=host \
-e MYSQL_ROOT_PASSWORD=123456 \
mariadb:10.3.11
mkdir -p /data0/mariadb-docker-conf/mysql
mkdir -p /data0/mariadb-docker-data
mkdir -p /data0/mariadb-docker-backup
docker cp master:/etc/mysql /data0/mariadb-docker-conf
docker rm -fv master
#slave 同理操作
2 启动数据库映射配置文件(两台机器都操作)
[root@10-222-32-122 ~] docker run --name master -d \
--network=host \
-v /data0/mariadb-docker-conf/mysql:/etc/mysql \
-v /data0/mariadb-docker-data:/var/lib/mysql \
-v /data0/mariadb-docker-backup:/opt \
-e MYSQL_ROOT_PASSWORD=123456 \
mariadb:10.3.11
[root@10-222-32-10 ~] docker run --name slave -d \
--network=host \
-v /data0/mariadb-docker-conf/mysql:/etc/mysql \
-v /data0/mariadb-docker-data:/var/lib/mysql \
-v /data0/mariadb-docker-backup:/opt \
-e MYSQL_ROOT_PASSWORD=123456 \
mariadb:10.3.11
专门映射一个目录/data0/mariadb-docker-backup给后面导入导出数据使用
使用的配置文件 /data0/mariadb-docker-conf/mysql/my.cnf
使用的数据目录 /data0/mariadb-docker-data
检查是否启动成功
[root@10-222-32-122 ~]# netstat -lntup | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 7374/mysqld
[root@10-222-32-122 ~]# docker exec master mysql -h10.222.32.122 -uroot -p123456 -P3306 -e "show databases"
网络模式使用的是docker host网络,直接复用宿主机上的网络资源,不需要在映射端口出来
slave 检查同理
3 更改数据库配置文件并重启数据库
#3.1 主库需要更改的配置
[root@10-222-32-122 ~]# egrep 'server-id|^log_bin' /data0/mariadb-docker-conf/mysql/my.cnf
server-id = 1
log_bin = /var/log/mysql/mariadb-bin
[root@10-222-32-122 ~]# docker restart master
#3.2 从库需要更改的配置
[root@10-222-32-10 ~]# egrep 'server-id' /data0/mariadb-docker-conf/mysql/my.cnf
server-id = 2
[root@10-222-32-10 ~]# docker restart slave
4 在master上创建复制账号
[root@10-222-32-122 ~]# mysql -h10.222.32.122 -uroot -p123456 -P3306
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'As4k.top';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
#这个权限专门负责 "复制"
5 配置从库连接上主库
[root@10-222-32-122 ~]# mysql -h10.222.32.122 -uroot -p123456 -P3306 -e "show master status"
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 330 | | |
+--------------------+----------+--------------+------------------+
[root@10-222-32-10 ~]# mysql -h10.222.32.10 -uroot -p123456 -P3306
mysql> CHANGE MASTER TO
MASTER_HOST='10.222.32.122',
MASTER_USER='repl',
MASTER_PORT=3306,
MASTER_PASSWORD='As4k.top',
MASTER_LOG_FILE='mariadb-bin.000001',
MASTER_LOG_POS=330;
6 备份master上的数据
#尽量进入容器内操作
[root@10-222-32-122 ~]# docker exec -it master bash
mysqldump -hlocalhost -uroot -p123456 -P3306 --all-databases --master-data > /opt/dbdump.db
7 把备份的数据还原到从库上
#跟上一个步骤用的同一台机器 注意IP地址是从库
[root@10-222-32-122 ~]# docker exec -it master bash
mysql -h10.222.32.10 -uroot -p123456 -P3306 < /opt/dbdump.db
#检查从库上是否已经有数据
mysql -h10.222.32.10 -uroot -p123456 -P3306 -e "show databases"
8 在从库上启动复制线程
mysql -h10.222.32.10 -uroot -p123456 -P3306
mysql> start slave;
mysql> show slave status\G
关于重来
如果中间有意外操作失误什么的,可以考虑清空从库上的数据重新操作
docker exec master mysql -h10.222.32.122 -uroot -p123456 -P3306 -e "show master status"
docker exec master mysql -h10.222.32.122 -uroot -p123456 -P3306 -e "show slave hosts"
docker exec master mysql -h10.222.32.10 -uroot -p123456 -P3306 -e "show slave status\G"
[root@10-222-32-122 ~]# docker exec master mysql -h10.222.32.122 -uroot -p123456 -P3306 -e "show master status"
File Position Binlog_Do_DB Binlog_Ignore_DB
mariadb-bin.000001 330
[root@10-222-32-122 ~]# docker exec master mysql -h10.222.32.122 -uroot -p123456 -P3306 -e "show slave hosts"
Server_id Host Port Master_id
2 3306 1
[root@10-222-32-122 ~]# docker exec master mysql -h10.222.32.10 -uroot -p123456 -P3306 -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.222.32.122
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 330
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 557
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
.....
root@10-222-32-122:/# mysqld --verbose --help | head -40
mysqld Ver 10.3.11-MariaDB-1:10.3.11+maria~bionic for debian-linux-gnu on x86_64 (mariadb.org binary distribution)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Starts the MariaDB database server.
Usage: mysqld [OPTIONS]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
......
删除docker容器
docker rm -fv ${CONTAINER_ID}
启动、停止从库复制线程
start slave;
stop slave;
show master status;
show slave status;
show slave hosts;
1 Got error: 1556: You can’t use locks with log tables
解决:使用的mysqldump版本不一致,请使用容器内部的mysqldump,或其它方式确保版本一致
https://blog.csdn.net/xys2015/article/details/109340223
https://blog.csdn.net/yabingshi_tech/article/details/51144051
csdn 110089797