Linux系统如何搭建MySQL主从复制和读写分离(双机热备)?

2022-09-04 08:08:59

主从复制:

当mysql数据库中的数据量过大,查询数据非常困难时,无论如何优化都会造成瓶颈,这时需要添加服务器设备来实现分布式数据库,实现多机热备份,实现多机热备,您必须首先了解主从数据库服务器版本的要求。主从mysql的安装和运行版本必须一致。因此,我们使用mysql自带的复制来实现mysql多机热备份的功能。mysql版本是5.7,用于演示。

读写分离:

它是将数据库上的读写操作分离,在多台服务器上共享读写压力,通常用于读远远大于写的情况。

读写分离的基本原则是让主数据库处理事务性的添加、修改和删除操作,而从数据库主要用于查询操作。数据库复制用于将事务操作导致的更改同步到群集中的从数据库。

当有更多的数据时,就会有很多对数据库的读写。只有一个写库,可以有多个读库。主从复制负责主库和多个读取库之间的数据同步。

配置环境

操作系统:两台CentOS 7.6的Linux系统(虚拟机)

数据库版本:MySQL 5.7

主服务器IP:192.168.1.100

从服务器IP:192.168.1.101

安装Mysql数据库

首先安装MySQL,新手按步骤进行操作(本站有专门的一篇教程教授如何安装MySQL,可在网站上方的搜索栏中搜索):

1、先检查系统是否安装有mysql

1

2

[root@localhost ~]#yum list installed mysql*

[root@localhost ~]#rpm –qa | grep mysql*

2、查看有没有安装包

1

[root@localhost ~]#yum list mysql*

3、安装mysql客户端

1

[root@localhost ~]#yum install mysql

4、安装mysql服务端

1

2

[root@localhost ~]#yum install mysql-server

[root@localhost ~]#yum install mysql-devel

5、开启端口

1

2

vi/etc/sysconfig/iptables

-A INPUT -p tcp -m tcp –dport 3306 -j ACCEPT

6、重启防火墙

1

2

systemctl restart iptables.service   -- 重启防火墙

systemctl stop iptables.service      -- 关闭防火墙

配置主(Master)数据库

1、修改数据库配置文件my.cnf

1

<p>[root@localhost ~]# vi /etc/my.cnf<br></p>

修改配置文件

1

2

3

4

5

6

7

8

9

[mysqld]#开启二进制日志

log-bin=mysql-bin

#标识唯一id(必须),一般使用ip最后位

server-id=100#不同步的数据库,可设置多个

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

binlog-ignore-db=mysql

#指定需要同步的数据库(和slave是相互匹配的),可以设置多个

binlog-do-db=test

注:日志的存储容量大小可以根据你的服务器资源实际情况修改。

2、重启MySQL数据库服务(mysqld)

1

service mysqld restart

3、登陆MySQL数据库允许从库获得主库日志

1

[root@localhost ~]# mysql -u root -p"你的密码"

注:第一次登陆是不需要输入root的密码的,有密码就要输入哦。

进入后做如下配置:

1

2

3

4

5

#给从库放权限

mysql>GRANT FILE ON *.* TO'repl'@'192.168.1.101'IDENTIFIED BY'repl password';#创建用户

mysql>GRANT REPLICATION SLAVE ON *.* TO'repl'@'192.168.0.101'IDENTIFIED BY'repl password';#修改用户权限

mysql>selecthost,user,password from mysql.user;#查看是否修改成功

mysql>FLUSH PRIVILEGES;#刷新权限

4、重启MySQL服务,登录MySQL,查看主库信息

1

2

3

[root@localhost ~]# service mysqld restart #重启mysql服务

[root@localhost ~]# mysql -u root -p #登陆mysql

mysql> show master status;#查看master状态

显示大概如下内容:

注:如果执行这个步骤始终为Empty set(0.00 sec),那说明前面的my.cnf没配置对,需要重新检查配置步骤。

配置从(Slave)数据库

1、修改从库的数据库配置文件

1

<p>[root@localhost ~]# vi /etc/my.cnf<br></p>

将里面的内容更改为

1

2

3

4

5

6

7

8

9

10

11

#开启二进制日志

log-bin=mysql-bin

server-id=101binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

binlog-ignore-db=mysql

#与主库配置保持一致

replicate-do-db=test

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

slave-net-timeout=60

2、重启MySQL服务并登陆

1

2

[root@localhost ~]# service mysqld restart

[root@localhost ~]# mysql -u root -p"你的密码"

修改:

#关闭Slave

mysql> stop slave; #设置连接主库信息

mysql> change master to master_host='192.168.1.100',master_user='repl',master_password='repl password',master_log_file='mysql-bin.000007', master_log_pos=120;#开启Slave

mysql> start slave;

注:上面的master_log_file是在配置Master的时候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一对应

3、查看从库状态信息

1

mysql> show slave status \G;

如下信息:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

************************* 1. row *************************

Slave_IO_State: Waitingformaster to send event

Master_Host: 192.168.1.100

Master_User: root

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 120

Relay_Log_File: localhost-relay-bin.000007

Relay_Log_Pos: 520

Relay_Master_Log_File: mysql-bin.000007

Slave_IO_Running: Yes//显示yes为成功

Slave_SQL_Running: Yes//显示yes为成功,如果为no,一般为没有启动master

Replicate_Do_DB:test

Replicate_Ignore_DB: mysql//上面的都是配置文件中的信息

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 357

Relay_Log_Space: 697

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error://如果为no,此处会显示错误信息

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 2

Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a

Master_Info_File:/usr/local/mysql/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave hasreadall relay log; waitingforthe slave I/Othread to update it

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 01

rowinset(0.00 sec)

到此整个过程就算配置好了。现在我们可以在主服务器上创建一个表,然后在从服务器上查询刚创建的这个表是否存在。

运行测试

1、关于增删改查,主从数据不一致问题:

原因:在主库的logbin中的确有执行删除语句,但是在从库的logbin中却没有删除语句。

解决:使用 use database 命令选取当前数据库架构中的需要操作的数据库,然后再执行删除操作,同步成功。

2、查询binlog主从日志的方法

查看binlog全部文件,常用的几个命令

mysql>show binary logs;

mysql> show variables like 'log_bin%';#详细信息

mysql>  show variables like 'binlog%';#查看binlog日志

mysql> show binlog events in'mysql-bin.000007';#或者使用mysqlbinlog,如果报错使用--no-defaults(使用全路径)

[root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019

3、手动清理master日志,最好关闭日志,在/etc/my.cnf

1

2

3

4

5

#手动刷新日志

mysql> show master status;#删除全部

mysql> reset slave;#或 rest master;

#删除MySQL-bin.004

mysql> PURGE MASTER LOGS TO'MySQL-bin.004';

4、基本命令

1

2

3

4

5

mysql> show master status;#查看主的状态

mysql> show slave status\G;#查看从的状态

mysql> show processlist;#查看mysql的进程状态信息

mysql> show master logs;#查看主的日志

mysql> reset slave;#(慎用,清除日志同时会清空从的配置信息)

 原文地址:【架构师小跟班 www.jiagou1216.com】

  • 作者:codernavcom
  • 原文链接:https://blog.csdn.net/wangxy_job/article/details/106313553
    更新时间:2022-09-04 08:08:59