简介
Galera Cluster for MySQL是一种同步复制解决方案,可以提高MySQL服务的可用性和性能。所有Galera Cluster节点都是相同的,完全代表集群,允许无约束的透明mysql客户端访问,充当单分布式MySQL服务器。它提供:
- 透明的客户端连接,因此它与现有应用程序高度兼容;
- 同步数据安全语义 - 如果客户端收到确认,将在每个节点上提交事务;
- 自动写入冲突检测和解决,使节点始终保持一致。
特性:
- 同步复制 Synchronous replication
- Active-active multi-master 拓扑逻辑
- 可对集群中任一节点进行数据读写
- 自动成员控制,故障节点自动从集群中移除
- 自动节点加入
- 真正并行的复制,基于行级
- 直接客户端连接,原生的 MySQL 接口
- 每个节点都包含完整的数据副本
- 多台数据库中数据同步由 wsrep 接口实现
废话不多说直接开整
安装
官方资源
资源下载地址:http://galeracluster.com/downloads/#downloads
技术说明:http://galeracluster.com/documentation-webpages/technicaldescription.html
GALERA集群文档:http://galeracluster.com/documentation-webpages/index.html
下载资源文件
MariaDB-10.1.36-centos73-x86_64-compat.rpm
MariaDB-10.1.36-centos73-x86_64-common.rpm
MariaDB-10.1.36-centos73-x86_64-client.rpm
boost-program-options-1.53.0-27.el7.x86_64.rpm
mariadb-libs-5.5.60-1.el7_5.x86_64.rpm
MariaDB-10.1.36-centos73-x86_64-compat.rpm
MariaDB-10.1.36-centos73-x86_64-server.rpm
节点环境
节点IP
Node1 IP192.168.0.1
Node2 IP198.168.0.2
Node3 IP192.168.0.3
依次安装所有rpm包(三个节点安装步骤一致)rpm -ivh MariaDB-10.1.36-centos73-x86_64-compat.rpm
rpm -ivh xxxxxxxx.rpm
节点安装完成后开始配置初始化数据库
启动数据库[root@F8SFRZ ~]$ systemctl start mariadb
初始化数据库密码[root@F8SFRZ ~]$ mysql_secure_installation
设置允许mysql远程调用
mysql -uroot -p
输入密码
回车
mysql> use mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%'IDENTIFIED BY '密码' WITH GRANT OPTION;
mysql> flush privileges;
可以找任意一款数据库客户端连接工具验证远程调用配置是否生效博主用的是Navicat Premium 12
修改 /etc/my.cnf
[root@F8SFRZ ~]$ vim /etc/my.cnf
添加 : !includedir /etc/my.cnf.d
Esc + :wq 保存退出
效果如下图所示
配置Galera server.cnf
[root@F8SFRZ ~]$ cd /etc/my.cnf.d/
[root@F8SFRZ ~]$ ls
auth_gssapi.cnf client.cnf cracklib_password_check.cnf mysql-clients.cnf server.cnf
cassandra.cnf connect.cnf enable_encryption.preset oqgraph.cnf tokudb.cnf
[root@F8SFRZ ~]$ vim server.cnf
[galera]
wsrep_on=ON #定义是否从当前会话进行更新复制
wsrep_provider=/usr/lib64/galera/libgalera_smm.so #定义Galera复制插件的路径
wsrep_cluster_address="gcomm://192.168.0.1,192.168.0.2,192.168.0.3"
wsrep_node_name= node1 #定义节点的逻辑集群名称
wsrep_node_address=192.168.0.1 #定义节点的IP地址
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 #允许在非事务性读取上强制执行严格的群集范围的语义。导致较大的读取延
迟。READ COMMITTED
具体属性含义请参考官方文档
http://galeracluster.com/documentation-webpages/mysqlwsrepoptions.html#wsrep-cluster-address
这里就不做逐一介绍了
Node2、Node3节点的配置除【wsrep_node_name、wsrep_node_address】改为相应节点的【IP】和【节点名称】外其余一样
配置完成开始启动集群服务
启动 MariaDB-Galera-Cluster
注意:如果在验证集群时发现集群状态为OFF,可以尝试先将数据库服务停止,再启动集群,再启动数据库服务
第一个启动的节点
[root@F8SFRZ ~]$ /bin/galera_new_cluster
[root@F8SFRZ ~]$ systemctl start mariadb
第二个启动的节点
[root@F8SFRZ ~]$ /bin/galera_new_cluster
[root@F8SFRZ ~]$ systemctl start mariadb
第三个启动的节点
[root@F8SFRZ ~]$ /bin/galera_new_cluster
[root@F8SFRZ ~]$ systemctl start mariadb
验证集群
查看Galera是否生效:show status like "wsrep_ready"
查看集群数量:show status like “wsrep_cluster_size”;
查看集群状态:show status like “wsrep%”;
查看在线主机:show status like “wsrep_incoming_addresses”;
[root@F8SFRZ ~]$mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 909
Server version: 10.1.36-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON | ON:启动 OFF:未启动
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 2 |
+--------------------+-------+
1 row in set (0.00 sec)
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 | 112 |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_cert_index_size | 636 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 4 |
| wsrep_cluster_size | 2 |
| wsrep_cluster_state_uuid | 9a9861c4-3a57-11e9-aa90-ebcd6c18b14f |
| 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 | 5fcaa3ef-3a59-11e9-ac9c-17e0602c0385 |
| wsrep_incoming_addresses | 192.168.0.1:3306,192.168.0.2:3306,192.168.0.3:3306 |
| wsrep_last_committed | 113 |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_cached_downto | 4 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_commits | 20 |
| 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.008850 |
| wsrep_local_send_queue_max | 2 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_local_state_uuid | 9a9861c4-3a57-11e9-aa90-ebcd6c18b14f |
| wsrep_protocol_version | 7 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <[email protected]> |
| wsrep_provider_version | 25.3.22(r3764) |
| wsrep_ready | ON |
| wsrep_received | 5 |
| wsrep_received_bytes | 717 |
| wsrep_repl_data_bytes | 1216105 |
| wsrep_repl_keys | 1670 |
| wsrep_repl_keys_bytes | 15879 |
| wsrep_repl_other_bytes | 0 |
| wsrep_replicated | 109 |
| wsrep_replicated_bytes | 1238960 |
| wsrep_thread_count | 2 |
+------------------------------+--------------------------------------+
58 rows in set (0.00 sec)
MariaDB [(none)]> exit
[root@F8SFRZ ~]$
[root@F8SFRZ ~]$
[root@F8SFRZ ~]$
[root@F8SFRZ ~]$
测试数据库集群同步
[root@F8SFRZ ~]$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 909
Server version: 10.1.36-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database testGalera;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testGalera |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> exit
连接到Node2、Node3节点,连接mysql,查看【testGalera】同步
注意:Galera最好是奇数台,3台起步,因为集群对外提供服务需要保持在半数以上才能对外提供服务(zokeerper同理)
MariaDB Galera局限性
- 目前的复制仅仅支持InnoDB存储引擎,任何写入其他引擎的表,包括mysql.*表将不会复制,但是DDL语句会被复制的,因此创建用户将会被复制,但是insert into mysql.user…将不会被复制的;
- DELETE操作不支持没有主键的表,没有主键的表在不同的节点顺序将不同,如果执行SELECT…LIMIT… 将出现不同的结果集.
- 在多主环境下LOCK/UNLOCK TABLES不支持,以及锁函数GET_LOCK(), RELEASE_LOCK();
- 查询日志不能保存在表中。如果开启查询日志,只能保存到文件中;
- 允许最大的事务大小由wsrep_max_ws_rows和wsrep_max_ws_size定义。任何大型操作将被拒绝。如大型的LOAD DATA操作;
- 如果DDL语句有问题将破坏集群