index
启动 MySQL 服务
- 创建 network
shell
# 172.18.0.1
docker network create --subnet 172.18.0.0/16 --gateway 172.18.0.1 mysql- 启动 MySQL 服务
shell
docker pull mysql:5.7.34
docker run -itd --name mysql1 -p 3316:3306 --network mysql --ip 172.18.0.101 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.34
docker run -itd --name mysql2 -p 3326:3306 --network mysql --ip 172.18.0.102 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.34- 查看 MySQL 运行状态
shell
mysql --version
# mysql Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using EditLine wrapper
mysqld --version
# mysqld Ver 5.7.34 for Linux on x86_64 (MySQL Community Server (GPL))\配置 MySQL 主从
- binlog 三种模式
- binlog 校验模式
- 默认 MySQL 配置文件路径
shell
cat /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf- 修改 MySQL 配置
/etc/mysql/mysql.conf.d/mysqld.cnf
conf
[mysqld]
; 不同 Server 需要不同 id
server_id=101
; 启动GTID
gtid-mode=ON
; ON-强制执行GTID一致性
enforce-gtid-consistency=ON
; 开启 MySQL binlog 记录, 使用 mysql-bin 作为binlog文件名前缀
log-bin=mysql-bin
; 指定 binlog 日志模式
; ROW:以行级别的更改为单位记录。
; STATEMENT:以 SQL 语句为单位记录更改操作。
; MIXED:默认情况下使用语句级别记录,但在某些情况下会自动切换到行级别记录1。
binlog_format=ROW
; 关闭 binlog 二进制日志完整性校验
; NONE:不进行校验。
; CRC32:使用 CRC32 校验算法。
; CRC32C:使用 CRC32C 校验算法。
binlog_checksum=NONE- 检查 MySQL 主节点配置状态
sql
show variables like 'server_id'\G| Variable_name | Value |
|---|---|
| server_id | 101 |
sql
show master status;| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
|---|---|---|---|---|
| mysql-bin.000001 | 150 |
- 创建同步用户
需要确认用户拥有 REPLICATION SLAVE 权限
shell
CREATE USER 'replica_user'@'%' IDENTIFIED BY 'your_password';
GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';
FLUSH PRIVILEGES;- 建立主从关系
sql
change master to
master_host ='172.18.0.101',
master_port = 3306,
master_user ='root',
master_password ='123456',
master_auto_position =1;- 启动主从同步
sql
start slave;- 检查 MySQL 从节点配置状态
sql
show master status;| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
|---|---|---|---|---|
| mysql-bin.000001 | 150 |
sql
show slave status;| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Waiting for master to send event | 172.18.0.101 | root | 3306 | 60 | mysql-bin.000001 | 150 | 1b7cd647eb37-relay-bin.000002 | 355 | mysql-bin.000001 | Yes | Yes | 0 | 0 | 150 | 561 | None | 0 | No | 0 | No | 0 | 0 | 101 | 926acfdd-fa32-11ee-829d-0242ac120065 | /var/lib/mysql/master.info | 0 | null | Slave has read all relay log; waiting for more updates | 86400 | 1 |
确认 当 Slave_IO_Running 与 Slave_SQL_Running 都为 YES 时,则代表主从同步运行正常
数据库全量备份 - mydumper
https://github.com/mydumper/mydumper apt-get install mydumper
shell
mkdir -p /tmp/mysql_backup/
mydumper -u root -p 123456 -h 127.0.0.1 -P 3306 -o /tmp/mysql_backup/ -F 4 -t 15 -v 3 -L /tmp/mysql_backup.log > /dev/null 2>&1
ls -lha /tmp/mysql_backup/核心文件 ./metadata
docker cp mysql1:/tmp /mysql1_tmp docker cp C:\mysql1_tmp\mysql_backup\ mysql2:/tmp
- 数据库全量恢复 - myloader
shell
myloader -u root -p 123456 -h 127.0.0.1 -P 3306 -o -d /tmp/mysql_backup/ -t 8 -v 3- 设置主从同步
sql
show master status;
show slave status;
stop slave;
reset master;
reset slave;
# from meatdate
set @@GLOBAL.GTID_PURGED = '926acfdd-fa32-11ee-829d-0242ac120065:1-9';
change master to
master_host ='172.18.0.101',
master_port = 3306,
master_user ='root',
master_password ='123456',
master_auto_position =1;
set sql_log_bin = 1;
start slave;
show master status;
show slave status;sql_log_bin = 0 关闭将 sql 记录到 binlog =1 时开启记录到 binlog
TODO
MySQL 主从建立
- 修改主从 GTID
- 跳过指定事务
MySQL 全量备份
MySQL 全量恢复
MySQL 增量备份与恢复 - 主从同步 mysqladmin flush-logs