一、Galera Repplication
- Galera复制发生在事务提交时, 通过广播事务写集群应用 客户端直接连接到 Galera Cluster;
- DMBS就和连接到原生的DBMS一样 ;
- wsrep API, 定义了Galera和DBMS之间的接口;
- 多主模型机制,不再通过mysql协议通过读取二进制文件实现,而是通过wresp协议在全局实现底层数据片复制,任何一节点都可读写。
二、Galera Cluster的特点
Galera是为MySQL提供的一个真正意义上的多主(M/M)集群, Galera Cluster是一个易于使用的高可用解决方案, 可提高系统的平均无故障时间, 并且提供数据的可靠性和集群的可扩展性。分担读操作,前端没必要读写分离、只需做负载均衡即可。
三、Galera Cluster的强大功能
- 同步复制
- 多主模型
- 可以在任意节点读写
- 自动控制各个节点的升级和降级
- 行级的并行控制
- 规模较小的客户端延迟
- 网络状态可用情况下,节点间基本实时数据同步,完胜MHA的日志可能不同步
四、Galera-Cluster在网络拓扑所处的位置
五、相关说明
Galera集群不需要提前配置MySQL复制, 并且也不需要基于MySQL复制
安装MariaDB-Galera-Server前, 不能安装mariadb和mariadb-server(其实MariaDB-Gelera算是MariaDB的一个分支), 至少三个节点。
下载地址:https://downloads.mariadb.org/mariadb-galera/10.0.37/
六、环境部署
各节点的/etc/hosts 文件配置内容如下:
172.16.92.31 node1.change-can.com node1
172.16.92.32 node2.change-can.com node2
172.16.92.33 node3.change-can.com node3
node1- 3:
# vim /etc/yum.repos.d/gelera.repo
[galera]
name=Galera Cluster from MariaDB
baseurl=http://yum.mariadb.org/5.5.62/centos7-amd64/
gpgcheck=0
[galera]
name=Galera Cluster from MariaDB
baseurl=http://yum.mariadb.org/5.5.62/centos7-amd64/
gpgcheck=0
# yum install MariaDB-Galera-server -y
Node1:
[root@node1 ~]# rpm -ql galera | grep -i smm.so
/usr/lib64/galera/libgalera_smm.so
[root@node1 ~]# vim /etc/my.cnf.d/server.cnf
[galera]
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://172.16.92.31,172.16.92.32,172.16.92.33"
wsrep_cluster_name = "mycluster"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
#下面两个需要为每个节点分别指定为当前节点的名字及IP地址:配置中,可省略
#wsrep_node_name = "node1"
#wsrep_node_address = "172.16.92.31"
[root@node1 ~]# scp /etc/my.cnf.d/server.cnf node2:/etc/my.cnf.d/
[root@node1 ~]# scp /etc/my.cnf.d/server.cnf node3:/etc/my.cnf.d/
首次启动时,需要在其中一个节点上初始化集群,执行如下命令:
[root@node1 ~]# /etc/rc.d/init.d/mysql start --wsrep-new-cluster
Starting MariaDB.181226 14:27:54 mysqld_safe Logging to '/var/lib/mysql/node1.err'.
181226 14:27:54 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
..... SUCCESS!
而后正常启动其他节点即可:
Node2-3:
# /etc/init.d/mysql start
查看集群中相关的参数:
SHOW STATUS LIKE 'wsrep_%';
验证功能:
node1
[root@node1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.62-MariaDB-wsrep MariaDB Server, wsrep_25.24.r9949137
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 mydb;
Query OK, 1 row affected (0.05 sec)
node2:
[root@node2 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.62-MariaDB-wsrep MariaDB Server, wsrep_25.24.r9949137
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 DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
MariaDB [(none)]> use mydb;
MariaDB [mydb]> CREATE TABLE tb1 (id int, name char(10));
node3:
[root@node3 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.62-MariaDB-wsrep MariaDB Server, wsrep_25.24.r9949137
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)]> use mydb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mydb]> SHOW TABLES;
+----------------+
| Tables_in_mydb |
+----------------+
| tb1 |
+----------------+
1 row in set (0.00 sec)
MariaDB [mydb]> DESC tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
node1:
MariaDB [(none)]> use mydb;
MariaDB [mydb]> CREATE TABLE tb2 (id int UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, name CHAR(30));
MariaDB [mydb]> INSERT INTO tb2 (name) VALUES ('hi'),('hello');
node2:
MariaDB [mydb]> SELECT * FROM tb2;
+----+-------+
| id | name |
+----+-------+
| 4 | hi |
| 7 | hello |
+----+-------+
MariaDB [mydb]> INSERT INTO tb2 (name) VALUES ('to'),('from');
MariaDB [mydb]> SELECT * FROM tb2;
+----+-------+
| id | name |
+----+-------+
| 4 | hi |
| 7 | hello |
| 11 | to |
| 14 | from |
+----+-------+
七、其他
读写分离:
mysql-proxy 不建议使用,可能出现各种坑,除非有二次开发能力
复制的问题和解决方案:
(1)数据损坏或丢失:
Master:MHA + semi repl
Slave: 重新复制
(2)混合使用存储引擎:
MyISAM:不支持事务;
InnoDB:支持事务;
(3)不唯一的server id:改完配置之后,重新复制
(4)复制延迟:需要额外的监控工具的辅助
数据库服务衡量指标:
qps: query per second
tps: transaction per second