基于Keepalived高可用集群的MariaDB读写分离机制实现

一 MariaDB读写分离机制

在实现读写分离机制之前先理解一下三种主从复制方式:
1.异步复制:
MariaDB默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题,主节点如果挂掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。
2.全同步复制:
指当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。因为需要等待所有从库执行完该事务才能返回,所以全同步复制的性能必然会收到严重的影响。
3.半同步复制:
介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到relay log中才返回给客户端。相对于异步复制,半同步复制提高了数据的安全性,同时它也造成了一定程度的延迟,这个延迟最少是一个TCP/IP往返的时间。所以,半同步复制最好在低延时的网络中使用。

在本次实验中我们采用一主两从的方式实现MySQL读写分离机制,而在主从复制中采用异步复制和半同复制两种方式来保证数据的安全性和完整性。为了保证读写分离器高可用,还可利用Keepalived实现高可用集群。

二 常用读写分离器的介绍

mysql-proxy:MySQL官方出品;
atlas:奇虎360公司二次开发产品;
amoeba:Alibaba集团开发的可实现读写分离、分片功能的读写分离器;
OneProxy:读写分离框架,由平民架构开发并维护;
ProxySQL:一款高性能读写分离器;
MaxScale:Mariadb官方,稳定可靠,比较有影响力。

本实验我们采用ProxySQL来实现读写分离。

三 架构拓扑图

四 主从复制配置

#分别在三台主从节点配置如下的内容
[[email protected] ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
server_id=1
log_bin=mysql-bin
[[email protected] ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
server_id=2
relay_log=relay-log
[[email protected] ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
server_id=3
relay_log=relay-log
#启动MariaDB服务
[[email protected] ~]# systemctl start mariadb.service
#登入MariaDB
[[email protected] ~]# mysql
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      245 |              |                  |
+------------------+----------+--------------+------------------+
#创建MariaDB复制账号
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘172.18.67.%‘ IDENTIFIED BY ‘replpass‘;
#安装半同步主从复制插件并启动主从复制功能
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘;
MariaDB [(none)]> SET @@global.rpl_semi_sync_master_enabled=ON;
#启动MariaDB服务
[[email protected] ~]# systemctl start mariadb.service
#登入MariaDB
[[email protected] ~]# mysql
#安装半同步主从复制插件并启动主从复制功能
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘;
MariaDB [(none)]> set @@global.rpl_semi_sync_slave_enabled=ON;
#配置slave1节点的master节点
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.18.67.11‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000003‘,MASTER_LOG_POS=442;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.67.11
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 422
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 422
              Relay_Log_Space: 817
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
#启动MariaDB服务
[[email protected] ~]# systemctl start mariadb.service
#登入MariaDB
[[email protected] ~]# mysql
#配置slave2节点的master节点
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.18.67.11‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000003‘,MASTER_LOG_POS=442;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.67.11
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 422
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 422
              Relay_Log_Space: 817
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
#进行测试
[[email protected] ~]# mysql -e "SHOW GLOBAL STATUS LIKE ‘%semi%‘" | grep ‘Rpl_semi_sync_master_clients‘
Rpl_semi_sync_master_clients 1
#在主节点创建数据库mydb
[[email protected] ~]# mysql -e "CREATE DATABASE mydb;"
#在从节点查看主节点创建的数据库mydb是否复制过来
[[email protected] ~]# mysql -e "SHOW DATABASES;" | grep mydb
mydb
[[email protected] ~]# mysql -e "SHOW DATABASES;" | grep mydb
mydb
#从节点已经将主节点创建的数据库复制过来了

五 读写分离ProxySQL的配置

1.节点一

[[email protected] ~]# vim /etc/proxysql.cnf
datadir="/var/lib/proxysql"
admin_variables=
{
 admin_credentials="admin:admin"
 mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
}
mysql_variables=
{
 threads=4
 max_connections=2048
 default_query_delay=0
 default_query_timeout=36000000
 have_compress=true
 poll_timeout=2000
 interfaces="0.0.0.0:3306;/tmp/mysql.sock"
 default_schema="information_schema"
 stacksize=1048576
 server_version="5.5.30"
 connect_timeout_server=3000
 monitor_history=600000
 monitor_connect_interval=60000
 monitor_ping_interval=10000
 monitor_read_only_interval=1500
 monitor_read_only_timeout=500
 ping_interval_server=120000
 ping_timeout_server=500
 commands_stats=true
 sessions_sort=true
 connect_retries_on_failure=10
}
mysql_servers =
(
 {
  address = "172.18.67.11"   # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
  port = 3306                # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
  hostgroup = 0              # no default, required
  status = "ONLINE"          # default: ONLINE
  weight = 1                 # default: 1
  compression = 0            # default: 0
 },
 {
  address = "172.18.67.12"
  port = 3306
  hostgroup = 1
  status = "ONLINE"          # default: ONLINE
  weight = 1                 # default: 1
  compression = 0            # default: 0
 },
 {
  address = "172.18.67.13"
  port = 3306
  hostgroup = 1
  status = "ONLINE"          # default: ONLINE
  weight = 1                 # default: 1
  compression = 0            # default: 0
 }
)
mysql_users:
(
 {
  username = "root"
  password = "mrlapulga"
  default_hostgroup = 0
  max_connections=1000
  active = 1
 }
)
mysql_replication_hostgroups=
(
 {
  writer_hostgroup=0
  reader_hostgroup=1
 }
)
#在主节点对用户进行授权:
MariaDB [(none)]> GRANT ALL ON *.* TO ‘root‘@‘172.18.67.%‘ IDENTIFIED BY ‘mrlapulga‘;
MariaDB [(none)]> FLUSH PRIVILEGES;
#启动读写分离器的节点1
[[email protected] ~]# systemctl start proxysql
#使用管理接口验证:
[[email protected] ~]# mysql -uadmin -h127.0.0.1 -padmin -P6032
MySQL [(none)]> SELECT hostgroup_id,hostname,hostname,status FROM mysql_servers;
+--------------+--------------+--------------+--------+
| hostgroup_id | hostname     | hostname     | status |
+--------------+--------------+--------------+--------+
| 0            | 172.18.67.11 | 172.18.67.11 | ONLINE |
| 1            | 172.18.67.12 | 172.18.67.12 | ONLINE |
| 1            | 172.18.67.13 | 172.18.67.13 | ONLINE |
+--------------+--------------+--------------+--------+
#测试读操作
[[email protected] ~]# mysql -uroot -pmrlapulga -h 127.0.0.1  -P3306
MySQL [OA]> use mydb;
MySQL [mydb]> DESC tbl1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(20)            | YES  |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+
#测试写操作,在proxysql节点插入一条字段
MySQL [mydb]> INSERT INTO mydb.tbl1 (name) VALUES (‘jack‘);
#在从节点一查看
[[email protected] ~]# mysql -e "SELECT * FROM mydb.tbl1;"
+----+------+
| id | name |
+----+------+
|  1 | jack |
+----+------+
#在从节点二查看
[[email protected] ~]# mysql -e "SELECT * FROM mydb.tbl1;"
+----+------+
| id | name |
+----+------+
|  1 | jack |
+----+------+

2.节点二

#将配置文件复制到ProxySQL节点二
[[email protected] ~]# scp /etc/proxysql.cnf 172.18.67.15:/etc/
#启动节点二的服务
[[email protected] ~]# systemctl start proxysql

六 配置高可用集群

#在ProxySQL1节点配置keepalived
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id proxysql
    vrrp_macst_group4 224.0.67.67
}
vrrp_instance HA_mysql {
    state MASTER
    interface eno16777736
    virtual_router_id 67
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
  auth_pass lKZvQVv9
    }
    virtual_ipaddress {
      172.18.67.33/16 dev eno16777736
    }
}
[[email protected] ~]# systemctl start keepalived
#在ProxySQL2节点配置keepalived
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id proxysql
    vrrp_macst_group4 224.0.67.67
}
vrrp_instance HA_mysql {
    state BACKUP
    interface eno16777736
    virtual_router_id 67
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
  auth_pass lKZvQVv9
    }
    virtual_ipaddress {
      172.18.67.33/16 dev eno16777736
    }
}
[[email protected] ~]# systemctl start keepalived

七 测试

#在ProxySQL节点1登录MySQL
[[email protected] ~]# mysql -uroot -h172.18.67.33 -pmrlapulga -P 3306
MySQL [(none)]> use mydb;
#查看数据库及表
MySQL [mydb]> SELECT * FROM tbl1;
+----+------+
| id | name |
+----+------+
|  1 | jack |
+----+------+
#在ProxySQL节点2登录MySQL
[[email protected] ~]# mysql -uroot -h172.18.67.33 -pmrlapulga -P 3306
MySQL [(none)]> use mydb;
#查看数据库及表
MySQL [mydb]> SELECT * FROM tbl1;
+----+------+
| id | name |
+----+------+
|  1 | jack |
+----+------+
#测试在ProxySQL1节点写操作
MySQL [mydb]> CREATE TABLE tbl2 (id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, name CHAR(20) NOT NULL, PRIMARY KEY(id));
#分别在两个从节点查看
[[email protected] ~]# mysql -e "DESC mydb.tbl2;"
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(20)            | NO   |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+
[[email protected] ~]# mysql -e "DESC mydb.tbl2;"
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(20)            | NO   |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+
时间: 2024-11-03 21:33:54

基于Keepalived高可用集群的MariaDB读写分离机制实现的相关文章

keepalived高可用集群技术介绍及实战演练

一.Keepalived是什么 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器. 二.Keepalived的特性 配置文件简单:配置文件比较简单,可通过简单配置实现高可用功能 稳定性强:keepalived是一个类似于

Keepalived高可用集群应用场景与配置

1.Keepalived单实例主备模式集群方案 这是最简单的模式,不只考虑高可用集群,先不考虑后方的Nginx负载均衡集群,即后端的服务器集群,参考下面的图示: 其对应的Keepalived核心配置如下: lb01 global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_serv

linux企业常用服务---lvs+Keepalived高可用集群

部署前准备: iptables和selinux没配置,关掉 挂载系统镜像作为本地yum源,修改yum文件 源码包准备keepalived-1.2.13.tar.gz 环境介绍: 主服务器ip:192.168.100.157(keeplived+lvs) 从服务器ip:192.168.100.156(keeplived+lvs) 节点服务器ip:192.168.100.153-192.168.100.155(httpd) 集群vip:192.168.100.95 1.安装keepalived(在两

Keepalived高可用集群

Keepalived高可用集群 keepalived高可用集群是指一个主服务器,一个备份服务器,共同使用一个虚拟的ip地址,当主服务器宕掉之后,备份服务器开始工作,这样就避免了访问事故. 搭建keepalived高可用集群 [master] #yum -y install keepalived #vim /etc/keepalived/keepalived.conf vrrp_instance webha { state MASTER 主服务器 interface eth0 网卡口 priori

部署Memcached+magent+keepalived高可用集群

Memcached+magent+keepalived高可用集群 magent是一款开源的代理服务软件,我们可以通过他来实现缓存数据的同步,当然这里说的同步不是说memcached之间就能互相通讯了, 而magent可以同时连接多个memcached节点, 通过magent绑定的VIP从客户端登录memcached写入数据,其他节点的memcached数据也会同步.? 案例环境 主机名称 IP 主要软件 memcached主 192.168.200.128 Magent.memcached.li

Keepalived高可用集群。

Keepalived高可用集群 Keepalived介绍 Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx,Haproxy,MySQL等)的高可用解决方案软件. Keepalived软件主要是通过VRRP协议实现高可用功能的.VRRP是Virtual Router Redundancy Protocol(

Nginx+Keepalived高可用集群应用实践

Nginx+Keepalived高可用集群应用实践 1.Keepalived高可用软件 1.1 Keepalived服务的三个重要功能 1.1.1管理LVS负载均衡软件 早期的LVS软件,需要通过命令行或脚本实现管理,并且没有针对LVS节点的健康检查功能.为了解决LVS的这些使用不便问题,Keepalived诞生了,可以说,Keepalived软件起初是专为解决LVS的问题而诞生的.因此,Keepalived和LVS的感情很深,他们的关系如同夫妻一样,可以紧密地结合,愉快地工作.Keepaliv

LVS+Keepalived高可用集群(CentOS7)

Keepalived高可用集群 什么是高可用集群? keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. keepalived故障切换转移原理 keepalived是以VRRP协议为实现基础的,VRRP虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip

Nginx负载均衡+Keepalived高可用集群

一.搭建环境及软件版本 负载均衡:Nginx 高可用:Keepalived Linux:Centos 6.5 Nginx:nginx-1.6.2 Keepalived:keepalived-1.2.13 LB-00:Nginx+Keepalived 192.168.174.40 LB-01:Nginx+Keepalived 192.168.174.41 Web1:LNMP 192.168.174.20 Web2:LAMP 192.168.174.30 二.负载均衡搭建 ①解压编译安装Nginx