MariaDB虽然被视为MySQL数据库的替代品,但它在扩展功能、存储引擎以及一些新的功能改进方面都强过MySQL。谢谢张树杰同学为我们准备的这系列MariaDB。希望大家能够喜欢!
本期主要讲诉Galera Cluster的内部架构及新节点加入,前面系列文章:
1): MariaDB Galera Cluster 之一 Database Replication
2):MariaDB Galera Cluster 之二 MariaDB Galera Cluster主要功能、优缺点和对比
3):MariaDB Galera Cluster 之三 Galera Cluster的内部架构及新节点加入
演示环境:
Server version: 10.2.11-MariaDB MariaDB Server
CentOS Linux release 7.4.1708 (Core)
IP地址:
192.168.110.2
192.168.110.3
192.168.110.4
系统配置
A、修改/etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
B、修改/etc/sysctl.conf
fs.file-max=655350
net.ipv4.ip_local_port_range = 1025 65000
net.ipv4.tcp_tw_recycle = 1
最后执行sysctl -p
C、安装依赖的一些包,比如lsof,socat,openssl,tar等
安装MariaDB
设置mariadb的yum源:
touch /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.2 CentOS repository list - created 2017-11-22 03:38 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
#baseurl = http://yum.mariadb.org/10.2/centos7-amd64
#gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64/
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
安装Mariadb
yum install -y MariaDB-server MariaDB-client socat
安装Xtrabackup
yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum install -y percona-xtrabackup-24
配置 MariaDB Galera Cluster
分别修改MariaDB Galera集群的每个节点上的/etc/my.cnf.d/server.cnf文件,具体每个节点的内容如下:
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
character-set-server=utf8
# * Galera-related settings
#
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="reyzar"
wsrep_cluster_address="gcomm://192.168.110.2,192.168.110.3,192.168.110.4" #集群的IP地址
wsrep_node_name='node1'
wsrep_node_address=192.168.110.2
bind-address=192.168.110.2
wsrep_slave_threads=1
wsrep_certify_nonPK=1
wsrep_max_ws_rows=131072
wsrep_max_ws_size=1073741824
wsrep_debug=0
wsrep_convert_LOCK_to_trx=0
wsrep_retry_autocommit=1
wsrep_auto_increment_control=1
wsrep_drupal_282555_workaround=0
wsrep_causal_reads=0
wsrep_sst_method=xtrabackup-v2 #默认是rsync全量拷贝,但是需要在donor节点上执行全局读锁(flushtables with read lock),建议采用xtrabackup热备份方式,只有在备份.frm表结构文件才会锁表
wsrep_sst_auth=root:admin@123 #数据库账号密码
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
# Allow server to accept connections on all interfaces.
#bind-address=0.0.0.0
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
# This group is only read by MariaDB-10.1 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.1]
第二台设备与第三台设备的集群配置信息与上面相差不大,只需要修改监听的IP地址即可
启动集群
启动第一个节点:
/etc/init.d/mysql start --wsrep-new-cluster
或galera_new_cluster
启动其他节点采用:
systemctl start mariadb
验证集群
tu 1
tu 2
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+---------+
| Variable_name | Value |
+--------------------+---------+
| wsrep_cluster_size | 3 |
+--------------------+---------+
1 row in set (0.00 sec)
注释:
wsrep_cluster_status为Primary,表示节点为主节点,正常读写。
wsrep_ready为ON,表示集群正常运行。
wsrep_cluster_size为3,表示集群有三个节点。
总结
1. 在生产环境下应该避免使用大事务,不建议在高并发写入场景下使用Galera Cluster架构,会导致集群限流,从而引起整个集群hang住,出现生产故障。针对这种情况可以考虑主从,实现读写分离等手段。
2. 对数据一致性要求较高,并且数据写入不频繁,数据库容量也不大(50GB左右),网络状况良好的情况下,可以考虑使用Galera方案。
閱讀更多 大數據和雲計算技術 的文章