Mysql主从复制
目录
MySQL 主从复制介绍及部署
简介
Mysql主从复制又叫做Replication,即AB复制,简单的说就是有A、B两台机器,A做主人,B做随从,A写入什么数据,B就写入什么数据,两者实现实时同步
-
单向主从
一个主人,一个仆人
-
一主多从
一个主人,多个仆人
-
互为主从
两台机器都可以写入数据,两台机器既是主人,也是仆人
-
多主多从
-
级联双主复制逻辑架构
级联复制模式下,部分slave
的数据同步不连接主节点,而是连接从节点。因为如果主节点有太多的从节点,就会损耗一部分性能用于replication
(复制),那么我们可以让3~5
个从节点连接主节点,其它从节点作为二级或者三级与从节点连接,这样不仅可以缓解主节点的压力,并且对数据一致性没有负面影响。
binlog---
Mysql中有一个binlog二进制日志,这个日志会记录下主服务器所有修改了的sql语句,从服务器把主服务器上的binlog二进制日志,在指定的位置开始复制主服务器所有修改的语句,在从服务器上执行一遍。
简而言之就是,主服务器会把create、update、delete语句都记录到一个二进制文件中(binlog),从服务器读取这个文件,执行一遍文件中记录的create、update、delete语句。从而实现主从数据同步。
基于binlog主从部署:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.245.128 | centos8 | 无 |
从数据库 | 192.168.245.131 | centos8 | 无 |
关于Mysql安装参考Mysql基础
Mysql主从配置:
#为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中 #查看主库内容 root@main ~]# mysql -uroot -p1 -e 'show databases;' +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ [root@main ~]# #查看从库内容 [root@from ~]# mysql -uroot -p1 -e 'show databases;' +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ [root@from ~]# #这里把主库当成有内容 #全备主库 #需要注意,全备主库需要给数据库加上读锁,避免在备份期间出现数据不同的情况。 ------------- #另开一个终端,给数据库加上读锁,此锁表的终端必须在备份完成以后才能退出 MariaDB [(none)]> flush tables with read lock; Query OK, 0 rows affected (0.000 sec) --------------- #备份主库并将备份文件传送到从库 [root@main ~]# mysqldump -uroot -p1 --all-databases > /opt/all-20220802.sql [root@main ~]# ls /opt/ |grep all all-20220802.sql #下面使用scp传输文件到从库服务器 [root@main ~]# scp /opt/all-20220802.sql root@192.168.245.131:/opt/ The authenticity of host '192.168.245.131 (192.168.245.131)' can't be established. ECDSA key fingerprint is SHA256:EPEnpw32sPDEME2GB11w2pASjYmTIZTcgkkmMdvmmnk. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.245.131' (ECDSA) to the list of known hosts. root@192.168.245.131's password: all-20220802.sql 100% 466KB 119.1MB/s 00:00 [root@main ~]# -------------------------- #别忘记退出锁表状态,直接进入当时另开的终端退出交互界面即可 ariaDB [(none)]> flush tables with read lock; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> exit Bye [root@main ~]# ------------------------------ oot@from ~]# ls /opt/ all-20220802.sql [root@from ~]# mysql -uroot -p1 < /opt/all-20220802.sql [root@from ~]# mysql -u root -p1 -e 'show databases;' +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ [root@from ~]# #这里应为主库与从库内容相同导致内容并无变化,倘若主从数据不同,到这里数据就应该相同了。 ··················
在数据库里创建一个同步账户授权给从数据库使用
MariaDB [(none)]> create user 'repl'@'192.168.245.131' identified by '1'; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.245.131'; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]>
配置主数据库
root@main ~]# vim /etc/my.cnf.d/mariadb-server.cnf #安装全部安装的mariadb,my.cnf文件中有写配置文件放在哪。 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid log-bin=mysql-bin //启用binlog日志 server-id=10 //数据库服务器唯一标识符,主库的server-id值必须比从库的大 symbolic-links=0 [root@main ~]# systemctl restart mariadb.service [root@main ~]# ss -antl 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 80 0.0.0.0:3306 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* [root@main ~]# mysql -uroot -p1 #查看主库状态 MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 328 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.000 sec) MariaDB [(none)]>
配置从数据库
[root@from ~]# vim /etc/my.cnf.d/mariadb-server.cnf //添加如下内容 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid server-id=20 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值 relay-log=mysql-relay-bin //启用中继日志relay-log symbolic-links=0 [root@from ~]# systemctl restart mariadb.service [root@from ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 80 0.0.0.0:3306 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* #配置并启动主从 MariaDB [(none)]> change master to master_host='192.168.245.128', master_user='repl', master_password='repl123', master_log_file='mysql-bin.000003', master_log_pos=328; #不同服务器后面写的都不同 Query OK, 0 rows affected (0.004 sec) #查看状态 MariaDB [(none)]> show slave status \G; ································ Slave_IO_Running: Yes Slave_SQL_Running: Yes ------------------------------ #测试: MariaDB [(none)]> create database tang ; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | tang | +--------------------+ 4 rows in set (0.000 sec) ------------------------------------- 从: MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | tang | +--------------------+ 4 rows in set (0.000 sec) MariaDB [(none)]>
需要注意:
Slave_IO_Running:No >>通常需要看是不是密码或者网络的问题或者pos不对 Slave_SQL_Running: No >>通常都是防火墙没关,主库没相应的用户阻挡连接。
GTID---
从Mysql5.6.5版本开始增加的一种主从复制方法:GTID,其全称是Global Transaction Identifier,即全局事务标识。通过GTID保证每个主库提交的事务在集群中都有唯一的一个事务id。强化了数据库主从的一致性和故障恢复数据的容错能力。在主库宕机发生主从切换的情况下,GTID方式可以让其他从库主动找到新主库复制的位置,而且GTID可以忽略已经执行过的事务,减少了数据发生错误的概率。
组成:GTID是对一个已经提交事务的编号,并且是全局唯一的。GTID是由UUID和TID组成的。UUID是Mysql实例的唯一表示,TID代表该实例已经提交的事务数量,随着事务提交数量递增。
基于GTID主从部署
环境:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.245.128 | centos8 | 无 |
从数据库 | 192.168.245.131 | centos8 | 无 |
master主库配置
这里演示maradbGTID配置方法: [root@master ~]# vim /etc/my.cnf.d/mariadb-server.cnf #在[mysqld]下配置,配置完需要重启 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip_name_resolve=ON relay_log=mysql-relaylog relay_log_index=mysql-relaylog relay_log_purge=OFF slow_query_log=ON server-id=10 innodb_file_per_table=ON binlog_format=ROW log_bin=mysql-binlog log_slave_updates=ON gtid_strict_mode=ON [root@master ~]# systemctl restart mariadb.service [root@master ~]# systemctl start mariadb.service [root@master ~]# mysql -uroot -p1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.28-MariaDB-log 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)]> grant replication slave on *.* to 'repl'@'192.168.245.131' identified by '1';Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> #备份当前数据库并发送给b主机 这里和上面binlog内容相同,不再演示。记得开锁。 #任意操作查看当前gtid_binlog_pos MariaDB [(none)]> create database tang; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> show global variables like 'gtid%'; +------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | gtid_binlog_pos | 0-10-3 | | gtid_binlog_state | 0-10-3 | | gtid_current_pos | 0-10-3 | | gtid_domain_id | 0 | | gtid_ignore_duplicates | OFF | | gtid_pos_auto_engines | | | gtid_slave_pos | | | gtid_strict_mode | ON | +------------------------+--------+ 8 rows in set (0.001 sec) MariaDB [(none)]>
从主机配置
[root@slave ~]# vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip_name_resolve=ON relay_log=mysql-relaylog relay_log_index=mysql-relaylog relay_log_purge=OFF slow_query_log=ON server-id=20 innodb_file_per_table=ON binlog_format=ROW log_bin=mysql-binlog log_slave_updates=ON gtid_strict_mode=ON [root@slave ~]# systemctl restart mariadb.service [root@slave ~]# mysql -u root -p1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 10.3.28-MariaDB-log 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 [tang]> source /opt/tang.sql; MariaDB [tang]> CHANGE MASTER TO master_host='192.168.245.128', master_port=3306, master_user='repl', master_password='1',master_use_gtid=slave_pos; Query OK, 0 rows affected (0.004 sec) MariaDB [tang]> start slave; Query OK, 0 rows affected (0.005 sec) MariaDB [tang]> 验证效果 MariaDB [tangyuxuan]> show global variables like 'gtid%'; +------------------------+-----------------+ | Variable_name | Value | +------------------------+-----------------+ | gtid_binlog_pos | 0-10-4 | | gtid_binlog_state | 0-20-37,0-10-4 | | gtid_current_pos | 0-10-4 | | gtid_domain_id | 0 | | gtid_ignore_duplicates | OFF | | gtid_slave_pos | 0-10-4 | | gtid_strict_mode | ON | +------------------------+-----------------+