vlambda博客
学习文章列表

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

主从复制:

当mysql数据库的数据量太大的时候,查询数据就很吃力了,无论怎么优化都会产生瓶颈,这时我们需要增加服务器设备来实现分布式数据库,实现多机热备份,要想实现多机的热备,首先要了解主从数据库服务器的版本的需求,主从mysql的安装运行版本需一致。
因此,我们利用mysql自带的REPLICATION来实现mysql多机热备的功能,mysql版本为5.7进行演示。






读写分离:

就是把对数据库的读操作和写操作分离开,将读写压力分担到多台服务器上,通常用于读远大于写的场景。
读写分离的基本原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。
数据多了之后,对数据库的读、写就会很多。写库就一个,读库可以有多个,利用主从复制负责主库和多个读库的数据同步。







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




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



配置环境

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

数据库版本:MySQL 5.7

主服务器IP:192.168.1.100

从服务器IP:192.168.1.101


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

安装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      -- 关闭防火墙




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



配置主(Master)数据库


1、修改数据库配置文件

[root@localhost ~]# vi /etc/my.cnf


更改配置文件

[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

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

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

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 #登陆mysql
mysql
> 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没配置对,请回去重新检查配置步骤。


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



配置从(Slave)数据库

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

[root@localhost ~]# vi /etc/my.cnf


将里面的内容修改为

#开启二进制日志
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服务,登录MySQL

[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、查看从库状态信息

mysql> show slave status \G;

如下信息:

************************* 1. row *************************
              Slave_IO_State
: Waiting for master 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 has read all relay log; waiting for the slave I/O thread 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

row in set (0.00 sec)


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




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



运行测试

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.004

mysql> 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;#(慎用,清除日志同时会清空从的配置信息)




《 励志分享 》

生活不可能一帆风顺,岁月不可能一尘不染,人生不可能一举成功。但我们相信,一切终将过去,一定会春暖花开。



       
         
         
       
           
             
             
           

看完本文有收获?点赞、分享是最大的支持