centos7 系统上 mariadb galera 集群搭建

时间:2022-09-19 20:25:15

1.环境

CentOS Linux release 7.3
MariaDB 10.1.25

2.安装MariaDB

配置mariadb10.1的yum源
[root@centos7-compute1 ~]# cat /etc/yum.repos.d/MariaDB.repo
[root@centos7-compute2 ~]# cat /etc/yum.repos.d/MariaDB.repo
[root@centos7-compute3 ~]# cat /etc/yum.repos.d/MariaDB.repo
MariaDB.repo:
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

3.使用yum安装

[root@centos7-compute1 ~]# sudo yum install MariaDB-server MariaDB-client galera
[root@centos7-compute2 ~]# sudo yum install MariaDB-server MariaDB-client galera
[root@centos7-compute3 ~]# sudo yum install MariaDB-server MariaDB-client galera

4.安全配置

[root@centos7-compute1 ~]# /usr/bin/mysql_secure_installation
[root@centos7-compute2 ~]# /usr/bin/mysql_secure_installation
[root@centos7-compute3 ~]# /usr/bin/mysql_secure_installation
compute1,compute2,compute3 三节点 启动MariaDB并赋权:
grant all privileges on *.* to root@"%" identified by "123456";
flush privileges;
然后全部节点关闭数据库
[root@centos7-compute1 ~]# systemctl stop mariadb
[root@centos7-compute2 ~]# systemctl stop mariadb
[root@centos7-compute3 ~]# systemctl stop mariadb
注意:此时需要全部节点关闭selinx,防火墙。防止接下来的影响集群通讯
 systemctl stop firewalld
systemctl disable firewalld

5.配置MariaDB Galera Cluster

修改三台节点上的/etc/my.cnf.d/server.cnf 文件
compute1 配置如下:
[mysqld]

[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='my_wsrep_cluster'
wsrep_cluster_address = "gcomm://192.168.140.197,192.168.140.141,192.168.140.192"
wsrep_node_name = centos7-compute1
wsrep_node_address=192.168.140.197
wsrep_on=ON
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_method=rsync
wsrep_sst_auth=root:123456

[embedded]

[mariadb]

[mariadb-10.1]

compute2 配置如下:

[mysqld]

[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='my_wsrep_cluster'
wsrep_cluster_address = "gcomm://192.168.140.197,192.168.140.141,192.168.140.192"
wsrep_node_name = centos7-compute2
wsrep_node_address=192.168.140.141
wsrep_on=ON
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_method=rsync
wsrep_sst_auth=root:123456

[embedded]

[mariadb]

[mariadb-10.1]

compute3 配置如下:
[mysqld]

[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name='my_wsrep_cluster'
wsrep_cluster_address = "gcomm://192.168.140.197,192.168.140.141,192.168.140.192"
wsrep_node_name = centos7-compute3
wsrep_node_address=192.168.140.192
wsrep_on=ON
binlog_format=ROW
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_method=rsync
wsrep_sst_auth=root:123456

[embedded]

[mariadb]

[mariadb-10.1]

6.启动集群

引导集群创建
只在compute1操作:
[root@centos7-compute1 ~]# /usr/sbin/mysqld --wsrep-new-cluster --user=root &

查看集群状态

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


MariaDB [(none)]> show status like "wsrep%";
+------------------------------+------------------------------------------------+
| Variable_name | Value |
+------------------------------+------------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 0.000000 |
| wsrep_causal_reads | 0 |
| wsrep_cert_deps_distance | 0.000000 |
| wsrep_cert_index_size | 0 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 1 |
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | 1e434901-71b5-11e7-b190-7bb2f4bbed7a |
| wsrep_cluster_status | Primary |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 0.000000 |
| wsrep_connected | ON |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 5.592e-06/1.25208e-05/2.5685e-05/8.62896e-06/5 |
| 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 | 35623b8e-71ad-11e7-af9f-52f25b42ebcf |
| wsrep_incoming_addresses | 192.168.140.197:3306 |
| wsrep_last_committed | 0 |
| 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.500000 |
| 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 | 1e434901-71b5-11e7-b190-7bb2f4bbed7a |
| 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 | 2 |
| wsrep_received_bytes | 155 |
| 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 |
+------------------------------+------------------------------------------------+

向集群中添加其他节点:

[root@centos7-compute2 ~]# systemctl start mariadb
[root@centos7-compute3 ~]# systemctl start mariadb

查看集群状态:

MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.00 sec)

7.测试数据同步

在 compute1上创建数据库,表并插入数据,观察compute2,compute3 数据情况
MariaDB [(none)]> create database galera;
MariaDB [galera]> create table t (id int primary key);
insert into t value(1);
insert into t value(2);
insert into t value(3);

查看compute2,compute3数据库

MariaDB [galera]> show tables;
+------------------+
| Tables_in_galera |
+------------------+
| t |
+------------------+
1 row in set (0.00 sec)
MariaDB [galera]> select * from t;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+

经过测试,删除数据库,增删查改表都可以实时同步。


8.故障测试

[root@centos7-compute3 ~]# systemctl stop mariadb
然后在compute1,compute2 分别插入数据时,等待compute3启动后自己会同步

并且此时集群节点为两个。

MariaDB [galera]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 2 |
+--------------------+-------+



参考文档http://galeracluster.com/documentation-webpages/galerainstallation.html

mariadb repo :https://downloads.mariadb.org/mariadb/repositories/#mirror=neusoft&distro=CentOS&distro_release=centos7-amd64--centos7&version=10.1