-
备份的三种类型
-
热备份
-
逻辑备份
-
物理备份
-
情景
入职企业,发现企业架构为一主多从,但是两台从服务器和主库不同 步,但是每天会全库北方主服务器上的数据到从服务器,由于数据量 不是很大,所以没有人处理主从不同步的问题。
-
技术
-
熟悉mysql数据库常见主从架构
-
理解mysql主从架构实现原理
-
掌握mysql主从架构的搭建
-
业务场景
-
企业对数据安全性越来越重视,由于常规备份不能实时记录 数据库的所有状态,为了能够保障数据库实时备份冗余,希 望将现有的单机数据库变成双机热备
-
目标
-
了解什么mysql的replication
-
理解mysql的replication的架构原理
-
掌握mysql基本复制架构 m-s
-
了解和掌握基于GTID复制特点以及搭建
集群概述
-
集群主要类型
-
高可用集群 High Avaible Cluster HA cluster
-
高可用集群是指通过特殊软件,把独立的服务器连接起来, 组成一个能够提供故障切换(Fail Over)功能的集群
-
高可用标准
-
常用的集群架构
-
mysql replication
-
mysql cluster
-
mysql group replication MGR
-
Maradb Galera CLuster
-
MHA|keepalived|HeatBeat|Lvs, Haproxy等技术构建高可用 集群
复制原理
-
replication,可以实现将数据从一台数据库服务器(mster)复制 到多台数据库服务器slave
-
默认情况下, replication属于异步复制,所以无需长连接
-
工作原理
-
master 主服务器
-
slave 从服务器
-
描述
-
slave端的io线程发送请求给master端的binlog dump线 程
-
master端的binlog dump线程获取二进制日志信息(文 件名和位置信息)发送给slave端的io线程
-
slave端的io线程获取到内容,依次写到slave端relay log (中继日志)并把master端的binlog文件名和位置记录到master .info里头,
-
slave端的sql线程检测到relaylog中的内容更新,就会解 析relaylog中的更新内容,并执行这些操作,从而达到和master端数据一致
复制架构体系
-
双机热备 主从复制
-
默认情况下 master接收读写,从服务器只接受读
-
级联复制
-
可以分担读的压力
-
中间服务器出现故障,就瘫痪了
-
并联复制 一主多从
-
解决单点故障
-
承担更多的读的压力
-
从服务器都从主服务器读取数据, master服务器压力大
-
双主复制
-
看起来可以同时接收读写,实际运作中只有一台服务器在工 作,另外一台只接受读
前期准备,克隆机器,设置ip,修改主机名称,关闭
NEtworkManager,防火墙 selinux,配置yum源,配置时间同步
master服务器中添加
sed -i '$a192 .168 .137.80
master.msyql.yuanyu.zhangmin' /etc/hosts
slave服务器中添加
sed -i '$a192 .168 .137.90
slave.msyql.yuanyu.zhangmin' /etc/hosts
时间同步是非常重要的设置
ntpdate cn.ntp.org .cn
主从复制之master配置
1.编写mysql安装脚本
-
上传mysql安装包到mstaer和slave
-
编写脚本
#!/bin/bash
yum list installed |grep libaio if [ $? ne 0 ]; then
yum -y install libaio
fi
echo libaio yes
rm -rf /etc/my.cnf
echo remo my.cnf yes
tar -xf mysql-8.0.33-linux-glibc2 .12 - x86_64.tar .xz
echo tar zx yes
cp - r ~/mysql-8.0.33-linux-glibc2 .12-x86_64 /usr/local/mysql
echo copy file to /usr/local/mysql yes
mkdir /usr/local/mysql/mysql-files echo mysql-files yes
grep mysql /etc/passwd
useradd - r -s /sbin/nologin mysql
chown mysql:mysql /usr/local/mysql/mysql-files chmod 750 /usr/local/mysql/mysql-files
/usr/local/mysql/bin/mysqld --initialize -- user=mysql --basedir=/usr/local/mysql/
/usr/local/mysql/bin/mysql_ssl_ rsa_setup -- datadir=/usr/local/mysql/data
cp /usr/local/mysql/support-files/mysql .server /etc/init.d/mysql8
sed -i '$aexport
PATH=/usr/local/mysql/bin:$PATH' /etc/profile source /etc/profile
- 编写配置文件
vim /usr/local/mysql/my.cnf
mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data socket=/tmp/mysql .sock
port=3306
log-error=/usr/local/mysql/data/db01 - master .err
log-bin=/usr/local/mysql/data/binlog server-id=10
character_set_server=utf8mb4
- 重启服务
root@mysql001 \~\]# service mysql8 restart Shutting down MySQL . . SUCCESS! Starting MySQL.Logging to '/usr/local/mysql/data/db01-master.err' . .. SUCCESS! 5. 设置开机启动 \[root@mysql001 \~\]# chkconfig --add mysql8 \[root@mysql001 \~\]# chkconfig mysql8 on \[root@mysql001 \~\]# chkconfig 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies \[target\]'。 ****主从复制之slave从服务器软件的安装**** 1. 不需要初始化 相对于主服务器的安装与配置,从服务器不需要初始化,他的数 据都来自于主服务器,其他都一样 2. 查看文件 \[root@slave \~\]# ls anaconda-ks.cfg mysql-8.0.33-linux-glibc2 .12 - x86_64.tar .xz initserver.sh mysql .sh 3. 脚本 vim mysql .sh #!/bin/bash yum list installed \|grep libaio if \[ $? ne 0 \]; then yum -y install libaio fi echo libaio yes rm -rf /etc/my.cnf echo remo my.cnf yes tar -xf mysql-8.0.33-linux-glibc2 .12 - x86_64.tar .xz cp - r \~/mysql-8.0.33-linux-glibc2 .12-x86_64 /usr/local/mysql echo copy file to /usr/local/mysql yes mkdir /usr/local/mysql/mysql-files echo mysql-files yes mkdir /usr/local/mysql/mysql-files echo mysql-files yes grep mysql /etc/passwd echo remo my.cnf yes tar -xf mysql-8.0.33-linux-glibc2 .12 - x86_64.tar .xz cp - r \~/mysql-8.0.33-linux-glibc2 .12-x86_64 /usr/local/mysql echo copy file to /usr/local/mysql yes mkdir /usr/local/mysql/mysql-files echo mysql-files yes grep mysql /etc/passwd useradd - r -s /sbin/nologin mysql chown mysql:mysql /usr/local/mysql/mysql-files chmod 750 /usr/local/mysql/mysql-files #/usr/local/mysql/bin/mysqld --initialize -- user=mysql --basedir=/usr/local/mysql/ cp /usr/local/mysql/support-files/mysql .server /etc/init.d/mysql8 sed -i '$aexport PATH=/usr/local/mysql/bin:$PATH' /etc/profile source /etc/profile 4. 配置文件 \[root@slave \~\]# ls /usr/local/mysql/ bin include LICENSE my.cnf README support-files docs lib man mysql-files share vim \[mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data socket=/tmp/mysql .sock
port=3310
log-error=/usr/local/mysql/data/db01-slave .err relay-log=/usr/local/mysql/data/relaylog
server-id=11
character_set_server=utf8mb4
主从复制之数据同步
-
master服务器同步到slave服务器
-
停用master服务器msyql服务 service mysql8 stop
-
master删除/usr/local/mysql/data/auto.cnf 每安装一个
mysql软件, data数据目录都会产生一个auto.cnf文件,里面 是一个唯一性编号,相当于每个人的身份证编号
root@mysql001 \~\]# ls /usr/local/mysql/data/auto.cnf /usr/local/mysql/data/auto.cnf \[root@mysql001 \~\]# cat /usr/local/mysql/data/auto.cnf \[auto
server-uuid=f6421989-5330-11ef-974f- 000c29ce78bb
root@mysql001 \~\]# rm -rf /usr/local/mysql/data/auto.cnf 3. master和slave都安装rsync yum -y install rsync 4. 同步master中的data到slave对应位置 rsync -av /usr/local/mysql/data root@10 .1 .1 .110:/usr/local/mysql/ 5. 启动主服务器和从服务器 \[root@mysql001 \~\]# service mysql8 start Starting MySQL . . SUCCESS! \[root@slave \~\]# service mysql8 start Starting MySQL.Logging to '/usr/local/mysql/data/db01-slave.err' . . SUCCESS! 6. 登录从服务器 \[root@mysql001 \~\]# /usr/local/mysql/bin/mysql -P3310 - pZhang_Min_666 mysql: \[Warning\] Using a password on the command line interface can be insecure . Welcome to the MySQL monitor . Commands end with ; or \\g . Your MySQL connection id is 8 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000 , 2023 , 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 . ****无法启动的几种情况**** 1. /etc/init.d/mysql8不存在,可能没有复制 2. mysql没有安装在/usr/local/mysql 3. my.cnf导致无法启动,格式 1. 安装路径 2. 数据目录 3. 套接字 4. 错误日志到底放在data里头 5. mysql账户无法写入/usr/local/mysql/ 6. 将错误日志放在data就没有权限文件 7. 直接修改/usr/local/mysql的权限 ****主从复制的实现**** 1. master创建授权账号 mysql\> create user 'slave'@'10.1.1.%' identified by 'slave_123' ; Query OK , 0 rows affected (0.02 sec) mysql\> grant replication slave on \* .\* to 'slave'@'10.1.1.%'; Query OK , 0 rows affected (0.01 sec) mysql\> flush privileges; 2. 在master中锁表,然后查看二进制文件和位置 mysql\> flush tables with read lock; mysql\> show master status; + - - - - - - - - - - - - - - -+ - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - - -+ \| File \| Position \| Binlog_Do_DB \| Binlog_Ignore_DB \| Executed_Gtid_Set \| + - - - - - - - - - - - - - - -+ - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - - -+ \| binlog.000003 \| 707 \| \| \| \| + - - - - - - - - - - - - - - -+ - - - - - - - - - -+ - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - - - - - - -+ 1 row in set (0.00 sec) 3. 在从服务器中,使用change master to 指定主服务器,并实现数 据同步 \[root@slave \~\]# /usr/local/mysql/bin/mysql - P3310 -pZhang_Min_666 mysql : \[Warning\] Using a password on the command line interface can be insecure . Welcome to the MySQL monitor . Commands end with ; or \\g . Your MySQL connection id is 8 Server version : 8.0.33 MySQL Community Server - GPL Copyright (c) 2000 , 2023 , 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= '10 .1 .1 .100' , -\> master_user= 'slave' , -\> master_password= 'slave_123' , -\> master_port=3306 , -\> master_log_file= 'binlog.000003' , -\> master_log_pos=707; Query OK , 0 rows affected , 9 warnings (0.01 sec) change master to master_host= '10 .1 .1 .100' , master_user= 'slave' , master_password= 'slave_123' , master_port=3306 , master_log_file= 'binlog.000004' , master_log_pos=1092; 4. 启动slave数据同步,查看状态 mysql\> start slave; mysql\> show slave status\\G 5. 常见文件 1. change-master-to写错 stop slave reset slave change master to ... 6. 解锁文件 mysql\> unlock tables; Query OK , 0 rows affected (0.00 sec) ****8.0需要**** ****ssl非对称加密**** 1. 获得远程master主机的公钥 mysql -uzhangmin -pabc_123 -h <192.168.71.166> - P3306 --get-server-public-key quit 2. 登录slave服务器本地的数据库 mysql -pZhang_Min_666 -P3310 3. 停用slave服务,重新设置slave服务 mysql\> stop slave; mysql\> reset slave; mysql\> change master to master_host='<192.168.71.166> ', master_user='zhangmin', master_password='abc_123', master_port=3306, master_log_file='binlog.000006',master_log_pos= 866; 4. 启动slave服务 mysql\> start slave mysql\> show slave status\\G