MariaDB数据库介绍之四、Galera Cluster

一、Galera Cluster

       Galera是一个MySQL(也支持MariaDB,Percona)的同步多主集群软件,Galera集群的复制功能基于Galeralibrary实现,为了让MySQL与Galera library通讯,特别针对MySQL开发了wsrep API。


二、Galera  Cluster的功能和优势   

功能:

            multi-master,即所有节点可以同时读写数据库

            自动的节点成员控制,失效节点自动被清除

            新节点加入数据自动复制

            并行复制,行级

优势:

            因为是multi-master,所以不存在延迟;更小的客户端延迟

            同时具有读和写的扩展能力

            节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的


三、实现Galera Cluster过程;简单实现

1、准备工作

①测试环境: CentOS 7 ;

node1 172.16.116.231
node2 172.16.116.232
node3 172.16.116.233
//同步各节点的时间 
# ntpdate ntp.sjtu.edu.cn 

//主机互信 
# vim /etc/hosts
172.16.116.231 node1
172.16.116.232 node2
172.16.116.233 node3

//名称服务器
# vim /etc/resolv.conf
nameserver 172.16.0.1


②安装包选择

galera-25.3.9-1.rhel7.el7.centos.x86_64.rpm
MariaDB-5.5.46-centos7-x86_64-client.rpm
MariaDB-5.5.46-centos7-x86_64-common.rpm
MariaDB-Galera-5.5.46-centos7-x86_64-server.rpm


2、各节点安装上述的软件包

# yum install -y *rpm
//安装完成,配置node1
[root@node1 ~]# vim /etc/my.cnf.d/server.cnf 
[galera]
# Mandatory settings
wsrep_provider=/usr/lib64/galera/libgalera_smm.so   
wsrep_cluster_address=gcomm://172.16.116.231,172.16.116.232,172.16.116.233  //定义节点
wsrep_cluster_name=Mycluster          //集群名称 
binlog_format=row                    //二进制格式为row
default_storage_engine=InnoDB        //默认存储引擎为innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0                //绑定ip 

//启动服务;要加上--wsrep-new-cluster 这个参数,否则为报错error
[root@node1 ~]# /etc/init.d/mysql start --wsrep-new-cluster    
Starting MySQL.... SUCCESS!


3、启动后查看的结果

########将/etc/my.cnf.d/server.cnf远程复制给node2,node3,启动node2,node3
[root@node2 ~]# /etc/init.d/mysql start
Starting MySQL......SST in progress, setting sleep higher. SUCCESS! 
[root@node3 ~]# /etc/init.d/mysql start
Starting MySQL......SST in progress, setting sleep higher. SUCCESS! 

#####回到node1节点,进入mysql
MariaDB [(none)]> show status like'wsrep_%';
+------------------------------+-------------------------------------------------------------+
| Variable_name                | Value                                                       |
+------------------------------+-------------------------------------------------------------+
| wsrep_local_state_uuid       | 198c2664-8f88-11e5-bf9c-0e81c9575644                        |
| wsrep_protocol_version       | 7                                                           |
| wsrep_last_committed         | 0                                                           |
| wsrep_replicated             | 0                                                           |
| wsrep_replicated_bytes       | 0                                                           |
| wsrep_repl_keys              | 0                                                           |
| wsrep_repl_keys_bytes        | 0                                                           |
| wsrep_repl_data_bytes        | 0                                                           |
| wsrep_repl_other_bytes       | 0                                                           |
| wsrep_received               | 10                                                          |
| wsrep_received_bytes         | 758                                                         |
| wsrep_local_commits          | 0                                                           |
| wsrep_local_cert_failures    | 0                                                           |
| wsrep_local_replays          | 0                                                           |
| wsrep_local_send_queue       | 0                                                           |
| wsrep_local_send_queue_max   | 1                                                           |
| wsrep_local_send_queue_min   | 0                                                           |
| wsrep_local_send_queue_avg   | 0.000000                                                    |
| wsrep_local_recv_queue       | 0                                                           |
| wsrep_local_recv_queue_max   | 1                                                           |
| wsrep_local_recv_queue_min   | 0                                                           |
| wsrep_local_recv_queue_avg   | 0.000000                                                    |
| wsrep_local_cached_downto    | 18446744073709551615                                        |
| wsrep_flow_control_paused_ns | 0                                                           |
| wsrep_flow_control_paused    | 0.000000                                                    |
| wsrep_flow_control_sent      | 0                                                           |
| wsrep_flow_control_recv      | 0                                                           |
| wsrep_cert_deps_distance     | 0.000000                                                    |
| wsrep_apply_oooe             | 0.000000                                                    |
| wsrep_apply_oool             | 0.000000                                                    |
| wsrep_apply_window           | 0.000000                                                    |
| wsrep_commit_oooe            | 0.000000                                                    |
| wsrep_commit_oool            | 0.000000                                                    |
| wsrep_commit_window          | 0.000000                                                    |
| wsrep_local_state            | 4                                                           |
| wsrep_local_state_comment    | Synced                                                      |
| wsrep_cert_index_size        | 0                                                           |
| wsrep_causal_reads           | 0                                                           |
| wsrep_cert_interval          | 0.000000                                                    |
| wsrep_incoming_addresses     | 172.16.116.231:3306,172.16.116.232:3306,172.16.116.233:3306 |
| wsrep_evs_delayed            |                                                             |
| wsrep_evs_evict_list         |                                                             |
| wsrep_evs_repl_latency       | 0/0/0/0/0                                                   |
| wsrep_evs_state              | OPERATIONAL                                                 |
| wsrep_gcomm_uuid             | 198b3495-8f88-11e5-8b30-6a22054488b1                        |
| wsrep_cluster_conf_id        | 3                                                           |
| wsrep_cluster_size           | 3                                                           |
| wsrep_cluster_state_uuid     | 198c2664-8f88-11e5-bf9c-0e81c9575644                        |
| wsrep_cluster_status         | Primary                                                     |
| wsrep_connected              | ON                                                          |
| wsrep_local_bf_aborts        | 0                                                           |
| wsrep_local_index            | 0                                                           |
| wsrep_provider_name          | Galera                                                      |
| wsrep_provider_vendor        | Codership Oy <[email protected]>                           |
| wsrep_provider_version       | 25.3.9(r3387)                                               |
| wsrep_ready                  | ON                                                          |
| wsrep_thread_count           | 2                                                           |
+------------------------------+-------------------------------------------------------------+


4、测试

///node1中添加1个数据库
MariaDB [(none)]> create database node1;
Query OK, 1 row affected (0.01 sec)

///node2上查看
[root@node2 ~]# mysql
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| node1              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

//node3上向node1数据库中添加1个表test3,并插入数据
MariaDB [(none)]> use node1;
Database changed
MariaDB [node1]> create table test3(id int unsigned auto_increment not null primary key,name char(10));
Query OK, 0 rows affected (0.02 sec)
MariaDB [node1]> insert into test3 (name) value ('zhang'),('wang');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0   

//回到node1上查看,并插入新的数据
MariaDB [(none)]> select * from node1.test3;
+----+-------+
| id | name  |
+----+-------+
|  3 | zhang |
|  6 | wang  |
+----+-------+
2 rows in set (0.00 sec)
MariaDB [(none)]>  insert into node1.test3 (name) value ('li'),('chen');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

//在node2上查看
MariaDB [(none)]> select * from node1.test3;
+----+-------+
| id | name  |
+----+-------+
|  3 | zhang |
|  6 | wang  |
|  7 | li    |
| 10 | chen  |
+----+-------+
4 rows in set (0.00 sec)

通过上述测试可以看看3个节点都可以读写数据,即时同步;但对于自动增长的字段插入新数据,序号不连续,解决方法,只能定义全局的计数器来实现~

至此,Galera Cluster的高可用mysql集群介绍完毕!

后续工作,可以加入keepalived+haproxy实现对Galera Cluster的进一步扩展,O(∩_∩)O~~~


你可能感兴趣的:(mysql,cluster,galera,galera)