mysql-主重复制
1 环境准备
- Mysql 5.7
- Docker Docker version 20.10.13, build a224086
- CentOS Linux release 7.9.2009 (Core)
下载镜像
docker pull mysql:5.7
2 创建mysql master容器
docker run -d --name mysql-master \
-p 3307:3306 \
-v /mydata/mysql-master/config:/etc/mysql/conf.d \
-v /mydata/mysql-master/log:/var/log/mysql \
-v /mydata/mysql-master/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7
3 配置my.cnf
[root@localhost config]# cd /mydata/mysql-master/config/
[root@localhost config]# vi my.cnf
[root@localhost config]# cat my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
4 创建mysql slave容器
docker run -d --name mysql-slave-01 \
-p 3308:3306 \
-v /mydata/mysql-slave-01/config:/etc/mysql/conf.d \
-v /mydata/mysql-slave-01/log:/var/log/mysql \
-v /mydata/mysql-slave-01/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7
5 配置my.cnf
[root@localhost config]# cd /mydata/mysql-master/config/
[root@localhost config]# vi my.cnf
[root@localhost config]# cat my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
6 修改master 节点配置,并重启
[root@localhost config]# cd /mydata/mysql-master/config/
[root@localhost config]# vi my.cnf
[root@localhost config]# cat my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
# 服务器id
server_id=1
# 开启二进制日志
log-bin=mysql-bin
# 需要同步的数据库
binlog-do-db=gmall
# 需要忽略的数据库
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[root@localhost config]# docker restart mysql-master
mysql-master
7 修改slave配置文件,并重启
[root@localhost config]# cd /mydata/mysql-slave-01/config/
[root@localhost config]# vi my.cnf
[root@localhost config]# cat my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=2
log-bin=mysql-bin
binlog-do-db=gmall
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[root@localhost config]# docker restart mysql-slave-01
mysql-slave-01
8 进入master容器,创建同步帐号
# 进入容器
docker exec -it mysql-master /bin/bash
# 进入主库mysql数据库
mysql -u root -p
# 授权root可以远程访问( 主从无关,为了方便我们远程连接mysql)
# 授权远程
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
# 刷新
flush privileges;
# 创建backup用户
# 应先创建新用户
create user 'backup'@'%' identified by '123456';
# 执行授权
grant all privileges on *.* to 'backup'@'%';
# 刷新
flush privileges;
# 授权远程
ALTER USER 'backup'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
# 刷新
flush privileges;
# 查看主库状态
show master status;
操作日志
[root@localhost config]# docker exec -it mysql-master /bin/bash
root@638fe85d87d0:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 542 | gmall | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> create user 'backup'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'backup'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'backup'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1526 | gmall | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
root@638fe85d87d0:/# exit
exit
9 进入slave设置主库连接
# 进入容器
docker exec -it mysql-slave-01 /bin/bash
# 进入mysql数据库
mysql -u root -p
# 设置主库连接
change master to master_host='192.168.200.168', master_user='backup', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=0, master_port=3307;
操作日志
[root@localhost config]# docker exec -it mysql-slave-01 /bin/bash
root@0456078c060a:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='192.168.200.168', master_user='backup', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=0, master_port=3307;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> quit
Bye
root@0456078c060a:/# exit
exit
10 进入slave容器设置启动从库同步
# 将主库binlog清除一下,这样它的位置就从0开始了
purge master logs to'mysql-bin.000001';
# 开始同步
start slave;
# 停止同步
# stop slave;
# 查看同步状态
show slave status\G;
操作日志
[root@localhost config]# docker exec -it mysql-slave-01 /bin/bash
root@0456078c060a:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> purge master logs to'mysql-bin.000001';
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.168
Master_User: backup
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1083
Relay_Log_File: 0456078c060a-relay-bin.000004
Relay_Log_Pos: 1296
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,sys,information_schema,performance_schema
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: 1083
Relay_Log_Space: 3095
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: 1
Master_UUID: 0798d77a-b615-11ec-b6ad-0242ac110002
Master_Info_File: /var/lib/mysql/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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
11 重启master、slave容器
[root@localhost config]# docker restart mysql-master
mysql-master
[root@localhost config]# docker restart mysql-slave-01
mysql-slave-01
12 测试同步
使用客户端连接工具分别连接master、slave容器数据库
在master创建gmall数据我们可以发现slave自动同步创建数据库