一、案例概述
1.1 案例概述-1
■ 在企业网站中,后端MySQL数据库中只有一台时,会有以下问题
- 单点故障,服务不可用
- 无法处理大量的并发数据请求
- 数据丢失一大灾难
1.2 案例概述-2
■ 更高级解决方案
- 通过主从复制的方式来同步数据,再通过读写分离来提升数据库的并发负载能力
1.3 案例前置知识点-1
■ MySQL主从复制的类型
- 基于语句的复制(默认)
◇ 在主服务器上执行的语句,从服务器执行同样的语句 - 基于行的复制
◇ 把改变的内容复制到从服务器 - 混合类型的复制
◇一旦发现基于语句无法精确复制时,就会采用基于行的复制
1.4 案例前置知识点-2
■ 主从复制的工作过程
- 在每个事务更新数据完成之前,Master在二进制日志记录这些改变。写入二进制日志完成后,Master通知存储引擎提交事物
- Slave将Master的Binary log复制到中继日志。首先Slave开始一个工作线程——I/O线程,I/O线程在Master上打开一个普通的连接,然后开始Binlog dump process。Binlog dump process从Master的二进制日志中读取事件,如果已经跟上Master,他会睡眠并等待Master产生新的事件。I/O线程将这些事件写入中继日志。
- SQL slave thread(SQL从线程)处理该过程的最后一步。SQL线程从中继日志读取事件,并重放其中的事件而更新Slave的数据,使其与Master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。
- 复制过程有一个很重要的限制,即复制在Slave上是串行化的,也就是说Master上的并行更新操作不能再Slave上并行操作。
二、案例操作
#########案例环境#############
IP地址 主机 操作系统
20.0.0.6 master centos-7.6-x86_64
20.0.0.3 Slave1 centos-7.6-x86_64
20.0.0.5 Slave2 centos-7.6-x86_64
###maste、slave1、slave2###
关闭防火墙、关闭核心防护,###不会的可以看看我前面的博客###
###建立时间同步环境###
1、在主机Master搭建时间同步服务器NTP(20.0.0.6)
[root@localhost ~]# yum -y install ntp ###如果不是最小化安装,就不用yum安装ntp
[root@localhost ~]# vi /etc/ntp.conf
###删除下面四行###
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
###添加这两行###
server 127.127.1.0
fudge 127.127.1.0 stratum 8
[root@localhost ~]# service ntpd restart
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# systemctl enable ntpd
2、在从服务器上配置NTP同步
登录到20.0.0.3
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.6
[root@localhost ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.6 >>/var/log/ntpdate.log
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# touch /var/log/ntpdate.log
[root@localhost ~]# tail -f /var/log/ntpdate.log ###动态查看更新日志文件
登录到20.0.0.5
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.6
[root@localhost ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.6 >>/var/log/ntpdate.log
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# touch /var/log/ntpdate.log
[root@localhost ~]# tail -f /var/log/ntpdate.log ###动态查看更新日志文件
####安装MySQL数据库,看我前面的博客,有详解!!!
#########登录Master主服务器配置20.0.0.6##########
[root@localhost ~]# vi /etc/my.cnf ###在原来server-id=1的地方修改成11后面新增log_bin=master-bin log-slave-updates=true###
server-id = 11
log_bin = master-bin
log-slave-updates = true
[root@localhost ~]# systemctl restart mysqld ###重启数据库
登录Master数据库给从服务器授权
[root@localhost ~]# mysql -uroot -p
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'abc123';
mysql> flush privileges;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 599 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#####登录salver1从服务器配置20.0.0.3#####
[root@localhost ~]# vi /etc/my.cnf ###在原来server-id=1的地方修改成22 后面新增relay-log=relay-log-bin relay-log-index=slave-relay-bin.index###
server-id = 22
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld ###重启数据库
登录slave数据库配置同步 ###注意下这边的master_log_file='master-bin.000001',master_log_pos=601;要和Master数据库信息不一致,不一致的话要更改先stop slave;然后更改同步信息
[root@localhost ~]# mysql -uroot -p
mysql> change master to master_host='20.0.0.6',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=599;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 20.0.0.6
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 604
Relay_Log_File: relay-log-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: yes ###开启
Slave_SQL_Running: Yes ###开启
....................略........................
#####登录salver2从服务器配置20.0.0.5#####
[root@localhost ~]# vi /etc/my.cnf
server-id = 33
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -uroot -p
mysql> change master to master_host='20.0.0.6',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=599;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 20.0.0.6
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 601
Relay_Log_File: relay-log-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: yes ###开启
Slave_SQL_Running: Yes ###开启
###验证主从复制效果###
###主的建库,从服务器会同步###
登录20.0.0.6
[root@localhost ~]# mysql -uroot -p
mysql> create database zk;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zk |
+--------------------+
5 rows in set (0.00 sec)
登录20.0.0.5
[root@localhost ~]# mysql -uroot -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zk |
+--------------------+
5 rows in set (0.00 sec)