1. 主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
- 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
- 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
1.1 主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2 主从形式
![image.png](https://img-blog.csdnimg.cn/img_convert/b5ca7a108a6e1dcca847c70088d18545.png#clientId=u70904c87-9eee-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u2cba5052&margin=[object Object]&name=image.png&originHeight=251&originWidth=528&originalType=url&ratio=1&rotation=0&showTitle=false&size=76457&status=done&style=none&taskId=u2a061a45-6dfc-4bcb-963d-703e956630d&title=)
- 一主一从
- 主主复制
- 一主多从—扩展系统读取的性能,因为读是在从库读取的
- 多主一从—5.7开始支持
- 联级复制
2. 主从复制原理
![image.png](https://img-blog.csdnimg.cn/img_convert/4e615356724a8b19ec70ebcfaa516820.png#clientId=u70904c87-9eee-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uc5126099&margin=[object Object]&name=image.png&originHeight=270&originWidth=582&originalType=url&ratio=1&rotation=0&showTitle=false&size=66153&status=done&style=none&taskId=u4e61ee3b-e600-48fc-80f2-f786c0295b3&title=)
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3. 主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.141.135 | mysql-5.7 | 有数据 |
从数据库 | 192.168.141.134 | mysql-5.7 | 无数据 |
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7版本
主从数据库重新安装
#确保从数据库与主数据库里的数据一样
#主库
[root@localhost ~]# yum -y install mariadb-server
CentOS-8.5.2111 - Base - mirrors.aliyun.com 21 kB/s | 3.9 kB 00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 15 kB/s | 1.5 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 40 kB/s | 4.3 kB 00:00
Dependencies resolved.
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled #修改这里
[root@localhost ~]# setenforce 0
#从库
[root@localhost ~]# hostnamectl set-hostname kefu
[root@localhost ~]# bash
[root@kefu ~]# yum -y install mariadb-server
CentOS Stream 8 - AppStream 2.4 MB/s | 24 MB 00:09
CentOS Stream 8 - BaseOS 9.8 MB/s | 25 MB 00:02
CentOS Stream 8 - Extras 25 kB/s | 18 kB 00:00
Dependencies resolved.
[root@kefu ~]# systemctl stop firewalld.service
[root@kefu ~]# vim /etc/selinux/config #主库一样的操作
[root@kefu ~]# setenforce 0
#两库相同操作
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@localhost ~]#
//同时修改密码
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password=password('123456');
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
//先查看主库有哪些库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.002 sec)
MariaDB [(none)]>
//再查看从库有哪些库,因为是刚开始安装,数据库应该是一样的
MariaDB [(none)]> show databases;
+----------