Home  >  Article  >  Database  >  MySQL 多主多活 Galera 集群部署使用_MySQL

MySQL 多主多活 Galera 集群部署使用_MySQL

WBOY
WBOYOriginal
2016-05-30 17:10:041081browse

Galera是一款可以让MySQL同步复制的工具,实现真正的双主、多主,客户端连接到不同的MySQL实例进行读写操作就跟操作同一个实例一样,多个MySQL之间的数据是完全一致的。Galera主要是在保证数据一致性的前提下提高整体读的吞吐量,因此比MySQL自带主从方式更好。

 

Galera并没有分表分库功能,如果想要使用Galera,又想分表分库,可与Cobar结合使用,Cobar下如果使用Galara可以获得更高的可靠性,数据一致性,避免有些Cobar切换了主从,而另外一些没切换带来的数据不一致问题。

 

 

第一步:下载安装

 

使用galera时,还需要一个Galera库,官方下载地址是http://galeracluster.com/downloads/  。

Galera需要的MySQL/MariaDB版本有一个额外wsrep的补丁,因此不能直接用普通的版本。MySQL的版本可以从Galera的官网下载到,目前提供5.5和5.6两个版本。

MariaDB的版本可以从MariaDB的官网下载到https://downloads.mariadb.org/mariadb-galera/  ,MariaDB的10.0对应MySQL的5.6。

 

源码编译galera前需要安装的依赖库有boost,scons,check,openssl的开发版,您可根据自己的情况安装。galera使用scons安装,因此跟常规的命令不同,其实只要执行一个脚本即可。

 

$ scripts/build.sh

编译后在当前目录下生成libgalera_smm.so文件。

 

 

第二步:配置

 

Galera只需要在MySQL的配置文件my.cnf中增加几行wsrep相关内容即可

 

[mysqld]

# 配置前面编译出来的libgalera_smm.so文件路径

wsrep_provider  = /home/lyw/c2/galera-3-25.3.12/libgalera_smm.so

 

# 配置整个集群各实例的ip:port

wsrep_cluster_address   = 'gcomm://192.168.1.8:24011,192.168.1.8:24021,192.168.1.8:24031'

 

# 配置自己的ip:port,每个配置各不相同

wsrep_node_address      = '192.168.1.8:24011'

其他配置根据自己的情况配置。我这里配置了3个,my11.cnf,my21.cnf,my31.cnf。

 

 

第三步:初始化

 

初始化同MySQL一样,对每个数据库执行一下 mysql_install_db 脚本即可:

 

$ scripts/mysql_install_db --defaults-file=etc/my11.cnf

$ scripts/mysql_install_db --defaults-file=etc/my21.cnf

$ scripts/mysql_install_db --defaults-file=etc/my31.cnf

 

第四步:启动

 

前面配置的三台MySQL,我们先启动第一台,需要设置 --wsrep_cluster_address=gcomm:// 参数:

 

$ bin/mysqld_safe --defaults-file=etc/my11.cnf --wsrep_cluster_address=gcomm:// &

 

等待第一台启动成功后,我们启动后面两台,这个时候不需要上面的参数,实际上是使用了配置文件中的值:

 

$ bin/mysqld_safe --defaults-file=etc/my21.cnf  &

$ bin/mysqld_safe --defaults-file=etc/my31.cnf  &

这样就整个集群就启动好了,以后如果有某进程挂了,需要重启,就不需要加--wsrep_cluster_address参数。

 

 

第五步:测试

 

启动好后我们要测试下是否真的成功。我这里使用mysql客户端,您可使用自己喜欢的客户端工具,注意这里连接的端口是MySQL的端口,而不是上面Galera的端口。

$ bin/mysql -uroot -h127.0.0.1 -P14011
MariaDB [(none)]> create database lyw;

$ bin/mysql -uroot -h127.0.0.1 -P14021
MariaDB [(none)]> show databases like 'lyw';
+----------------+
| Database (lyw) |
+----------------+
| lyw            |
+----------------+
1 row in set (0.00 sec)

$ bin/mysql -uroot -h127.0.0.1 -P14031
MariaDB [(none)]> show databases like 'lyw';
+----------------+
| Database (lyw) |
+----------------+
| lyw            |
+----------------+
1 row in set (0.00 sec)

 

 

可见在一个库里作了一个操作,在其他库中也有了相同的修改,Galera启动成功。

我们也可以查看wsrep相关的参数判断是否启动成功:

 

MariaDB [(none)]> show variables like 'wsrep_on';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_on      | ON    |
+---------------+-------+

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

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

 

wsrep_on 值为ON则说明启动成功。

wsrep_connected值为ON说明连接到了集群。

wsrep_ready值为ON说明已经准备好接受SQL请求了。该值最关键。

wsrep打头的参数都是跟galera相关的,其他的在使用中慢慢研究了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn