目录
主从简介
主从作用
实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务
主从形式
一主一从
主主复制
一主多从—扩展系统读取的性能,因为读是在从库读取的
多主一从—5.7开始支持
联级复制
主从复制原理
主从复制步骤:
主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
从库生成两个线程,一个I/O线程,一个SQL线程
I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
传统MySQL主从复制配置
主从复制需要两台虚拟机,分别为zzh(主服务器)和wrq(从服务器)。
mysql二进制安装
[root@zzh ~]# cd /usr/src
[root@zzh src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz //下载二进制格式的mysql软件包
--2022-08-01 20:28:17-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 223.119.203.77, 2600:140e:6:79d::2e31, 2600:140e:6:7a7::2e31
mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz 100%[=======================================================================================================>] 643.57M 4.65MB/s in 4m 42s
2022-08-01 20:33:00 (2.28 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
[root@zzh src]# groupadd -r mysql
[root@zzh src]# useradd -M -s /sbin/nologin -g mysql mysql //创建用户和组
[root@zzh src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@zzh src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@zzh src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.38-linux-glibc2.12-x86_64 sbin share src
[root@zzh src]# cd /usr/local/
[root@zzh local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/' //解压软件至/usr/local/
[root@zzh local]# chown -R mysql.mysql /usr/local/mysql
[root@zzh local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Aug 1 20:35 /usr/local/mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/ //修改目录/usr/local/mysql的属主属组
[root@zzh local]# ls /usr/local/mysql
LICENSE README bin docs include lib man share support-files
[root@zzh local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@zzh local]# . /etc/profile.d/mysql.sh
[root@zzh local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin //添加环境变量
[root@zzh local]# cd /usr/local/mysql
[root@zzh mysql]# mkdir /opt/data
[root@zzh mysql]# chown -R mysql.mysql /opt/data/
[root@zzh mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 1 20:36 data //建立数据存放目录
[root@zzh mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/ //初始化数据库
2022-08-02T00:38:52.012074Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-02T00:38:52.892419Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-02T00:38:53.025109Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-02T00:38:53.043118Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7bd5e07b-11fb-11ed-bfd4-000c2920bee5.
2022-08-02T00:38:53.044292Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-08-02T00:38:53.200529Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T00:38:53.200558Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T00:38:53.201549Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-02T00:38:53.237207Z 1 [Note] A temporary password is generated for root@localhost: bGod:lRuM9_p //这会生成一个临时随机密码
[root@zzh ~]# cat /etc/my.cnf //生成配置文件
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@zzh ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@zzh ~]# vim /etc/init.d/mysqld //配置服务启动脚本
46 basedir=/usr/local/mysql
47 datadir=/opt/data
[root@zzh ~]# /etc/init.d/mysqld start //启动mysql
Starting MySQL.Logging to '/opt/data/zzh.err'.
. SUCCESS!
[root@wrq ~]# 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@zzh ~]# /usr/local/mysql/bin/mysql -uroot -p
Enter password: //使用临时密码登录//修改密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38
Copyright (c) 2000, 2022, 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> set password = password('zhaozihao');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
//从服务器
[root@wrq ~]# cd /usr/src
[root@wrq src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-08-01 20:28:15-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 223.119.203.77, 2600:140e:6:79d::2e31, 2600:140e:6:7a7::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|223.119.203.77|:443... connected.
mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz 100%[=======================================================================================================>] 643.57M 4.73MB/s in 4m 44s
2022-08-01 20:33:00 (2.27 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
//同上操作
[root@wrq ~]# 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@wrq ~]# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38
Copyright (c) 2000, 2022, 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> set password = password('zhaozihao');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql主从配置
[root@zzh ~]# systemctl stop firewalld.service
[root@zzh ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zzh ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config
[root@zzh ~]# setenforce 0
[root@wrq ~]# systemctl stop firewalld.service
[root@wrq ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@wrq ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config
[root@wrq ~]# setenforce 0 //主从服务器都关闭防火墙和selinux
主服务器配置
[root@zzh ~]# vim /etc/my.cnf //在主服务器上配置主从复制
log_bin=mysql_bin //开启二进制日志
server_id=10 //服务id号,不可重复,为0则拒绝从服务器连接
[root@zzh ~]# systemctl restart mysqld.service //重启mysql服务
[root@zzh ~]# /usr/local/mysql/bin/mysql -uroot -pzhaozihao
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 2
Server version: 5.7.38-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> show master status; //查看主库的状态
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> create user 'slave'@'%' identified by '20021020';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; //在主服务器授权一个数据库用户用于复制
Query OK, 0 rows affected (0.01 sec)
从服务器配置
[root@slave ~]# vim /etc/my.cnf //配置文件
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysqldata
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysqldata/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
server-id=15
relay-log=mysql-relay-bin //启用中继日志relay-log
[root@slave ~]# systemctl restart mysqld.service //重启mysql服务
mysql> change master to
-> master_host='192.168.78.20',
-> master_user='slave',
-> master_password='zhaozihao',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=154; //配置并启动主从复制
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G //查看从服务器状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试验证
mysql> create database zzh; //在主服务器创建一个zzh库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zzh |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases; //在从数据库中查看是否同步
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zzh |
| sys |
+--------------------+
5 rows in set (0.00 sec)
GTID主从
GTID工作原理
从服务器连接到主服务器之后,把自己执行过的GTID (Executed_Gtid_Set: 即已经执行的事务编码) 、获取到的GTID (Retrieved_Gtid_Set: 即从库已经接收到主库的事务编号) 发给主服务器,主服务器把从服务器缺少的GTID及对应的transactions发过去补全即可。当主服务器挂掉的时候,找出同步最成功的那台从服务器,直接把它提升为主即可。如果硬要指定某一台不是最新的从服务器提升为主, 先change到同步最成功的那台从服务器, 等把GTID全部补全了,就可以把它提升为主了。
GTID是MySQL 5.6的新特性,可简化MySQL的主从切换以及Failover。GTID用于在binlog中唯一标识一个事务。当事务提交时,MySQL Server在写binlog的时候,会先写一个特殊的Binlog Event,类型为GTID_Event,指定下一个事务的GTID,然后再写事务的Binlog。主从同步时GTID_Event和事务的Binlog都会传递到从库,从库在执行的时候也是用同样的GTID写binlog,这样主从同步以后,就可通过GTID确定从库同步到的位置了。也就是说,无论是级联情况,还是一主多从情况,都可以通过GTID自动找点儿,而无需像之前那样通过File_name和File_position找点儿了。
简而言之,GTID的工作流程为:
master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。
slave端的i/o 线程将变更的binlog,写入到本地的relay log中。
sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。
如果有记录,说明该GTID的事务已经执行,slave会忽略。
如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog。
在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
GTID主从配置
主库配置
[root@zzh ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
log-bin=mysql_bin //开启二进制日志
server-id=10
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
在主数据库里创建一个同步账号授权给从数据库使用
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'zhaozihao';
Query OK, 0 rows affected, 1 warning (0.00 sec)
从库配置
[root@wrq ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
server-id=20
relay-log=myrelay
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
read_only=on
master-info-repository=TABLE
relay-log-info-repository=TABLE
[root@master ~]# systemctl restart mysqld.service //重启服务生效
从库设置要同步的主库信息,并开启同步
mysql> change master to
-> master_host='192.168.78.20',
-> master_port=3306,
-> master_user='slave',
-> master_password='zhaozihao',
-> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave; //启动主从复制
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G; //查看从服务器状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试
mysql> create database zzh; //在主服务器创建一个zzh库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zzh |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases; //在从库查看是否同步
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zzh |
| sys |
+--------------------+
5 rows in set (0.00 sec)