Mariadb Galera Cluster 集群搭建

站在巨人的肩膀上

part.0 环境信息

操作系统: centos7.4
集群节点数: 3
主机信息:
192.168.1.217 node1 selinux=disable
192.168.1.227 node2 selinux=disable
192.168.1.238 node3 selinux=disable

part.1 集群搭建步骤

1.1 修改hosts文件信息

$ vi /etc/hosts
192.168.1.217 node1
192.168.1.227 node2
192.168.1.238 node3

1.2 引入mariadb repo 文件

$ vi /etc/yum.repo.d/mariadb.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

然后进行安装

$ sudo yum install MariaDB-server MariaDB-client galera

1.3 mariadb 初始化与权限分配

systemctl start mariadb
mysql_secure_installation 

在初始化过程中,可以对root密码进行修改等操作
然后对用户进行授权

mysql> grant all privileges on *.* to root@'%' identified by '密码';
mysql> flush privileges;

1.4 配置 galera

$ vi /etc/my.cnf.d/server.cnf

[galera]
wsrep_on=ON 
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.217,192.168.1.227,192.168.1.238"
wsrep_node_name= node1
wsrep_node_address=192.168.1.217
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=120M
wsrep_sst_method=rsync
wsrep_causal_reads=ON

此文件在三个节点中都需要修改。并且根据节点的信息来修改wsrep_node_address和wsrep_node_name两项,修改为相应节点的hostname和ip。

part.2 启动集群

启动 MariaDB Galera Cluster 服务:

$ galera_new_cluster
$ systemctl start mariadb

启动后可以查看集群的状态,集群服务将使用4567和3306两个端口。

$ netstat -tulpn | grep -e 4567 -e 3306
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      13436/mysqld        
tcp6       0      0 :::3306                 :::*                    LISTEN      13436/mysqld  

其他节点的启动方式相同。

part.3 验证集群是否正常

3.1 查看galera插件

MariaDB [(none)]>  show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready   | ON    |
+---------------+-------+

3.2 查看集群集齐数

MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+

3.3 查看集群状态

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           | 6                                                        |
| wsrep_cert_deps_distance     | 0.000000                                                 |
| wsrep_cert_index_size        | 0                                                        |
| wsrep_cert_interval          | 0.000000                                                 |
| wsrep_cluster_conf_id        | 7                                                        |
| wsrep_cluster_size           | 3                                                        | 集群成员
| wsrep_cluster_state_uuid     | f16cdbb3-495a-11e9-8b44-df48c6cbb633                     | 集群唯一标识
| wsrep_cluster_status         | Primary                                                  | 主服务器
| wsrep_cluster_weight         | 3                                                        | 
| 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             | c81b04a3-495b-11e9-9b95-3bff3ad9deb1                     |
| wsrep_incoming_addresses     | 192.168.1.227:3306,192.168.1.238:3306,192.168.1.217:3306 | 连接中的数据库
| wsrep_last_committed         | 6                                                        | sql提交记录
| wsrep_local_bf_aborts        | 0                                                        | 执行事务过程被本地中断
| wsrep_local_cached_downto    | 18446744073709551615                                     |
| wsrep_local_cert_failures    | 0                                                        |
| wsrep_local_commits          | 0                                                        |
| wsrep_local_index            | 0                                                        |
| wsrep_local_recv_queue       | 0                                                        |
| wsrep_local_recv_queue_avg   | 0.000000                                                 |
| wsrep_local_recv_queue_max   | 1                                                        |
| 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       | f16cdbb3-495a-11e9-8b44-df48c6cbb633                     | 集群ID
| wsrep_open_connections       | 0                                                        |
| wsrep_open_transactions      | 0                                                        |
| wsrep_protocol_version       | 9                                                        |
| wsrep_provider_name          | Galera                                                   |
| wsrep_provider_vendor        | Codership Oy                         |
| wsrep_provider_version       | 25.3.25(r3836)                                           |
| wsrep_ready                  | ON                                                       |
| wsrep_received               | 4                                                        |
| wsrep_received_bytes         | 494                                                      |
| wsrep_repl_data_bytes        | 0                                                        |
| wsrep_repl_keys              | 0                                                        |
| wsrep_repl_keys_bytes        | 0                                                        |
| wsrep_repl_other_bytes       | 0                                                        |
| wsrep_replicated             | 0                                                        |
| wsrep_replicated_bytes       | 0                                                        |
| wsrep_thread_count           | 2                                                        |
+------------------------------+----------------------------------------------------------+

3.4 查看连接的主机

MariaDB [(none)]> show status like "wsrep_incoming_addresses";
+--------------------------+----------------------------------------------------------+
| Variable_name            | Value                                                    |
+--------------------------+----------------------------------------------------------+
| wsrep_incoming_addresses | 192.168.1.227:3306,192.168.1.238:3306,192.168.1.217:3306 |
+--------------------------+----------------------------------------------------------+

3.5 测试是否能同步

在node1节点创建一个数据库,然后在其他节点进行验证。

MariaDB [(none)]> create database tests;
Query OK, 1 row affected (0.010 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| tests              |
| mysql              |
| performance_schema |
+--------------------+

在其他节点使用show databases,查看是否同样有tests数据库。

补充:

异常问题1:galera主机非正常关机,比如遭遇停电等情况。
异常处理: 可直接开启galera集群的主服务器主机的mariadb服务,在开启成员主机的mariadb服务。

异常问题2 :galera集群的主服务器和成员主机的mysql服务无法启动。
异常处理: 删除garlera主服务器的/var/lib/mysql/grastate.dat状态文件,galera_new_cluster启动服务。启动正常。登录并查看wsrep状态。删除galera成员主机中的/var/lib/mysql/grastate.dat状态文件,systemctl restart mariadb重启服务。启动正常。登录并查看wsrep状态。

你可能感兴趣的:(Mariadb Galera Cluster 集群搭建)