@Alex-Zhao
2018-04-17T15:23:56.000000Z
字数 5322
阅读 79
数据库
linuxadmin.io/Galera Cluster MariaDB Configuration On CentOS7
Galera集群是一个真正使用同步复制的多主mysql集群,它允许任意节点作为master节点以及自动添加节点和删除节点。这种多主集群配置不同于传统的MySQL主从,它能更好的提供负载均衡和容错功能。以下内容将帮助你配置基本的Galera集群和MariaDB在Centos7中。我们选择配置3个节点,这也是Galera集群的最小规模。如果你想配置两个节点作为集群,当其中一个节点出现故障或者脱离集群,另一个节点将停止工作。
首先在3个节点上分别安装MariaDB version 10.1
1. 配置yum源
vim /etc/yum.repos.d/MariaDB10.1.repo
-----
# MariaDB 10.1 CentOS repository list - created 2017-08-10 00:39 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
---
yum -y install MariaDB-server MariaDB-client MariaDB-common
mysql_secure_installation
yum install -y rsync lsof
systemctl enable mariadb
vim /etc/my.cnf.d/server.cnf
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
wsrep_on=ON #开启MariaDB的复制功能,该功能默认是关闭的
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.100,192.168.1.101,192.168.1.102" #主节点写最前边,逗号分隔集群中所有IP。
## Galera Cluster Configuration
wsrep_cluster_name="cluster1" #集群名称自己任意定义
## Galera Synchronization Configuration
wsrep_sst_method=rsync #同步复制方法,我们这里用rsync,官方文档有详细说明。
## Galera Node Configuration
wsrep_node_address="192.168.1.100" #当前节点IP
wsrep_node_name="centos7-vm1" #当前节点名称,自己任意定义需要集群中唯一,建议主机名
log_error=/var/log/mariadb.log
touch /var/log/mariadb.log
chown mysql:mysql /var/log/mariadb.log
galera_new_cluster
lsof -i:3306 #3306是MySQL client连接使用
lsof -i:4567 #用途数据库数据复制流量,本人在这里配置的时候 该端口并未开启,而是在其他节点开启后,主节点的4567才开启。
如果你想开启Centos 7中的firewall,需要做如下配置:
firewall-cmd --zone=public --add-service=mysql --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=4567/tcp --permanent
firewall-cmd --zone=public --add-port=4567/udp --permanent
firewall-cmd --reload #重新加载firewall
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
#显示如下信息说明集群中已经有一个节点。
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 1 |
+--------------------+-------+
wsrep_node_address="192.168.1.100"
wsrep_node_name="centos7-vm1"
systemctl start mariadb
进入数据库查看集群节点数
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
#当三个节点全部开启MariaDB服务,集群节点值显示3
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
你也可以查看log信息显示如下,说明添加节点成功:
WSREP: Member 1.0 (centos7-vm2) synced with group.
当节点离开,log信息也会有显示:
WSREP: forgetting 96a5eca6 (tcp://192.168.1.101:4567)
WSREP: New COMPONENT: primary = yes, bootstrap = no, my_idx = 0, memb_num = 2
在数据库中也能看到Galera集群的所有配置信息:
MariaDB [(none)]> show status like 'wsrep%';
+------------------------------+--------------------------------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 0 |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_cert_index_size | 2 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 6173c852-7ca0-11e7-8d8e-0e2551d18de1 |
| wsrep_cluster_status | Primary |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_connected | ON |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_flow_control_paused | 0.000000 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_flow_control_sent | 0 |
| wsrep_gcomm_uuid | 87a5891a-7ca0-11e7-a3bb-fe31f8409645 |
| wsrep_incoming_addresses | 192.168.1.101:3306,192.168.1.7:3306,192.168.1.100:3306 |
| wsrep_last_committed | 2 |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_cached_downto | 1 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_commits | 0 |
| wsrep_local_index | 1 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_avg | 0.125000 |
| wsrep_local_recv_queue_max | 2 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_avg | 0.000000 |
| wsrep_local_send_queue_max | 1 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_local_state_uuid | 6173c852-7ca0-11e7-8d8e-0e2551d18de1 |
| wsrep_protocol_version | 7 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 25.3.20(r3703) |
| wsrep_ready | ON |
| wsrep_received | 8 |
| wsrep_received_bytes | 1169 |
| wsrep_repl_data_bytes | 359 |
| wsrep_repl_keys | 1 |
| wsrep_repl_keys_bytes | 31 |
| wsrep_repl_other_bytes | 0 |
| wsrep_replicated | 1 |
| wsrep_replicated_bytes | 454 |
| wsrep_thread_count | 2 |
+------------------------------+--------------------------------------------------------+
mysql -u root -p -e "create database galera_test;"
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| galera_test |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)