前言
为了保证数据的安全性,mysql 容器在启动时都会使用数据卷持久化数据。
提示:以下是本篇文章正文内容,下面案例可供参考
一、准备MySQL镜像
我这里用的自己的镜像,大家可以下载,参考命令如下:
docker pull mysql:8.0.28
二、MySQL-master安装与配置
1.创建MySQL-master容器
启动命令如下(示例):
docker run --name mysql8_master \
-e MYSQL_ROOT_PASSWORD=123456 \
-v /mysql8_master/mydata:/var/lib/mysql \
-v /mysql8_master/log:/var/log/mysql \
-v /mysql8_master/conf:/etc/mysql/conf.d \
-dp 13306:3306 \
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/mysql:8.0.30-8.6
启动命令简述
docker run --name mysql8_master \ --指定容器名为mysql8_master
-e MYSQL_ROOT_PASSWORD=123456 \ --MySQL密码为123456
-v /mysql8_master/mydata:/var/lib/mysql \ --MySQL数据持久化目录
-v /mysql8_master/log:/var/log/mysql \ --日志持久化目录
-v /mysql8_master/conf:/etc/mysql/conf.d \ --配置文件持久化目录
-dp 13306:3306 \ --指定映射端口为13306
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/mysql:8.0.30-8.6 --指定镜像名称
2.修改master配置文件
vim my.cnf(示例):
client]
default_character_set=utf8mb4
[mysql]
default_character_set=utf8mb4
[mysqld]
character_set_server=utf8mb4
server_id=01
binlog-ignore-db=mysql
log-bin=master-log-bin
binlog_cache_size=1M
binlog_format=mixed
#expire_logs_days=7 --已经弃用
#slave_skip_errors=1062 --已经弃用
#lower-case-table-names = 1
踩坑部分:加了不区分大小写,容器无法启动。
3.重启master容器
docker restart mysql8_master
4.创建用户赋权操作
--进入容器
docker exec -it mysql8_master /bin/bash
--登录mysql
mysql -u root -p
--检查字符集
mysql> show variables like 'character%';
--创建用户
mysql> create user 'slave'@'%' identified by '1234.abcd';
--用户赋权
mysql> grant replication slave, replication client on *.* to 'slave'@'%';
三、MySQL-Slave 的安装与配置
1.创建MySQL-slave容器
docker run --name mysql8_slave \
-e MYSQL_ROOT_PASSWORD=123456 \
-v /mysql8_slave/mydata:/var/lib/mysql \
-v /mysql8_slave/log:/var/log/mysql \
-v /mysql8_slave/conf:/etc/mysql/conf.d \
-dp 23306:3306 \
anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/mysql:8.0.30-8.6
与master容器启动相同,修改容器名,持久化目录,端口号即可
2.修改slave配置文件
[client]
default_character_set=utf8mb4
[mysql]
default_character_set=utf8mb4
[mysqld]
character_set_server=utf8mb4
server_id=02
binlog-ignore-db=mysql
log-bin=slave-log-bin
binlog_cache_size=1M
binlog_format=mixed
relay_log=relay-log-bin
log_slave_updates=1
read_only=1
3.重启MySQL-slave容器
docker restart mysql8_slave
四、配置主从状态
1.查看master状态
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| master-log-bin.000006 | 157 | | mysql | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
2.slave指定master
# 修改从库指向到主库,使用主库记录的文件名以及位点
# 主库show master status; 显示的 File, Position )
CHANGE MASTER TO
master_host = '172.17.0.1',
master_user = 'slave',
master_password = 'slave',
master_log_file = 'master-log-bin.000006',
master_log_pos =1026;
#启动同步
START SLAVE;
3.查看主从同步状态
五、测试
1.在master 中创建一个数据表。
mysql> create database test;
Query OK, 1 row affected (0.02 sec)
mysql> use test;
Database changed
mysql> create table emp(id int,name varchar(30),age int);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into emp values(1,'zs',18);
Query OK, 1 row affected (0.02 sec)
mysql> insert into emp values(2,'ls',19);
Query OK, 1 row affected (0.02 sec)
mysql> insert into emp values(3,'ww',20);
Query OK, 1 row affected (0.01 sec)
2.slave 查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> select * from emp;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | zs | 18 |
| 2 | ls | 19 |
| 3 | ww | 20 |
+------+------+------+
3 rows in set (0.00 sec)
总结
至此主从搭建完成,如搭建过程中出现同步状态错误,需检查同步账号,端口,用户及密码是否在正确。