通过Galera实现MariaDB/MySQL多活,前端由Haproxy和Keepalived实现负载和高可用,无疑是当前最主流的技术方案,本文主要讲述通过Galera搭建MariaDB多活的方案。
系统平台
操作系统:CentOS7.5
服务器配置
服务器IP | 主机名 | 应用 |
10.10.100.101 | MARIADB-A | MariaDB-Galera-server MariaDB-client galera |
10.10.100.102 | MARIADB-B | |
10.10.100.103 | MARIADB-C | |
10.10.100.201 | HAPROXY-A | haproxy keepalived |
10.10.100.202 | HAPROXY-B |
搭建Galera+MariaDB
条件:最少三个节点
原理:通过wresp协议在全局实现底层数据片复制、优于MHA
优点:节点间实时数据同步、方便横向扩展
特点:前端只需做负载均衡,无需程序做读写分离
注意事项
- 禁用Selinux
- 防火墙开放MariaDB和Galera的端口
准备工作
每个数据库节点配置相同的yum源,编辑文件/etc/yum.repos.d/mariadb.repo:
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.0/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
安装MariaDB
yum -y install MariaDB-Galera-server MariaDB-client galera systemctl enable mariadb systemctl start mariadb
我的网络访问国外网站好慢,大家可以自己先在MariaDB官网下载相应的版本,然后配个本地源,但本文不讲述。
配置MariaDB
初始化MariaDB,每个节点都需要初始化一次,除了改密码(Mysql@123),其余步骤都按“y”。
mysql_secure_installation
关闭MariaDB:
/etc/init.d/mysql stop
配置Cluster
修改配置/etc/my.cnf:
[client-server] [mysqld_safe] #log-error=/var/log/mariadb/mariadb.log #pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/my.cnf.d
修改配置/etc/my.cnf.d/server.cnf:
[server] [mysqld] bind-address = 10.10.100.101 #user=mysql #datadir=/var/lib/mysql #socket=/var/lib/mysql/mysql.sock #symbolic-links=0 #skip-external-locking #skip-name-resolve innodb_file_per_table = on max_connections = 4096 collation-server = utf8_general_ci character-set-server=utf8 innodb_flush_method = O_DIRECT wait_timeout = 28800 binlog_cache_size = 16M max_allowed_packet = 64M expire_logs_days = 30 sort_buffer_size = 128M innodb_buffer_pool_size = 512M [galera] wsrep_on=ON wsrep_provider="/usr/lib64/galera/libgalera_smm.so" wsrep_cluster_address="gcomm://10.10.100.101,10.10.100.102,10.10.100.103" wsrep_cluster_name="galera_cluster" wsrep_node_address=10.10.100.101 wsrep_node_name=mysql-a wsrep_slave_threads=1 wsrep_causal_reads=ON wsrep_certify_nonPK=ON wsrep_sst_method=rsync #wsrep_sst_donor=msyql-a,mysql-b,mysql-c #wsrep_sst_method=xtrabackup-v2 #wsrep_sst_auth=wsrepl:Mysql@123 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address = 10.10.100.101 innodb_flush_log_at_trx_commit=0 [embedded] [mariadb] [mariadb-10.0]
当使用XtraBackup进行热备时,需要创建用于同步数据的账号,取消以上配置中wsrep_sst_method相关参数的注释并修改,XtraBackup安装包可在其官网下载。
mysql -uroot -pMysql@123
use mysql update user set host='%' where host='localhost'; create user 'wsrepl'@'%' identified by 'Mysql@123'; grant all on *.* to 'wsrepl'@'%'; flush privileges; exit
同步配置文件
同步完成后,一定要修改每个节点的bind-address、wsrep_node_address和wsrep_node_name。
for i in 2 3; do scp /etc/my.cnf.d/server.cnf root@10.10.100.10$i:/etc/my.cnf.d/; done
启动集群
初始化cluster,不要在其他节点上执行该命令。当集群中所有节点都关闭后,需要重新初始化。
mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql --wsrep-new-cluster &
启动其他节点数据库:
mysqld_safe --defaults-file=/etc/my.cnf.d/server.cnf --user=mysql &
其他可能涉及到的优化
修改hosts:
echo -e "mariadb-a 10.10.100.101\nmariadb-b 10.10.100.102\nmariadb-c 10.10.100.103" >> /etc/hosts
在/etc/security/limits.conf添加:
* soft nofile 65536 * hard nofile 65536
在/etc/sysctl.conf添加:
fs.file-max = 655350 net.ipv4.ip_local_port_range = 1025 65000 net.ipv4.tcp_tw_recycle = 1
问题思考
1.如果出现网络故障导致脑裂,该如何处理?
可以尝试:set global wsrep_provider_options="pc.bootstrap=true";
2.使用xtrabackup方式同步时,需要注意哪些事项?
每个MariaDB需要创建用于同步数据的账号
3.当所有节点都关闭后,怎么重启Cluster?
首先,在任意一台主机上编辑/var/lib/mysql/grastate.dat,修改 seqno 的值,将“-1”改为“1”
然后,正常启动集群“galera_new_cluster”
最后,通过“/etc/init.d/mysql start”启动其他节点
搭建Haproxy+Keepalived
安装过程略,搭建成功后,应用通过VIP连接数据库,由Haproxy担任负载均衡、Galera担任数据同步。
配置Haproxy
修改配置/etc/haproxy/haproxy.cfg:
global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon spread-checks 3 tune.bufsize 32768 tune.maxrewrite 1024 tune.ssl.default-dh-param 2048 stats socket /var/lib/haproxy/stats defaults log global log 127.0.0.1 local3 mode http option tcplog option dontlognull retries 10 option redispatch timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen mariadb bind 10.10.100.200:3306 mode tcp balance roundrobin server mariadb-a 10.10.100.101:3306 weight 5 server mariadb-b 10.10.100.102:3306 weight 5 server mariadb-c 10.10.100.103:3306 weight 5 #balance source #hash-type consistent #option tcplog #option clitcpka #option srvtcpka #option httpchk #timeout client 28800s #timeout server 28800s #server mariadb-a 10.10.100.101:3306 inter 30s fastinter 3s downinter 3s rise 3 fall 3 #server mariadb-b 10.10.100.102:3306 inter 30s fastinter 3s downinter 3s rise 3 fall 3 backup #server mariadb-c 10.10.100.103:3306 inter 30s fastinter 3s listen stats bind 10.10.100.200:1080 mode http option httplog maxconn 10 stats enable stats refresh 30s stats uri /stats stats realm MaCluster\ Haproxy stats auth admin:admin stats hide-version stats admin if TRUE
配置keepalived
为了防止脑裂,将两台Keepalive的state都设为BACKUP、proiority必须小于100,修改配置/etc/keepalived/keepalived.conf:
global_defs { router_id HAPROXY } vrrp_script chk_haproxy { script "/etc/keepalived/chk_haproxy.sh" interval 1 weight 2 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 201 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy } virtual_ipaddress { 10.10.100.200/24 } track_interface { ens33 } }
注意:需要修改interface(如果网卡名不同)、priority和virtual_router_id。
配置监控脚本
Haproxy的进程监控脚本,如果haproxy进程不存在,则自动关闭keepalived。
网上有说监控脚本不执行,但本人的配置和脚本是正常的,有图为证:
编辑脚本/etc/keepalived/chk_haproxy.sh:
#!/bin/bash chkha=`ps -C haproxy --no-header |wc -l` if [ $chkha -eq 0 ];then systemctl stop keepalived fi
原创文章禁止转载:技术学堂 » Galera+Haproxy+Keepalived搭建MariaDB集群