MySQL的Galera Cluster配置说明
Galera Cluster介绍 Galera是一个MySQL(也支持MariaDB,Percona)的同步多主集群软件,目前只支持InnoDB引擎。 主要功能: 同步复制 真正的multi-master,即所有节点可以同时读写数据库 自动的节点成员控制,失效节点自动被清除 新节点加入数据自动复制 真正的
Galera Cluster介绍
Galera是一个MySQL(也支持MariaDB,Percona)的同步多主集群软件,目前只支持InnoDB引擎。
主要功能:
- 同步复制
- 真正的multi-master,即所有节点可以同时读写数据库
- 自动的节点成员控制,失效节点自动被清除
- 新节点加入数据自动复制
- 真正的并行复制,行级
- 用户可以直接连接集群,使用感受上与MySQL完全一致
优势:
因为是多主,所以不存在Slave lag(延迟)
不存在丢失交易的情况
同时具有读和写的扩展能力
更小的客户端延迟
节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的
技术:
Galera集群的复制功能基于Galera library实现,为了让MySQL与Galera library通讯,特别针对MySQL开发了wsrep API。
MariaDB介绍
MariaDB是MySQL的一个分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可。
开发这个分支的原因之一是Oracle公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。
MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
在存储引擎方面,使用XtraDB来代替MySQL的InnoDB。
方案总览
- haproxy作为MariaDB Galera Cluster的前端
- 2台haproxy用keepalived避免单点故障
- 3台MariaDB和一个garbd仲裁节点组成集群,仲裁节点上无数据
- Galera的SST采用Percona提供的XtraBackup(防止锁表,非阻塞)
环境描述
OS: CentOS 6.0 64bit
MariaDB server1: |
192.168.0.171 |
MariaDB server2: |
192.168.0.172 |
MariaDB server3: |
192.168.0.173 |
HAProxy server1: |
192.168.0.151 |
HAProxy server2: |
192.168.0.152 |
HAProxy共享的VIP: |
192.168.0.170 |
MySQL root password: |
q1w2e3!@# |
Cluster root username: |
clusteroot |
Cluster root password: |
q1w2e3!@# |
Galera SST user: |
sst |
Galera SST password: |
sstpass123 |
环境准备
以下操作以MariaDB server1为示例。
1.准备hosts文件
编辑上述每台服务器的/etc/hosts,添加如下内容:
192.168.0.151 haproxy1.cluster.local haproxy1 192.168.0.152 haproxy2.cluster.local haproxy2 192.168.0.171 galera1.cluster.local galera1 192.168.0.172 galera2.cluster.local galera2 127.0.0.1 ::1 |
2.安装163,epel,Percona,MariaDB的YUM源
[root@mdb-01 ~]# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
[root@mdb-01 ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@mdb-01 ~]# cat CentOS6-Base-163.repo > /etc/yum.repos.d/CentOS-Base.repo
[root@mdb-01 ~]# vi /etc/yum.repos.d/MariaDB.repo
# MariaDB 5.5 CentOS repository list - created 2013-05-16 01:58 UTC # http://mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/5.5/centos6-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
[root@mdb-01 ~]# rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
[root@mdb-01 ~]# vi /etc/yum.repos.d/Percona.repo
[percona] name = CentOS $releasever - Percona baseurl=http://repo.percona.com/centos/$releasever/os/$basearch/ enabled = 1 gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-percona gpgcheck = 1 |
[root@mdb-01 ~]# wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-percona http://www.percona.com/downloads/RPM-GPG-KEY-percona
[root@mdb-01 ~]# yum makecache
Galera Cluster的配置
1.安装MariaDB、galera、xtrabackup
[root@mdb-01 ~]#
[root@mdb-01 ~]#
2.设置开机启动MariaDB
[root@mdb-01 ~]#
[root@mdb-01 ~]#
3.设置MariaDB的root密码,并做安全加固
[root@mdb-01 ~]#
4.确认MariaDB已正确安装并处于运行状态
[root@mdb-01 ~]#
Enter password:
Welcome to the MariaDB monitor.
Your MariaDB connection id is 11
Server version: 5.5.29-MariaDB MariaDB Server, wsrep_23.7.3.rXXXX
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
注意查看是否有"wsrep_23.7.3.rXXXX"的输出。
5.创建用于同步数据库的SST帐号
[root@mdb-01 ~]#
mysql> GRANT USAGE ON *.* to sst@'%' IDENTIFIED BY 'sstpass123';
mysql> GRANT ALL PRIVILEGES on *.* to sst@'%';
mysql> FLUSH PRIVILEGES;
mysql> quit
6.创建wsrep.cnf文件
[root@mdb-01 ~]#
[root@mdb-01 ~]#
只需要修改如下4行:
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"
wsrep_sst_auth=sst:sstpass123
wsrep_sst_method=xtrabackup
注意:
"gcomm://" 是特殊的地址,仅仅是Galera cluster初始化启动时候使用。
如果集群启动以后,我们关闭了第一个节点,那么再次启动的时候必须先修改
"gcomm://"为其他节点的集群地址,例如
wsrep_cluster_address="gcomm://192.168.0.172:4567"
7.在/etc/my.cnf中加入如下一行
!includedir /etc/my.cnf.d/
8.确认本机防火墙上开放了所需TCP 3306和TCP 4567的端口
[root@mdb-01 ~]#
[root@mdb-01 ~]#
9.重起MariaDB服务
[root@mdb-01 ~]#
10.确认MySQL的3306端口和wsrep的4567端口处于监听状态
[root@mdb-01 ~]#
tcp
tcp
至此,集群的第一个节点配置完毕。
11.为集群增加其他节点
在Galera Cluster中,新接入的节点叫Joiner,给joiner提供复制的节点叫Donor。
对于生产环境使用,建议设立一个专用的"参考节点",这个"参考节点"不执行任何客户端的SQL请求。
上图中红色的NODE A即为"参考节点",这样做的好处有如下几条:
1.数据一致性:
因为"参考节点"本身不执行任何客户端SQL,所以在这个节点上发生transaction冲突的可能性最小。
因此如果发现集群有数据不一致的时候,"参考节点"上的数据应该是集群中最准确的。
2.数据安全性:
因为"参考节点"本身不执行任何客户端SQL,所以在这个节点上发生灾难事件的可能性最小。
因此当整个集群宕掉的时候,"参考节点"应该是恢复集群的最佳节点。
3.高可用
"参考节点"可以作为专门state snapshot donor。
因为"参考节点"不服务于客户端,因此当使用此节点进行SST的时候,不会影响用户体验,并且前端的负载均衡设备也不需要重新配置。
构造如上图的结构,新节点的操作步骤如下:
1.按照上述1-9的步骤安装MariaDB和Galera library
2.除了第5步wsrep_cluster_address的配置稍有不同:
wsrep_cluster_address="gcomm://Node-A-IP:4567"
3.重起MariaDB
4.按上述步骤配置下一个节点,将其wsrep_cluster_address指向其前一个节点,例如:
Node C -> Node B, Node D -> Node C, … , Node N -> …
5.配置Node A重新加入集群,修改/etc/my.cnf.d/wsrep.cnf中的wsrep_cluster_address地址指向Node N。
wsrep_cluster_address="gcomm://Node-N-IP:4567"
修改节点的wsrep_cluster_address有两种方式:
1)修改配置文件中的wsrep_cluster_address,然后重启MySQL
2)直接修改MySQL全局变量
mysql> SHOW VARIABLES LIKE 'wsrep_cluster_address'; +-----------------------+----------------------------+ | Variable_name +-----------------------+----------------------------+ | wsrep_cluster_address | gcomm://192.168.0.172:4567 | +-----------------------+----------------------------+ 1 row in set (0.00 sec) mysql> set global wsrep_cluster_address="gcomm://192.168.0.172:4567,192.168.0.173:4567"; Query OK, 0 rows affected (2.20 sec) mysql> SHOW VARIABLES LIKE 'wsrep_cluster_address'; +-----------------------+-------------------------------------------------------+ | Variable_name +-----------------------+-------------------------------------------------------+ | wsrep_cluster_address | gcomm://192.168.0.172:4567,192.168.0.173:4567 +-----------------------+-------------------------------------------------------+ 1 row in set (0.00 sec) |
12.确认galera集群正确安装和运行
[root@mdb-01 ~]#
wsrep_ready 为ON,则说明MariaDB Galera集群已经正确运行了。
wsrep_cluster_size 标明了组成集群的节点个数。
13.给集群加入Galera arbitrator
对于只有2个节点的Galera Cluster和其他集群软件一样,需要面对极端情况下的"脑裂"状态。
为了避免这种问题,Galera引入了"arbitrator(仲裁人)"。
"仲裁人"节点上没有数据,它在集群中的作用就是在集群发生分裂时进行仲裁,集群中可以有多个"仲裁人"节点。
"仲裁人"节点加入集群的方法很简单,运行如下命令即可:
[root@arbt ~]# garbd -a gcomm://192.168.0.171:4567 -g my_wsrep_cluster -d
参数说明:
-d 以daemon模式运行
-a 集群地址
-g 集群名称
Haproxy的相关配置
1.创建用于后端MySQL服务器健康检查的数据库帐号
haproxy现在支持option mysql-check,这个检查会向后端服务器发送2个包,一个用于客户端认证,一个用于关闭连接。这个检查需要在每个节点上都需要创建一个无密码的MySQL帐号。
[root@mdb-01 ~]#
Enter password:
mysql> INSERT INTO mysql.user (host,user) values ('%','haproxy');
mysql> FLUSH PRIVILEGES;
mysql> quit
2.编译haproxy和keepalived(过程略,可参考我这篇博文:http://blog.sina.com.cn/s/blog_704836f40101gm11.html)
3.配置keepalived(此处只列出Master的配置)
vrrp_script chk_haproxy { |
4.配置haproxy
defaults |
其他相关
1.Galera Cluster部署的前置检查
在要转成Galera Cluster的数据库上执行如下SQL语句:
SELECT DISTINCT |
上述SQL检索数据库,输出不符合使用Galera的表的信息,对应的5个字段顺序为:表,表引擎,是否无主键,是否有全文索引,是否有空间索引。
找到不符合的原因,对应修改即可。
2.MySQL Galera监控
监控状态参数说明:
参考文档:
MariaDB和Galera Cluster相关文档
http://www.alexwilliams.ca/blog/2009/08/10/using-haproxy-for-mysql-failover-and-redundancy/
https://github.com/olafz/percona-clustercheck
https://mariadb.com/blog/setup-mariadb-enterprise-cluster-part-3-setup-ha-proxy-load-balancer-read-and-write-pools
http://www.sebastien-han.fr/blog/2012/04/08/mysql-galera-cluster-with-haproxy/
http://www.alexwilliams.ca/blog/2009/08/10/using-haproxy-for-mysql-failover-and-redundancy/
http://blog.secaserver.com/2011/07/install-mysql-replication-and-cluster-using-galera/
http://weetinyworld.blogspot.tw/2013/04/setting-up-mariadb-galera-cluster-from.html
http://blog.wu-boy.com/2013/03/galera-cluster-for-mysql-multi-master-replication/
http://www.sebastien-han.fr/blog/2012/08/29/setup-galera-with-sst-xtrabackup-method/
http://blog.secaserver.com/2011/07/install-mysql-replication-and-cluster-using-galera/
http://blog.secaserver.com/2012/02/high-availability-mysql-cluster-galera-haproxy/
http://www.severalnines.com/resources/clustercontrol-mysql-haproxy-load-balancing-tutorial
http://opentodo.net/2012/12/mysql-multi-master-replication-with-galera/
http://www.planetlarg.net/open-source-cookbook/data-storage/replace-mysql-mysql-galera
http://www.sebastien-han.fr/blog/2012/04/08/mysql-galera-cluster-with-haproxy/
http://www.zrwm.com/?cat=130
http://openquery.com/blog/galera-predeployment-check
http://www.percona.com/files/presentations/percona-live/nyc-2012/PLNY12-galera-cluster-best-practices.pdf
https://groups.google.com/forum/#!msg/codership-team/OUxATjcznPI/H8ftsjZwptAJ
http://www.zrwm.com/?p=5844
http://beerpla.net/2008/09/05/mysql-slave-lag-delay-explained-and-7-ways-to-battle-it/
http://wiki.vps.net/vps-net-features/cloud-servers/template-information/galeramysql-recommended-cluster-configuration/
http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
GTID和mysqlfailover相关文档
http://scriptingmysql.wordpress.com/2012/12/06/using-the-mysql-script-mysqlfailover-for-automatic-failover-with-mysql-5-6-gtid-replication/
http://svenmysql.blogspot.se/2012/10/failover-and-flexible-replication.html
http://svenmysql.blogspot.tw/2012/10/advanced-use-of-global-transaction.html
http://svenmysql.blogspot.se/2013/03/flexible-fail-over-policies-using-mysql.html
http://www.percona.com/sites/default/files/presentations/MHA_Toronto_Presentation.pdf
http://hickey.in/?p=78
http://dev.mysql.com/doc/refman/5.6/en/replication-gtids-howto.html
http://huoding.com/2011/04/05/59
http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html
http://dev.mysql.com/doc/refman/5.5/en/replication-howto-masterstatus.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings
