手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下MySQL/MariaDB Galera集群搭建过程

[日期:2016-01-10] 来源:Linux社区  作者:liujiduo [字体: ]

MariaDB介绍

MariaDB是开源社区维护的一个MySQL分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可证。

MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

详细介绍请参考链接:

http://mariadb.org/(官网)

Galera Cluster介绍

Galera Cluster是基于MySQL/innodb二次开发而成的一个支持“多主同步”的数据库主从集群,具有高可用,易于扩展等特点。

详细介绍请参考链接:

http://galeracluster.com/(官网)

本文使用的Linux发行版:CentOS6.7 下载地址:https://wiki.centos.org/Download

1.  添加yum源

[root@localhost ~]# vi /etc/yum.repos.d/CentOS-MariaDB.repo

添加如下几行:

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/rhel6-amd64
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1

2. 安装mariadb galera软件包

[root@localhost ~]# yum install MariaDB-Galera-server MariaDB-client galera

3. 修改防火墙配置

[root@localhost ~]# vi /etc/sysconfig/iptables

添加如下几行:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4444 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4567 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4568 -j ACCEPT

4. 重启防火墙功能

[root@localhost ~]# service iptables restart

5. 安装selinux管理工具

[root@localhost ~]# yum provides /usr/sbin/semanage
[root@localhost ~]# yum -y install policycoreutils-Python

6. 修改selinux安全策略

[root@localhost ~]# semanage port -a -t mysqld_port_t -p tcp 4567
[root@localhost ~]# semanage port -a -t mysqld_port_t -p tcp 4568
[root@localhost ~]# semanage permissive -a mysqld_t

7. 启动mysql服务

[root@localhost ~]# service mysql start

8. 执行mysql安全设置

[root@localhost ~]# mysql_secure_installation

(先设置root账户密码,再一直“y”下去即可)

9. 创建用于节点同步的账号

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> grant usage on *.* to sst@'%' identified by '123456';
MariaDB [(none)]> flush privileges;

10. 修改mysql默认字符集

MariaDB [(none)]> show variables like 'character%';
MariaDB [(none)]> set character_set_server = utf8;
MariaDB [(none)]> set character_set_database = utf8;

11. 修改集群节点配置

[root@localhost ~]# cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
[root@localhost ~]# vi /etc/my.cnf.d/wsrep.cnf

修改如下几行:

wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"    #集群节点N的地址(注意把前面的"#"删掉!)
wsrep_sst_auth=sst:123456    #节点N的数据库账户和密码
  • 参数说明

  "gcomm://" 是特殊的地址,仅仅是galera cluster初始化启动时候使用。
  如果集群启动以后,我们关闭了第一个节点,那么再次启动的时候必须先修改"gcomm://"为其他节点的集群地址,例如wsrep_cluster_address="gcomm://192.168.0.152"。

检查/etc/my.cnf中有没有!includedir /etc/my.cnf.d/这一行,没有则添加。

[root@localhost ~]# vi /etc/my.cnf

到这里,第1个节点的配置就完成了,然后在另一台主机上按照步骤1~11配置第2个节点,只需修改节点2的wsrep_cluster_address为节点1的IP即可,以此类推。

12. 启动集群节点

  • 检查mysql进程:[root@localhost ~]# ps aux|grep mysql
  • 停止mysql服务:[root@localhost ~]# service mysql stop
  • 启动第1个节点:[root@localhost ~]# service mysql bootstrap

  • 启动第2、3、...个节点:[root@localhost ~]# service mysql start

(注意:启动mysql之前先检查一下服务是否已经启动,不要重复启动,如果无法停止当前mysql服务则手动kill掉mysql的进程)

13. 检查集群运行状态

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> show status like 'wsrep%';

如果wsrep_connected=ON且wsrep_ready=ON则说明节点成功接入集群。

14. 配置集群的仲裁节点

对于只有2个节点的galera cluster和其他集群软件一样,需要面对极端情况下的“脑裂”状态。为了避免这种问题,galera引入了“arbitrator(仲裁人)”。
“仲裁人”节点上没有数据,它在集群中的作用就是在集群发生分裂时进行仲裁,集群中可以有多个“仲裁人”节点。将“仲裁人”节点加入集群的方法很简单,运行如下命令即可:
[root@localhost ~]# garbd -a gcomm://<节点IP> -g my_wsrep_cluster -d

  • 参数说明

  -a 集群地址
  -g 集群名称
  -d 以daemon模式运行

15. 检查数据库是否符合要求

部署到集群之前,建议先检查数据库是否符合galera的要求,比如存储引擎必须是innodb、数据表必须有主键等,否则记录将不会在多台复制。

选择指定的数据库,执行以下SQL输出不符合要求的表及其原因,根据相应的原因修改即可:

select distinct concat( t.table_schema, '.', t.table_name ) as tbl, t. engine, 
    if ( isnull(c.constraint_name), 'nopk', '' ) as nopk, 
    if ( s.index_type = 'fulltext', 'fulltext', '' ) as ftidx, 
    if ( s.index_type = 'spatial', 'spatial', '' ) as gisidx 
from information_schema. tables as t 
left join information_schema.key_column_usage as c 
on ( t.table_schema = c.constraint_schema and t.table_name = c.table_name and c.constraint_name = 'primary' ) 
left join information_schema.statistics as s 
on ( t.table_schema = s.table_schema and t.table_name = s.table_name and s.index_type in ('fulltext', 'spatial')) 
where t.table_schema not in ( 'information_schema', 'performance_schema', 'mysql' ) and t.table_type = 'base table' 
and ( t. engine <> 'innodb' or c.constraint_name is null or s.index_type in ('fulltext', 'spatial')) 
order by t.table_schema, t.table_name;

16. 常见问题

1)启动mysql时出错:SST in progress, setting sleep higher. ERROR!

  • 确保本机已安装rsync:[root@localhost ~]# yum list|grep rsync
  • 确保已允许galera sst使用的端口4444、4567、4568通过防火墙并重启防火墙功能
  • 确保selinux已对端口4444开放权限:[root@localhost ~]# semanage port -a -t mysqld_port_t -p tcp 4444

2)查看galera集群状态时wsrep_connected和wsrep_ready的值均为OFF!

 打开/etc/my.cnf.d/wsrep.cnf文件,找到wsrep_cluster_address="gcomm://"这一行,检查前面是否有"#",如果有则删掉并重启mysql。

MariaDB Galera Cluster 部署(如何快速部署MariaDB集群)  http://www.linuxidc.com/Linux/2015-07/119512.htm

MariaDB Galera Cluster 的详细介绍请点这里
MariaDB Galera Cluster 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-01/127256.htm

linux
相关资讯       MYSQL集群  MariaDB Galera集群 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款