一、主服务器操作
1.docker-compose文件
version: '3'
services:
mysql:
image: mariadb:10.5.28
container_name: mariadb_1
restart: always
ports:
- 13306:3306
environment:
MARIADB_ROOT_PASSWORD: 123456 # 设置root用户密码
TZ: Asia/Shanghai
LANG: en_US.UTF-8
MARIADB_DATABASE: test01 # 初始化的数据库名称
volumes:
- ./mysql_conf:/etc/mysql
- ./mysql_data:/var/lib/mysql
2.配置文件编写
vim /data/mariadb/mysql_conf/mariadb.conf.d/50-server.cnf
# 启用binlog日志
server-id = 1
log-bin=mysql1
3.重启服务
4.创建一个主从复制用户并授权
#用户授权
[root@mysql53 ~]# mysql
mysql> create user repluser@"%" identified by "123456";
mysql> grant replication slave on *.* to repluser@"%";
create user dbuser@"%" identified by "dbuser";
grant replication slave on *.* to dbuser@"%";
5.查看信息
MariaDB [(none)]> show master status;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql1.000001 | 645 | | |
+---------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000057 | 342 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.000 sec)
二、从服务器操作
1.docker-compose文件
2.配置文件编写
vim /data/mariadb/mysql_conf/mariadb.conf.d/50-server.cnf
# 启用binlog日志
server-id = 2
relay-log=mysql-relay-bin # 中继日志
3.重启服务
4.登录mysql操作
[root@mysql54 ~]# mysql
mysql> change master to
master_host="192.168.88.13",
master_port=13306,
master_user="repluser",
master_password="123456",
master_log_file="mysql1.000001",
master_log_pos=645;
master_host="192.168.88.13", # 主服务器的ip
master_port=13306, # 主服务器的端口
master_user="repluser", # 用户名
master_password="123456", # 密码
master_log_file="mysql1.000001", # 刚刚主查出来的
master_log_pos=645; # 刚刚主查出来的
4.启动从库上的复制相关线程
mysql> start slave ;
| 线程名称 | 核心职责 |
|---|---|
| Slave IO Thread(IO 线程) | 连接主库 → 读取主库的二进制日志(binlog) → 将 binlog 内容写入从库的中继日志(relay log) |
| Slave SQL Thread(SQL 线程) | 读取从库的中继日志 → 解析其中的 SQL 语句 → 在从库上执行这些 SQL,实现数据和主库一致 |
5.查看状态信息
mysql> show slave status \G
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.88.53
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql53.000001
Read_Master_Log_Pos: 667
Relay_Log_File: mysql54-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql53.000001
Slave_IO_Running: Yes //IO线程
Slave_SQL_Running: Yes //SQL线程
三、测试
1.在master服务器建库 、建表、添加用户 、授权
[root@mysql53 ~]# mysql 连接服务
mysql> create database gamedb; 建库
mysql> create table gamedb.user(name char(10) , class char(3)); 建表
mysql> create user plj@"%" identified by "123456"; 创建用户
mysql> grant select,insert,update,delete on gamedb.* to plj@"%" ; 授予权限
2.在slave服务器查看库、表、用户
[root@mysql54 ~]# mysql 连接服务
mysql> show databases; 查看库
mysql> desc gamedb.user; 查看表头
mysql> select user from mysql.user where user="plj"; 查看用户
mysql> show grants for plj@"%" ; 查看权限
3.连接master服务器存储数据
mysql -h192.168.88.53 -uplj -p123456
mysql> insert into gamedb.user values ("yaya","nsd");
mysql> select * from gamedb.user;
+------+-------+
| name | class |
+------+-------+
| yaya | nsd |
+------+-------+
1 row in set (0.01 sec)
mysql>
4.连接从服务器查看数据
[root@mysql50 ~]# mysql -h192.168.88.54 -uplj -p123456
Mysql> select * from gamedb.user;
+------+-------+
| name | class |
+------+-------+
| yaya | nsd |
+------+-------+
Mysql>