mysql galera cluster配置

1. Galera Cluster介绍

Galera是一个MySQL(也支持MariaDB,Percona)的同步多主集群软件,目前只支持InnoDB引擎。

mysql galera cluster配置_第1张图片
image.png

1.1 主要功能:

    1. 同步复制
    1. 真正的multi-master,即所有节点可以同时读写数据库
    1. 自动的节点成员控制,失效节点自动被清除
    1. 新节点加入数据自动复制
    1. 真正的并行复制,行级
    1. 用户可以直接连接集群,使用感受上与MySQL完全一致

1.2 优势:

    1. 因为是多主,所以不存在Slave lag(延迟)
  • 2 .不存在丢失交易的情况
    1. 同时具有读和写的扩展能力
    1. 更小的客户端延迟
  • 5 .节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的

1.3 技术:

Galera集群的复制功能基于Galera library实现,为了让MySQL与Galera library通讯,特别针对MySQL开发了wsrep API。


mysql galera cluster配置_第2张图片
image.png

mysql galera cluster配置_第3张图片
image.png

mysql galera cluster配置_第4张图片
image.png

1.4 同步原理

在Galera Cluster中,新接入的节点叫Joiner,给joiner提供复制的节点叫Donor。


mysql galera cluster配置_第5张图片
image.png

对于生产环境使用,建议设立一个专用的"参考节点",这个"参考节点"不执行任何客户端的SQL请求。


mysql galera cluster配置_第6张图片
image.png

上图中红色的NODE A即为"参考节点",这样做的好处有如下几条:
  • 1.数据一致性:
    因为"参考节点"本身不执行任何客户端SQL,所以在这个节点上发生transaction冲突的可能性最小。因此如果发现集群有数据不一致的时候,"参考节点"上的数据应该是集群中最准确的。
  • 2.数据安全性:
    因为"参考节点"本身不执行任何客户端SQL,所以在这个节点上发生灾难事件的可能性最小。因此当整个集群宕掉的时候,"参考节点"应该是恢复集群的最佳节点。
  • 3.高可用
    "参考节点"可以作为专门state snapshot donor。因为"参考节点"不服务于客户端,因此当使用此节点进行SST的时候,不会影响用户体验,并且前端的负载均衡设备也不需要重新配置。

2 .服务器环境

机器 ip 操作系统
mysql-cluster1 192.168.8.85 centos7
mysql-cluster1 192.168.8.87 centos7
mysql-cluster3 192.168.8.79 centos7

3 . 安装前准备

关闭防火墙,修改selinux

systemctl disable firewalld
vim /etc/selinux/config

修改主机名

 hostnamectl set-hostname mysql-cluster1

安装gcc、gcc-c++、boost-devel、check-devel、openssl-devel、libaio、perl、perl-devel、rsync、lsof、net-tools、autoconf

# yum install gcc gcc-c++ -y
# yum boost-devel  -y
# yum install check-devel openssl-devel -y
# yum install libaio -y
# yum install perl perl-devel rsync lsof net-tools -y
# yum -y install autoconf

下载scons安装(scons-2.5.0.tar.gz)

# tar -zxvf scons-2.5.0.tar.gz 
# cd scons-2.5.0
# python setup.py build
# python setup.py install

创建用户、创建目录

# groupadd mysql
# useradd -g mysql mysql
# mkdir -p /usr/local/data
# mkdir -p /usr/localmysql
# chown -R mysql:mysql /usr/local/data
# chown -R mysql:mysql /usr/local/mysql
# chmod -R 755 /usr/local/mysql
# chmod -R 755 /usr/local/data

4 .MySql Galera安装

# tar -zxvf mysql-wsrep-5.6.38-25.21-linux-x86_64.tar.gz
# cd mysql-wsrep-5.6.38-25.21-linux-x86_64
# mv * /usr/local/mysql/
# chown -R mysql:mysql /usr/local/data
# chown -R mysql:mysql /usr/local/mysql 
# ./scripts/mysql_install_db --no-defaults --datadir=/usr/local/data/ --basedir=/usr/local/mysql/
 --user=mysql
# ln -s /usr/local/mysql/bin/* /usr/sbin/

5 .安装Galera复制插件

# tar -zxvf galera-3-25.3.22.tar.gz
# cd galera-3-25.3.22
# scons
# cp garb/garbd /usr/local/mysql/bin/
# cp libgalera_smm.so /usr/local/mysql/lib/plugin/

6 .配置MySQL Galera

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# mkdir -p /var/lib/mysql
# chown mysql:mysql /var/lib/mysql/
# vim /etc/my.cnf

[client]
port=3306
socket=/var/lib/mysql/mysql.sock

[mysqld]
wsrep_node_name=mysql-cluster1
wsrep_provider=/usr/local/mysql/lib/plugin/libgalera_smm.so
wsrep_sst_method=rsync
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/data
socket=/var/lib/mysql/mysql.sock
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
innodb_locks_unsafe_for_binlog=1
innodb_flush_log_at_trx_commit=0
innodb_doublewrite=0
innodb_file_per_table=1

binlog_format=ROW
log-bin=mysql-bin
server-id=101
relay-log=mysql-relay-bin
log-slave-updates=1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]

log-error=/var/lib/mysql/mysql.log
pid-file=/var/lib/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

7 .MySQL Galera启动与关闭

# chmod -R 755 /usr/local/mysql/
# chown -R mysql:mysql /usr/local/mysql/
# cd /var/lib/mysql/
# touch mysql.log
# chmod 755 mysql.log
# chown mysql:mysql mysql.log
# /usr/local/mysql/bin/mysqld_safe --wsrep_cluster_address=gcomm:// >/dev/null&
或
# service mysqld start --wsrep_cluster_address=gcomm://

查看mysql启动端口

# netstat -plantu|grep mysqld
mysql galera cluster配置_第7张图片
image.png

注:
1)"gcomm://"是特殊的地址,仅仅是galera cluster初始化启动时候使用,再次启动的时候需要使用具体的ip地址。
2)端口4567使用的默认端口。该端口的防火墙设置规则应该和3306的一样。

关闭

# mysqladmin -uroot -p shutdown
或者
# service mysqld stop

8 .添加MySQL Galera新节点

添加新节点的时候,新接入的节点叫Joiner,给Joiner提供复制的节点叫Donor。新的节点接入需要:
1)安装带wsrep patch的Mysql版本
2)安装Galera复制插件
3)配置好新节点的Mysql(参考Doner的my.cnf)
4)配置或启动的gcomm://的地址是需要使用donor的IP。

接入节点mysql-cluster2

/usr/local/mysql/bin/mysqld_safe --wsrep_cluster_address="gcomm://192.168.8.85:4567,192.168.8.87:4567" >/dev/null &

接入节点mysql-cluster3

/usr/local/mysql/bin/mysqld_safe --wsrep_cluster_address="gcomm://192.168.8.87:4567,192.168.8.79:4567" >/dev/null &

If you initialized the first PXC node, so it's standalone primary node, then adding new nodes is very simple. On a new node, you just specify the existing node's IP in this variable:
Code:

wsrep_cluster_address=gcomm://node1_IP>
Then full SST will copy data from existing node to a new node, eventually making both nodes identical.
Once all machines are joined into a cluster, then add all the IPs in the my.cnf on each node:
Code:
wsrep_cluster_address=gcomm://IP1,IP2,IPn
https://www.percona.com/forums/questions-discussions/percona-xtradb-cluster/12030-add-new-node-in-cluster

wsrep_cluster_address
Description: The addresses of cluster nodes to connect to when starting up, for example gcomm://192.168.0.1:1234?gmcast.listen_addr=0.0.0.0:2345. Good practice is to specify all possible cluster nodes, in the form gcomm://,,. Specifying an empty ip (gcomm://) will cause the node to start a new cluster (which should not be done in the my.cnf file, as after each restart the server will not rejoin the current cluster). The variable can be changed at runtime in some configurations, and will result in the node closing the connection to any current cluster, and connecting to the new address. If specifying a port, note that this is the Galera port, not the MariaDB port
commandline: --wsrep-cluster-address=value
Scope: Global
Dynamic: No
Data Type: String
https://mariadb.com/kb/en/library/galera-cluster-system-variables/#wsrep_cluster_address

9 .验证同步

在其中一个节点上创建账号

mysql> create user 'test'@'192.168.8.%' identified by '123456';
mysql> grant all privileges on *.* to 'test'@'192.168.8.%' with grant option;
mysql> flush privileges;

在其他节点查看账号同步情况

mysql> select user,host from mysql.user;

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