1. 主从同步原理
1.1 基本原理
slave会从master读取binlog来进行数据同步
1.2 具体步骤
- (1): master将数据改变记录到二进制日志binlog中;
- (2): slave上执行
start slave
命令后,slave会创建一个IO线程,用来连接master,请求master中的binlog; - (3): 当slave连接master时,master会创建一个log dump线程,用于发送binlog内容。在读取binlog的内容的操作中,会对主节点上的binlog加锁,当读取完成并发送给从服务器后解锁;
- (4): IO线程接受主节点发送过来的更新后,保存到中继日志relay log中;
- (5): slave的SQL线程,读取relay log日志,并解析成具体操作,从而实现主从操作一致,最终数据一致。
2. 开始一主两从配置
2.1 服务器规划
容器名 | 端口 | 角色 |
---|---|---|
atguigu-mysql-master | 3306 | master |
atguigu-mysql-slave1 | 3307 | slave1 |
atguigu-mysql-slave2 | 3308 | slave2 |
2.2 环境需要
- docker
- 防火墙关闭
#关闭docker
systemctl stop docker
#关闭防火墙
systemctl stop firewalld
#启动docker
systemctl start docker
2.3 配置master
- docker拉取mysql 5.7镜像
docker pull mysql:5.7
- docker创建并启动master
docker run -d \
-p 3306:3306 \
-v /atguigu/mysql/master/conf:/etc/mysql/conf.d \
-v /atguigu/mysql/master/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name atguigu-mysql-master \
mysql:5.7
- 创建MySQL主服务器配置文件
vim /atguigu/mysql/master/conf/my.cnf
写入如下内容
[mysqld]
# 服务器唯一id,默认值1
server-id=1
# 设置日志格式,默认值ROW
binlog_format=STATEMENT
# 二进制日志名,默认binlog
log-bin=binlog
- 重启master节点
docker restart atguigu-mysql-master
- 进入master节点内部
#进入容器:env LANG=C.UTF-8 避免容器中显示中文乱码
docker exec -it atguigu-mysql-master env LANG=C.UTF-8 /bin/bash
#进入容器内的mysql命令行
mysql -uroot -p
#修改默认密码校验方式
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
- master中创建slave用户
-- 创建slave用户
CREATE USER 'atguigu_slave'@'%';
-- 设置密码
ALTER USER 'atguigu_slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
-- 授予复制权限
GRANT REPLICATION SLAVE ON *.* TO 'atguigu_slave'@'%';
-- 刷新权限
FLUSH PRIVILEGES;
- 查看master状态
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 3740 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
记下File
和Position
的值。执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化。
2.4 配置slave1
- docker启动slave1
docker run -d \
-p 3307:3306 \
-v /atguigu/mysql/slave1/conf:/etc/mysql/conf.d \
-v /atguigu/mysql/slave1/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name atguigu-mysql-slave1 \
mysql:5.7
- 创建mysql从服务器配置文件
vim /atguigu/mysql/slave1/conf/my.cnf
写入如下内容
[mysqld]
# 服务器唯一id,每台服务器的id必须不同,如果配置其他从机,注意修改id
server-id=2
# 中继日志名,默认xxxxxxxxxxxx-relay-bin
relay-log=relay-bin
# 设置普通用户为只读,该项如果不设置,表示普通用户slave可读可写
read_only=1
# 设置超级用户为只读,该项如果不设置,表示slave可读可写
super_read_only=1
# 指定要同步的数据库,必须的
replicate-do-db=testdb
- 重启slave1
docker restart atguigu-mysql-slave1
- 进入slave1节点内部
#进入容器:env LANG=C.UTF-8 避免容器中显示中文乱码
docker exec -it atguigu-mysql-slave1 env LANG=C.UTF-8 /bin/bash
#进入容器内的mysql命令行
mysql -uroot -p
#修改默认密码校验方式
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
- 配置主从关系
CHANGE MASTER TO MASTER_HOST='xxx.xxx.xxx.xxx',
MASTER_USER='atguigu_slave',MASTER_PASSWORD='123456', MASTER_PORT=3306,
MASTER_LOG_FILE='binlog.000001',MASTER_LOG_POS=3740;
注意这里的MASTER_LOG_FILE
与MASTER_LOG_POS
是上面master的File
和Position
- 启动主从同步
START SLAVE;
- 查看状态
注意:Slave_IO_Running
与Slave_SQL_Running
两个线程都必须全为Yes
,则说明主从配置成功!
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.24.10.198
Master_User: atguigu_slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 3740
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 3947
Relay_Master_Log_File: binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: testdb
Replicate_Ignore_DB:
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: 3740
Relay_Log_Space: 4148
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: 00fee855-5058-11ed-b0c0-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)
ERROR:
No query specified
2.5 配置slave2
步骤同slave1,注意修改配置文件中的server-id
文章来源:https://www.toymoban.com/news/detail-406623.html
3. 一些其它的命令
- 查看只读开关
- 查看通用日志开关或路径
mysql> show global variables like '%read_only%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| super_read_only | ON |
| transaction_read_only | OFF |
| tx_read_only | OFF |
+-----------------------+-------+
5 rows in set (0.00 sec)
mysql> show variables like 'general_log%';
+------------------+---------------------------------+
| Variable_name | Value |
+------------------+---------------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/eb30519b4804.log |
+------------------+---------------------------------+
2 rows in set (0.00 sec)
mysql>
4. SpringBoot实现
https://blog.csdn.net/qq_39198749/article/details/127577547?spm=1001.2014.3001.5502文章来源地址https://www.toymoban.com/news/detail-406623.html
到了这里,关于MySQL:一主两从架构(读写分离)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!