共计 6868 个字符,预计需要花费 18 分钟才能阅读完成。
如何搭建 Percona XtraDB Cluster 集群
一、环境准备
主机 IP 主机名 操作系统版本 PXC
192.168.244.146 node1 CentOS7.1 Percona-XtraDB-Cluster-56-5.6.30
192.168.244.147 node2 CentOS7.1 Percona-XtraDB-Cluster-56-5.6.30
192.168.244.148 node3 CentOS7.1 Percona-XtraDB-Cluster-56-5.6.30
关闭防火墙或者允许 3306, 4444, 4567 和 4568 四个端口的连接
关闭 SElinux
二、下载 PXC
安装 PXC yum 源
# yum install http://www.percona.com/downloads/percona-release/RedHat/0.1-3/percona-release-0.1-3.noarch.rpm
这样会在 /etc/yum.repos.d 下生成 percona-release.repo 文件
安装 PXC
# yum install Percona-XtraDB-Cluster-56
最终下载下来的版本是 Percona-XtraDB-Cluster-56-5.6.30
注意:三个节点上均要安装。
三、配置节点
配置节点一
修改 node1 的 /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains the IPs of node#1, node#2 and node#3
wsrep_cluster_address=gcomm://192.168.244.146,192.168.244.147,192.168.244.148
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #1 address
wsrep_node_address=192.168.244.146
# SST method
wsrep_sst_method=xtrabackup-v2
# Cluster name
wsrep_cluster_name=my_centos_cluster
# Authentication for SST method
wsrep_sst_auth="sstuser:s3cret"
启动 node1
# systemctl start mysql@bootstrap.service
注意:这个是 CentOS 7 下的启动方式,如果是 CentOS 6,则启动方式为 # /etc/init.d/mysql bootstrap-pxc
之所以采用 bootstrap 启动,其实是告诉数据库,这是第一个节点,不用进行数据的同步。
利用这种方式启动,相当于 wsrep_cluster_address 方式设置为 gcomm://。
此时,可登录客户端查看数据库的状态
mysql> show status like ‘wsrep%’;
主要关注以下参数的状态
+------------------------------+--------------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid | 1fbb69e3-32a3-11e6-a571-aeaa962bae0c |
...
| wsrep_local_state | 4
| wsrep_local_state_comment | Synced |
...
| wsrep_cluster_size | 1
...
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
...
| wsrep_ready | ON |
在上面的配置文件中,有个 wsrep_sst_auth 参数。该参数是用于其它节点加入到该集群中,利用 XtraBackup 执行 State Snapshot Transfer(类似于全量同步)的。
所以,接下来是授权
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cret';
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;
配置节点二
修改 node2 的 /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains the IPs of node#1, node#2 and node#3
wsrep_cluster_address=gcomm://192.168.244.146,192.168.244.147,192.168.244.148
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #2 address
wsrep_node_address=192.168.244.147
# SST method
wsrep_sst_method=xtrabackup-v2
# Cluster name
wsrep_cluster_name=my_centos_cluster
# Authentication for SST method
wsrep_sst_auth="sstuser:s3cret"
启动 node2
# systemctl start mysql
如果是 CentOS 6,则启动方式为 # /etc/init.d/mysql start
如果在启动的过程中出现问题,可查看 mysql 的错误日志,如果是 RPM 安装,默认是 /var/lib/mysql/ 主机名.err
启动完毕后,也可通过 mysql> show status like ‘wsrep%’; 命令查看集群的信息。
配置节点三
修改 node3 的 /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains the IPs of node#1, node#2 and node#3
wsrep_cluster_address=gcomm://192.168.244.146,192.168.244.147,192.168.244.148
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #3 address
wsrep_node_address=192.168.244.148
# SST method
wsrep_sst_method=xtrabackup-v2
# Cluster name
wsrep_cluster_name=my_centos_cluster
# Authentication for SST method
wsrep_sst_auth="sstuser:s3cret"
启动 node3
# systemctl start mysql
登录数据库,查看集群的状态
+------------------------------+--------------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid | 1fbb69e3-32a3-11e6-a571-aeaa962bae0c |
...
| wsrep_local_state | 4
| wsrep_local_state_comment | Synced |
...
| wsrep_cluster_size | 3
...
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
...
| wsrep_ready | ON
通过 wsrep_cluster_size 可以看出集群有 3 个节点。
四、测试
下面来测试一把,在 node3 中创建一张表,并插入记录,看 node1 和 node2 中能否查询得到。
node3 中创建测试表并插入记录
root@node3 > create table test.test(id int,description varchar(10));
Query OK, 0 rows affected (0.18 sec)
root@node3 > insert into test.test values(1,'hello,pxc');
Query OK, 1 row affected (0.01 sec)
node1 和 node2 中查询
root@node1 > select * from test.test;
+------+-------------+
| id | description |
+------+-------------+
| 1 | hello,pxc |
+------+-------------+
1 row in set (0.00 sec)
root@node2 > select * from test.test;
+------+-------------+
| id | description |
+------+-------------+
| 1 | hello,pxc |
+------+-------------+
1 row in set (0.05 sec)
至此,Percona XtraDB Cluster 搭建完毕~
总结:
1. 刚开始启动 node2 的时候,启动失败,错误日志中报如下信息:
2016-06-15 20:06:09 4937 [ERROR] WSREP: failed to open gcomm backend connection: 110: failed to reach primary view: 110 (Connection timed out)
at gcomm/src/pc.cpp:connect():162
2016-06-15 20:06:09 4937 [ERROR] WSREP: gcs/src/gcs_core.cpp:gcs_core_open():208: Failed to open backend connection: -110 (Connection timed out)
2016-06-15 20:06:09 4937 [ERROR] WSREP: gcs/src/gcs.cpp:gcs_open():1387: Failed to open channel 'my_centos_cluster' at 'gcomm://192.168.244.146,192.168.244.147,192.168.244.148': -110 (Connection timed out)
2016-06-15 20:06:09 4937 [ERROR] WSREP: gcs connect failed: Connection timed out
2016-06-15 20:06:09 4937 [ERROR] WSREP: wsrep::connect(gcomm://192.168.244.146,192.168.244.147,192.168.244.148) failed: 7
2016-06-15 20:06:09 4937 [ERROR] Aborting
2016-06-15 20:27:03 5870 [ERROR] WSREP: Failed to read 'ready <addr>' from: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.244.147' --datadir '/var/lib/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix ''--parent'5870'''
Read: '(null)'
2016-06-15 20:27:03 5870 [ERROR] WSREP: Process completed with error: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '192.168.244.147' --datadir '/var/lib/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix ''--parent'5870''' : 2 (No such file or directory)
2016-06-15 20:27:03 5870 [ERROR] WSREP: Failed to prepare for 'xtrabackup-v2' SST. Unrecoverable.
2016-06-15 20:27:03 5870 [ERROR] Aborting
特别是下面的报错信息,根据 https://mariadb.com/kb/en/mariadb/problem-with-the-galera-wsrep_sst_method-xtrabackup-v2/ 的解决思路,还以为是 socat 的版本太低。
后来才发现,是 SElinux 没有关闭。。。
另外,在节点加入集群的过程中,如果报有关 xtrabackup-v2 的错误,不妨先将 wsrep_sst_method 的方式设置为 rsync 或者 mysqldump,看能否成功。
2. 以 systemctl start mysql@bootstrap.service 启动的节点,必须以 systemctl stop mysql@bootstrap.service 关闭,如果以 systemctl stop mysql 关闭,则没效果。
参考文档:
1. http://www.linuxidc.com/Linux/2016-06/132701.htm
2. PXC 官方文档
3. http://galeracluster.com/documentation-webpages/
Percona XtraDB Cluster 的详细介绍 :请点这里
Percona XtraDB Cluster 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-06/132700.htm