mysql双机热备 读写分离_轻松搭建MySQL主从复制、读写分离(双机热备)

2022年10月11日10:17:31

主从复制: 当mysql数据库的数据量太大的时候,查询数据就很吃力了,无论怎么优化都会产生瓶颈,这时我们需要增加服务器设备来实现分布式数据库,实现多机热备份,要想实现多机的热备,首先要了解主从数据库服务器的版本的需求,主从mysql的安装运行版本需一致。因此,我们利用mysql自带的REPLICATION来实现mysql多机热备的功能,mysql版本为5.7进行演示。读写分离: 就是把对数据库的读操作和写操作分离开,将读写压力分担到多台服务器上,通常用于读远大于写的场景。读写分离的基本原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。数据多了之后,对数据库的读、写就会很多。写库就一个,读库可以有多个,利用主从复制负责主库和多个读库的数据同步。

上一章我们讲了再Laravel中配置读写分离和负载均衡,那么有点小小的问题还需要在本节中完成,就是mysql的主从复制,如果没有主从复制的话,数据会有些问题,所以本节也把最重要的内容分享出来,大家学习。

配置环境

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

数据库版本:MySQL 5.7

主服务器IP:192.168.1.100

从服务器IP:192.168.1.101

安装Mysql数据库(后面单独出一篇mysql的安装教程)

我们现在就简单安装一下mysql,新手按步骤进行操作:

1、先检查系统是否安装有mysql[root@localhost ~]#yum list installed mysql*[root@localhost ~]#rpm –qa | grep mysql*

2、查看有没有安装包[root@localhost ~]#yum list mysql*

3、安装mysql客户端[root@localhost ~]#yum install mysql

4、安装mysql服务端[root@localhost ~]#yum install mysql-server[root@localhost ~]#yum install mysql-devel

5、开启端口vi etc/sysconfig/iptables-A INPUT -p tcp -m tcp –dport 3306 -j ACCEPT

6、重启防火墙systemctl restart iptables.service -- 重启防火墙systemctl stop iptables.service -- 关闭防火墙

配置主(Master)数据库

1、修改数据库配置文件[root@localhost ~]# vi etc/my.cnf

更改配置文件[mysqld]#开启二进制日志log-bin=mysql-bin#标识唯一id(必须),一般使用ip最后位server-id=100#不同步的数据库,可设置多个binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=mysql#指定需要同步的数据库(和slave是相互匹配的),可以设置多个binlog-do-db=test注:日志的存储容量设置的大小是根据你的服务器资源实际情况修改的。

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

3、登陆MySQL数据库允许从库获得主库日志[root@localhost ~]# mysql -u root -p"你的密码"注:第一次登陆是不需要输入root的密码的,有密码就要输入哦。

进入后做如下配置:#给从库放权限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>select host,user,password from mysql.user; #查看是否修改成功mysql>FLUSH PRIVILEGES; #刷新权限

4、重启MySQL服务,登录MySQL,查看主库信息[root@localhost ~]# service mysqld restart #重启mysql服务[root@localhost ~]# mysql -u root -p #登陆mysqlmysql> show master status; #查看master状态

显示大概如下内容+------------------+----------+--------------+----------------------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+----------------------------------+-------------------+| mysql-bin.000007 | 120 | ufind_db |information_schema,performance_schema,mysql | |+------------------+----------+--------------+----------------------------------+-------------------+1 row in set (0.00 sec)注:如果执行这个步骤始终为Empty set(0.00 sec),那说明前面的my.cnf没配置对,请回去重新检查配置步骤。

配置从(Slave)数据库

1、修改从库的数据库配置文件[root@localhost ~]# vi etc/my.cnf

将里面的内容修改为#开启二进制日志log-bin=mysql-binserver-id=101binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=mysql#与主库配置保持一致replicate-do-db=testreplicate-ignore-db=mysqllog-slave-updatesslave-skip-errors=allslave-net-timeout=60

2、重启MySQL服务,登录MySQL[root@localhost ~]# service mysqld restart[root@localhost ~]# mysql -u root -p"你的密码"

并作如下修改:#关闭Slavemysql> 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;#开启Slavemysql> start slave;注:上面的master_log_file是在配置Master的时候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一对应。

3、查看从库状态信息mysql> show slave status \G;

如下信息:************************* 1. row ************************* Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.1.100Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000007Read_Master_Log_Pos: 120Relay_Log_File: localhost-relay-bin.000007Relay_Log_Pos: 520Relay_Master_Log_File: mysql-bin.000007Slave_IO_Running: Yes //显示yes为成功 Slave_SQL_Running: Yes //显示yes为成功,如果为no,一般为没有启动master Replicate_Do_DB: testReplicate_Ignore_DB: mysql//上面的都是配置文件中的信息 Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 357Relay_Log_Space: 697Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: //如果为no,此处会显示错误信息 Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 2Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6aMaster_Info_File: usr/local/mysql/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 01 row in set (0.00 sec)

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

运行测试

1、关于增删改查,主从数据不一致问题:原因:在主库的logbin中的确有执行删除语句,但是在从库的logbin中却没有删除语句。解决:使用 use database 选取当前数据库架构中的需要操作的数据库,然后在执行删除,OK同步成功。

2、查询binlog主从日志的方法查看binlog全部文件mysql>show binary logs;#查看binlog是否开启NO为开启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#手动刷新日志mysql> show master status;#删除全部mysql> reset slave; #或 rest master;#删除MySQL-bin.004mysql> PURGE MASTER LOGS TO 'MySQL-bin.004';

4、基本命令mysql> show master status; #查看主的状态mysql> show slave status\G; #查看从的状态mysql> show processlist; #查看mysql的进程状态信息mysql> show master logs; #查看主的日志mysql> reset slave;#(慎用,清除日志同时会清空从的配置信息)

来源:

https://www.toutiao.com/a6823566082964455947/“IT大咖说”欢迎广大技术人员投稿,投稿邮箱:aliang@itdks.com

来都来了,走啥走,留个言呗~

IT大咖说  |关于版权

由“IT大咖说(ID:itdakashuo)”原创的文章,转载时请注明作者、出处及微信公众号。投稿、约稿、转载请加微信:ITDKS10(备注:投稿),茉莉小姐姐会及时与您联系!

感谢您对IT大咖说的热心支持!

  • 作者:weixin_39690972
  • 原文链接:https://blog.csdn.net/weixin_39690972/article/details/111493539
    更新时间:2022年10月11日10:17:31 ,共 5603 字。