mysql主从

MySQL主从复制用于备份、故障切换、读写分离等,包括传统主从和GTID主从两种方式。传统主从基于二进制日志文件位置复制,而GTID主从通过全局事务ID实现更简单的复制和数据一致性。主从复制原理涉及主库记录写操作到binlog,从库接收并执行这些操作。配置包括创建同步账号、配置主从数据库等步骤。
摘要由CSDN通过智能技术生成

主从介绍

什么是mysql主从

所谓mysql主从就是建立两个完全一样的数据库,其中一个为主要使用的数据库,另一个为次要的数据库,一般在企业中,存放比较重要的数据的数据库服务器需要配置主从,这样可以防止因数据库服务器宕机导致数据丢失,还能保证业务量太多、数据太多和访问人数太多时服务的质量(服务器响应速度),还能提供故障切换、读写分离、和备份等等功能。

主从形式

在这里插入图片描述

  • 一主一从
  • 主主复制:当作备份使用,当主服务器出现故障时,另一个主服务器会自动顶上。
  • 一主多从:用来实现读写分离,当写操作较少时,读操作较多时使用,主服务器用来实现写操作,从服务器用来实现读操作。
  • 多主一从:用来实现读写分离,当写操作较多时,读操作较少时使用,主服务器用来实现写操作,从服务器用来实现读操作。
  • 联级复制

传统主从和gtid主从的区别

传统主从
传统主从复制主要是基于二进制日志文件位置的复制,因此主必须启动二进制日志记录并建立唯一的服务器ID,复制组中的每个服务器都必须配置唯一的服务器ID。如果您省略server-id(或者明确地将其设置为其默认值0),则主设备将拒绝来自从设备的任何连接。
gtid主从
MySQL 5.6 的新特性之一,全局事务标识符(GTID)是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有一对一的映射关系 。它由服务器ID以及事务ID组合而成。这个全局事务ID不仅仅在原始服务器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。

主从复制的原理

在这里插入图片描述
主从复制的工作流程:主库将用户所有的写操作(增删改,查除外)记录到binlog日志当中并且生成一个log dump线程,从库生成I/O和SQL线程,从库的I/O线程向主库的log dump线程以I/O流的形式发送请求,主库的log dump线程收到从库I/O线程的请求后将binlog日志发送给从库,从库I/O线程收到binlog日志后将其写道relay log(中继日志)文件中,再由从库的SQL线程将relay log文件中的日志解析成sql脚本,最后执行生成的sql脚本文件,来实现主从的操作一致,达到最终数据一致的目的。

主从复制配置

主从复制配置步骤:

  • 确保所有主从主机的防火墙已经关闭
  • 确保主数据库和从数据库里的数据一样
  • 再主数据库里创建一个同步账号并且授权给从数据库使用
  • 配置主数据库配置文件
  • 配置从数据库配置文件

传统主从的配置

实验环境:

数据库角色 IP 系统版本
主数据库 192.168.247.137 CentOS8
从数据库 192.168.247.211 CentOS8
从数据库 192.168.247.213 CentOS8

关闭所有主机的防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

确保数据一致性
主:

[root@localhost ~]# mysql -uroot -phanao.
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: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

从数据库1:

[root@localhost ~]# mysql -uroot -phanao.
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 3
Server version: 5.7.33 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

从数据库2:

[root@localhost ~]# mysql -uroot -phanao.
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 3
Server version: 5.7.33 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

如果数据库中的数据不一样,则需要全备主服务器中数据,然后恢复到从服务器中,备份时使用以下命令锁表,使数据库不能插入数据,在从数据库中使用备份与主数据库数据一致后关闭锁表。

mysql> FLUSH TABLES WITH READ LOCK;

创建账号并授权给从数据库

mysql> create user 'hanao'@'192.168.247.211' identified by 'hanao.';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'hanao'@'192.168.247.211';
Query OK, 0 rows affected (0.01 sec)

mysql> create user 'hanao'@'192.168.247.213' identified by 'hanao.';
Query OK, 0 rows affected (0.01
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值