MariaDB GaleraCluster简介及双主HA实践
分布式数据库/数据库集群系统中数据库复制是十分重要的,常用的有以下两种架构:Master/SlaveReplication架构(左)和Multi-masterReplication架构(右)。
What is GaleraCluster?
Galera Cluster is awrite-set replication service PRovider in the formof the dlopenable library. It provides synchronous replication andsupports multi-master replication. Galera Cluster is capable ofunconstrained parallel applying (that is,“parallel replication”),multicast replication and automatic node provisioning.
The primary focus ofGalera Cluster is data consistency. Transactions are either appliedto every node or not at all. Galera Cluster is not a clustermanager, a load balancer or a cluster monitor. What it does it keepdatabases synchronized provided that they were properly configuredand synchronized in the beginning.
而GaleraCluster提供了并行的复制机制,能实现同步的,多主的复制方式。因此具有很多实用特性:(1)可以在任意节点上进行读写(2)自动剔除故障节点(3)自动加入新节点(4)真正行级别的并发复制(5)客户端连接跟操作单数据库的体验一致。
在数据复制方面有同步和异步两种方式,而同步方式的优势十分明显:(1)高可用(当节点宕机时数据不会丢失,数据复制一致性高,实现不复杂);(2)具有较高的性能;(3)跨集群。但是缺点是随着节点数量的增加,其事务的响应时间,冲突的概率和死锁率会呈指数增长。(因此像MySQL和PostgreSQL都采用异步复制方式)
•DatabaseManagement System (DBMS) The databaseserver that runs on the individual node. Galera Cluster can useMySQL, MariaDB or Percona XtraDB.
•wsrepAPI The interface andthe responsibilities for the database server and replicationprovider. It consists of:
•wsrephooks The integrationwith the database server engine for write-setreplication.
•dlopen()Thefunction that makes the wsrep provider available to the wsrephooks.
•Galera ReplicationPlugin The plugin thatenables write-set replication service functionality.
•GroupCommunication plugins(GCS plugins) The various groupcommunication systems available to Galera Cluster. Forinstance,gcommand Spread..
State SnapshotTransfer (SST)
SST是集群提供给节点用来从一个节点到其它节点对一个完整数据的拷贝传输功能。GaleraCluster可以使用逻辑方法(mysqldump)和物理方法(rsync,rsync_wan,xtrabackup等)。
如下图,对此本文设计了一个基于freeradius认证的VPN集群系统,其核心的数据库采用MariaDB双主GaleraCluster实现,同时借助keepalive和haproxy实现负载均衡。
设VIP地址为192.168.10.99
节点 | Hostname | FQDN | Ipaddress | 备注(CentOS7) |
Master | radiusone | radiusone.ctgu.cn | 192.168.10.91 | Keepalive,haproxy,mariadb |
Slave | radiustwo | radiustwo.ctgu.cn | 192.168.10.92 | Keepalive,haproxy,mariadb |
说明:由于是双机配置,相同的操作或配置以#开头,否则[root@radiusone/two~]# 开头。
# cat/etc/hosts
127.0.0.1
::1
192.168.10.91
192.168.10.92
# yum install -yepel-release
为了安装galeramariadb-galera-common mariadb-galera-server mariadb-libs mariadbmariadb-galera-common,方便起见,这里使用RDO的源。
#wgethttps://repos.fedorapeople.org/repos/openstack/openstack-kilo/rdo-release-kilo-1.noarch.rpm
# rpm -ivhrdo-release-kilo-1.noarch.rpm
# yum update-y
# yum install -y wgetnet-tools
# vim/etc/sysconfig/selinux
SELINUX=disabled
# setenforce0
设置防火墙
# iptables -A INPUT-p tcp -m state --state NEW -m multiport --dports3306,3307,4567,4568,4444 -j ACCEPT
#iptables-save
# systemctl enableiptables
# cat/etc/sysconfig/iptables
# sampleconfiguration for iptables service
# you can edit thismanually or use system-config-firewall
# please do not askus to add additional ports/services to this defaultconfiguration
*filter
:INPUT ACCEPT[0:0]
:FORWARD ACCEPT[0:0]
:OUTPUT ACCEPT[0:0]
-A INPUT -m state--state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -jACCEPT
-A INPUT -i lo -jACCEPT
-A INPUT -p tcp -mstate --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp--dport 3306 -j ACCEPT
-A INPUT -p tcp--dport 3307 -j ACCEPT
-A INPUT -p tcp--dport 4567 -j ACCEPT
-A INPUT -p tcp--dport 4568 -j ACCEPT
-A INPUT -p tcp--dport 4444 -j ACCEPT
-A INPUT -j REJECT--reject-with icmp-host-prohibited
-A FORWARD -j REJECT--reject-with icmp-host-prohibited
COMMIT
# systemctl restartiptables
# systemctl stopfirewalld
# systemctl disablefirewalld
# yum install -ymariadb mariadb-libs mariadb-devel mariadb-galera-commonmariadb-galera-server
# systemctl enable
# systemctl enable
[root@radiusone ~]#systemctl restart mariadb
[root@radiusone ~]#mysql_secure_installation
设置root用户远程使用密码passWord访问
[root@radiusone ~]#mysql -u root –p
MariaDB [(none)]>grant all privileges on *.* to 'root'@'%' identified by 'password'with grant option;
MariaDB[(none)]>
# vim/etc/my.cnf.d/galera.cnf +27
bind-address=0.0.0.0
[root@radiusone ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=1G"
wsrep_cluster_address=gcomm://
#wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
wsrep_cluster_name='dbcluster'
wsrep_node_address='192.168.10.91'
wsrep_node_name='radiusone'
#wsrep_sst_method=xtrabackup
wsrep_sst_method=rsync
wsrep_sst_auth=username:Password
字段说明
wsrep_provider – Ifyou are running on 32bit it will be in the /usr/lib otherwise on64bit systems it will be in /usr/lib64
wsrep_cluster_address– Include all theaddresses of the servers that will exist in the cluster. You canuse hostnames if you wish
wsrep_cluster_name –This is the name of the cluster. All servers in the cluster mustmatch names
wsrep_node_address– The IP address of thisserver
wsrep_node_name– The name of thisserver
wsrep_sst_auth – Thisis the username and password used to authenticate xtrabackup. Setthis the same on all machines
[root@radiustwo ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=1G"
wsrep_cluster_address=gcomm://192.168.10.91
#wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
wsrep_cluster_name='dbcluster'
wsrep_node_address='192.168.10.92'
wsrep_node_name='radiustwo'
#wsrep_sst_method=xtrabackup
wsrep_sst_method=rsync
wsrep_sst_auth=username:Password
[root@radiusone ~]#systemctl restart mariadb
[root@radiustwo ~]#systemctl restart mariadb
【按照下文附录中的Mariadb-galeraHA集群启动顺序操作】
# yum install -ykeepalived
# vim/etc/sysctl.conf
net.ipv4.tcp_keepalive_intvl=1
net.ipv4.tcp_keepalive_time=5
net.ipv4.tcp_keepalive_probes=5
net.ipv4.ip_forward=1
net.ipv4.ip_nonlocal_bind=1
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-arptables=1
# sysctl-p
配置keepalive forVIP 192.168.10.99
[root@radiusone ~]#cat /etc/keepalived/keepalived.conf
! Configuration Filefor keepalived
global_defs{
}
vrrp_scriptcheck_haproxy {
}
vrrp_instance VI_1{
}
[root@radiustwo ~]#cat /etc/keepalived/keepalived.conf
! Configuration Filefor keepalived
global_defs{
}
vrrp_scriptcheck_haproxy {
}
vrrp_instance VI_1{
}
# systemctl restartkeepalived
# systemctl enablekeepalived
可以查看当前VIP192.168.10.99在radiusone[MASTER]主机上
[root@radiusone ~]#ip a
2: eth0: mtu 1500qdisc pfifo_fast state UP qlen 1000
[root@radiustwo ~]#ip a
2: eth0: mtu 1500qdisc pfifo_fast state UP qlen 1000
当停止radiusone上的keepalive时,VIP会自动漂移至radiustwo上
[root@radiusone ~]#systemctl stop
[root@radiustwokeepalived]# ip a
2: eth0: mtu 1500qdisc pfifo_fast state UP qlen 1000
[root@radiusone ~]#mysql -h 192.168.10.99 -u root -p
# yum install-y haproxy
# cat/etc/haproxy/haproxy.cfg
global
defaults
# systemctl starthaproxy
添加数据库服务
# vim/etc/haproxy/haproxy.cfg
listengalera_cluster
# systemctl restarthaproxy
# systemctlenable
重点要检查的字段
SHOW GLOBAL STATUSLIKE’wsrep_%’;
(1)SHOW GLOBAL STATUSLIKE ’wsrep_cluster_state_uuid’; 集群中每个节点的值必须一致。
(2)SHOW GLOBAL STATUSLIKE ’wsrep_cluster_size’; 显示集群的规模,此例应该显示”2”。
(3)SHOW GLOBAL STATUSLIKE ’wsrep_cluster_status’; 显示”Primary”为正常。
(4)SHOW GLOBAL STATUSLIKE ’wsrep_ready’; 显示”ON”为正常。
(5) SHOW GLOBALSTATUS LIKE ’wsrep_connected’; 显示”ON”为正常。
(6) SHOW GLOBALSTATUS LIKE ’wsrep_local_state_comment’; 显示”Joined”为正常。
至此,MariaDBGalera双主Cluster构建完成,接下来即可参考官方文档完成freeradius和daloradius以及vpn-l2tp的部署,实现基于freeradius认证的VPN集群系统。
https://mariadb.com/kb/en/mariadb/starting-and-stopping-mariadb/
(1)先启动radiusone节点mariadb,并且vim/etc/my.cnf.d/server.cnf配置文件中
wsrep_cluster_address=gcomm://
#wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
(2)再启动radiustwo节点mariadb,并且vim/etc/my.cnf.d/server.cnf配置文件中
#wsrep_cluster_address=gcomm://
wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
(3)重启radiusone节点mariadb,并且vim/etc/my.cnf.d/server.cnf配置文件中
#wsrep_cluster_address=gcomm://
wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
(4)查看端口(VIP在radiusone上面)
[root@radiusone ~]#netstat -anp | grep 3306
tcp
tcp
tcp
[root@radiusone ~]#netstat -anp | grep 3307
tcp
tcp
[root@radiustwo ~]#netstat -anp | grep 3306
tcp
[root@radiustwo ~]#netstat -anp | grep 3307
tcp
检查mariadb集群的状态
# mysql -u root–p
MariaDB [(none)]>SHOW STATUS LIKE 'wsrep%';
https://mariadb.com/kb/en/mariadb/galera-cluster-status-variables/
(1)关闭或者重启slave节点radiustwo,检查能否使用VIP:3307正常访问数据库;【通过】
(2)关闭或者重启master节点radiusone,检查能否使用VIP:3307正常访问数据库;【通过】
(3)将master(radiusone)和slave(radiustwo)节点全部关闭,启动后,检查能否使用VIP:3307正常访问数据库。【按照上面集群启动的顺序来操作】
(1)查看日志如果有以下类似错误:
# tailf/var/log/mariadb/mariadb.log
160203 18:31:37[Warning] WSREP: no nodes coming from prim view, prim notpossible
160203 18:31:37[Note] WSREP:view(view_id(NON_PRIM,4c674ade-ca61-11e5-a9f8-7e298e07ba
33,1) memb{
} joined {
} left {
} partitioned{
})
160203 18:31:38[Warning] WSREP: last inactive check more than PT1.5S ago(PT3.50687S)
, skippingcheck
160203 18:32:07[Note] WSREP: view((empty))
160203 18:32:07[ERROR] WSREP: failed to open gcomm backend connection: 110: failedto
160203 18:32:07[ERROR] WSREP: gcs/src/gcs_core.c:gcs_core_open():202: Failed toopen
backend connection:-110 (Connection timed out)
160203 18:32:07[ERROR] WSREP: gcs/src/gcs.c:gcs_open():1291: Failed to openchannel '
dbcluster' at'gcomm://192.168.10.91': -110 (Connection timed out)
160203 18:32:07[ERROR] WSREP: gcs connect failed: Connection timed out
160203 18:32:07[ERROR] WSREP: wsrep::connect() failed: 7
160203 18:32:07[ERROR] Aborting
160203 18:32:07[Note] WSREP: Service disconnected.
160203 18:32:08[Note] WSREP: Some threads may fail to exit.
160203 18:32:08[Note] /usr/libexec/mysqld: Shutdown complete
多半是因为防火墙的问题,使用iptables-F关闭防火墙,再次重启集群,检查HA是否正常。
# sudo iptables-F
(2)Mariadb无法重启,报如下错误
160203 21:03:26[Note] Plugin 'FEEDBACK' is disabled.
160203 21:03:26[Note] Server socket created on IP: '0.0.0.0'.
160203 21:03:26[ERROR] Can't start server: listen() on TCP/IP port: Addressalready in use
160203 21:03:26[ERROR] listen() on TCP/IP failed with error 98
160203 21:03:26[ERROR] Aborting
16020321:03:26
16020321:03:27
160203 21:03:27[Note] /usr/libexec/mysqld: Shutdown complete'
原因是3306端口被占用,使用命令netstat-anp | grep 3306查看可知被haproxy监听listengalera_cluster端口占用。
解决方法:
关闭haproxy,重启mariadb集群成功后,再开启haproxy。
# iptables-F
# systemctl stophaproxy
# systemctl restartmariadb
# systemctl restarthaproxy
参考:
http://serverfault.com/questions/623054/centos-7-freeradius-fails-to-start-on-boot-due-to-priority
或者
将haproxy监听端口改为其它端口,比如3307。但是其它应用(比如freeradius和daloradius)在配置数据库是也要将端口设为3307。再或者是将各节点的mariadb配置为其它端口,修改
listengalera_cluster
# vim/etc/my.cnf.d/galera.cnf +27
bind-address=0.0.0.0
主要参考:http://galeracluster.com/documentation-webpages/galera-documentation.pdf
cp /usr/share/zoneinfo/Asia/Shanghai/etc/localtime
新闻热点
疑难解答