Linux——mysql主从复制与读写分离
一,理解什么是mysql主从复制
1,mysql支持的复制类型
- 基于语句的复制:
在主服务器上执行的sql语句,在从服务器上执行同样的语句,mysql默认采用基于语句的复制,效率比较高。
- 基于行的复制:
把改变的内容复制过去,而不是把命令在从服务器上执行一遍。
- 基于二进制文件的复制:
完全基于语句复制,binlog日志文件中记录原始 SQL 语句(默认模式)。
2,mysql主从复制的工作流程
- 在每个事务更新数据完成之前,Master 将这些改变记录进二进制日志。写入二进制日志完成后,Master 通知存储引擎提交事务。
- Slave 将 Master 的 Binary log 复制到其中继日志(Relay log)。首先,Slave 开始一个工作线程--I/0 线程,I/0 线程在 Master 上打开一个普通的连接,然后开始 Binlog dump process。Binlog dump process 从 Master 的二进制日志中读取事件,如果已经跟上 Master,它会睡眠并等待 Master 产生新的事件。I/0 线程将这些事件写入中继日志。
- SQL slave thread(SQl 从线程)处理该过程的最后一步。SQL 线程从中继日志读取事件,并重放其中的事件而更新 Slave数据,使其与 Master 中的数据保持一致。只要该线程与 I/0 线程保持一致,中继日志通常会位于0S的缓存中,所以中继日志的开销很小。复制过程有一个很重要的限制,即复制在S1ave 上是串行化的,也就是说 Master 上的并行更新操作不能在 Slave 上并行操作。
二,配置mysql主从复制
1,准备工作
dnf -y install ntpdate ##安装时间同步软件包
date ##查看时间是否同步systemctl stop firewalld ##关闭防火墙
setenforce 0 ##关闭linux内核##修改mysql配置文件,添加以下内容(mysqld模块中添加)
[root@localhost local]# vim /etc/my.cnf
log-bin=/usr/local/mysql/mysql-bin ##指定二进制文件位置
server-id=1 ##设置mysql服务id
binlog-format=MIXED ##用于控制二进制日志的记录格式为混合模式systemclt restart mysqld ##重启mysql数据库
2,登陆mysql进行操作
create user 'myslave'@'%' identified by 'pwd123'; ##创建用户grant replication slave on *.* to 'myslave'@'%'; ##给复制权限
alter user 'myslave'@'%' identified with mysql_native_password by 'pwd123'; ##为myslave设置密码为pwd123.flush privileges; ##更新用户信息mysql> show master status; ##查看主服务区状态信息,等会要用
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 342 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3,在从设备进行操作
[root@localhost local]# vim /etc/my.cnf
server-id=2 ##在从服务器添加id(不能与主服务器重复)
systemclt restart mysqld ##重启mysql数据库##在从服务器进行操作
change master to master_host='192.168.10.101',master_user='myslave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1438;start slave; ##启动slave
show slave status\G ##查看主从状态Slave_IO_Running: Yes ##找到此部分为两个yes就成功了Slave_SQL_Running: Yes
4,验证
##在主服务器创建名为auth的数据库
mysql> create database auth;##查看从服务器是否同步成功
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| auth |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
三,配置mysql主主复制
1,配置从服务器
##在从服务器添加
[root@bogon ~]# vim /etc/my.cnf
log-bin=/usr/local/mysql/mysql-bin
binlog-format=MIXEDsystemclt restart mysqld ##重启mysql数据库create user 'myslave'@'%' identified by 'pwd123'; ##创建用户
grant replication slave on *.* to 'myslave'@'%'; ##修改权限
alter user 'myslave'@'%' identified with mysql_native_password by 'pwd123'; ##该密码
flush privileges; ##刷新show master status; ##查看状态信息
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1149 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)stop slave; ##一定要关闭slave
2,配置双主连接
##在101操作
change master to master_host='192.168.10.102',master_user='myslave',master_ppassword='pwd123',master_log_file='mysql-bin.000001',master_log_pos=1149; ##查看102的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1149 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)##在102操作
change master to master_host='192.168.10.101',master_user='myslave',master_password='pwd123',master_log_file='mysql-bin.000001',master_log_pos=342; ##查看101的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 342 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)