Percona XtraDB Cluster 9节点快速部署

1.概述


PXC(Percona XtraDB Cluste)相比group replication出现要早,方案相对成熟,已经有很多企业已经用在了生产环境,pxc也和gr一样支持多主写入,但是人们一般将它用在复制场景。Pxc是基于galera的一种方案,因此最少需要三个节点(一般会部署为两个数据节点,一个仲裁节点)。这里我们也部署一个九节点的pxc。
QQ图片20161221141005

2.环境准备


2.1用户创建

groupadd mysql
useradd –g mysql mysql

2.2xtrabakcup安装
下载xtrabackup,解压到/opt目录下,并且配置用户环境变量。

[root@10-25-7-48 percona-xtrabackup-2.4.5]# pwd
/opt/percona-xtrabackup-2.4.5
[root@10-25-7-48 percona-xtrabackup-2.4.5]# ls -l
total 12
drwxr-xr-x  2 mysql mysql 4096 Dec 19 17:44 bin
drwxr-xr-x  3 mysql mysql 4096 Dec 19 17:44 man
drwxr-xr-x 14 mysql mysql 4096 Dec 19 17:44 percona-xtrabackup-2.4-tes
[root@10-25-7-48 percona-xtraDB-xluster-5.7.14]# pwd
/opt/percona-xtraDB-xluster-5.7.14
[root@10-25-7-48 percona-xtraDB-xluster-5.7.14]# ls -l
total 92
drwxr-xr-x  2 mysql mysql  4096 Dec 19 17:44 bin
-rw-r--r--  1 mysql mysql 17987 Dec 19 17:44 COPYING
-rw-r--r--  1 mysql mysql  1703 Dec 19 17:44 COPYING-jemalloc
drwxr-xr-x  2 mysql mysql  4096 Dec 19 17:44 docs
-rw-rw-r--  1 mysql mysql   264 Dec 19 23:39 gvwstate.dat
drwxr-xr-x  3 mysql mysql  4096 Dec 19 17:44 include
drwxr-xr-x  4 mysql mysql  4096 Dec 19 17:44 lib
drwxr-xr-x  4 mysql mysql  4096 Dec 19 17:44 man
drwx------ 10 mysql mysql  4096 Dec 19 17:44 mysql-test
drwx------  5 mysql mysql  4096 Dec 19 17:44 percona-xtradb-cluster-tests
-rw-r--r--  1 mysql mysql  2478 Dec 19 17:44 README.MySQL
-rw-r--r--  1 mysql mysql 19627 Dec 19 17:44 README-wsrep
drwxr-xr-x 28 mysql mysql  4096 Dec 19 17:44 share
drwxr-xr-x  2 mysql mysql  4096 Dec 19 17:44 support-files
drwxr-xr-x  2 mysql mysql  4096 Dec 19 17:44 xinetd.d

2.3 Mysql用户环境变量配置

PATH=$PATH:$HOME/bin:/opt/percona-xtraDB-xluster-5.7.14/bin:/opt/percona-xtrabackup-2.4.5/bin

export PATH
export MYSQL_HOME=/opt/mysql-5.7.17
export MYSQL_PS1="\\u@\\h [\\d] \\r:\\m:\\s>>>"
export MYSQL_UNIX_PORT=/data/ha_mysql/s1/s1.sock
#export MYSQL_PWD 
#export MYSQL_TCP_PORT=3306

2.4验证配置

[mysql@10-25-7-48 ~]$ mysql -V
mysql  Ver 14.14 Distrib 5.7.14-8, for Linux (x86_64) using  6.0
[mysql@10-25-7-48 ~]$ xtrabackup -v
xtrabackup version 2.4.5 based on MySQL server 5.7.13 Linux (x86_64) (revision id: e41c0be)

注意:xtrabackup用于添加节点用,pxc自动调用xtrabackup构建新节点。

2.5依赖包安装

yum install socat -y

3.目录准备


mkdir -p /data/for_pxc/{p1,p2,p3,p4,p5,p6,p7,p8,p9}
chown –R mysql:mysql /data/for_pxc

4.配置文件准备


/data/conf/for_pxc/p1.cnf

[mysql@10-25-126-227 for_pxc]$ pwd
/data/conf/for_pxc
[mysql@10-25-126-227 for_pxc]$ ls -l
total 4
-rw-rw-r-- 1 mysql mysql 994 Dec 20 10:32 p1.cnf
[mysql@10-25-126-227 for_pxc]$ cat p1.cnf 
[mysqld]
port = 6010
socket=/data/for_pxc/p1/p1.sock
datadir=/data/for_pxc/p1
basedir=/opt/percona-xtraDB-xluster-5.7.14
innodb_buffer_pool_instances=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=2
user=mysql
server-id=1
log_error=error.log
binlog_format=ROW
wsrep_cluster_address='gcomm://10.25.126.227:6030,10.25.126.227:6031,10.25.126.227:6032,10.25.126.227:6033,10.25.126.227:6034,10.25.126.227:6035,10.25.126.227:6036,10.25.126.227:6037,10.25.126.227:6038,10.25.126.227:6039'
wsrep_provider=/opt/percona-xtraDB-xluster-5.7.14/lib/libgalera_smm.so
wsrep_sst_receive_address=10.25.126.227:6020
wsrep_node_incoming_address=10.25.126.227:6010
wsrep_slave_threads=2
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc1
wsrep_provider_options = "gcache.size=512M;gmcast.listen_addr=tcp://10.25.126.227:6030;"
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:passw0rd
pxc_strict_mode=ENFORCING
innodb_autoinc_lock_mode=2
default_storage_engine=InnoDB

5.初始化p1节点


/opt/percona-xtraDB-xluster-5.7.14/bin/mysqld --initialize-insecure --basedir=/opt/percona-xtraDB-xluster-5.7.14 --datadir=/data/for_pxc/p1

6.启动第一个节点


[mysql@10-25-126-227 percona-xtraDB-xluster-5.7.14]$ cd /opt/percona-xtraDB-xluster-5.7.14
[mysql@10-25-126-227 percona-xtraDB-xluster-5.7.14]$ ./bin/mysqld_safe --defaults-file=/data/conf/for_pxc/p1.cnf --wsrep-new-cluster &

7.检查第一个节点


QQ图片20161221140414

8.添加用户


CREATE USER 'sstuser'@'%' IDENTIFIED BY 'passw0rd';
GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON * . * TO 'sstuser'@'%';
FLUSH PRIVILEGES;

9.第二个节点配置文件准备


/data/conf/for_pxc/p2.cnf

[mysqld]
port = 6021
socket=/data/for_pxc/p2/p2.sock
datadir=/data/for_pxc/p2
basedir=/opt/percona-xtraDB-xluster-5.7.14
innodb_buffer_pool_instances=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=2
user=mysql
server-id=2
log_error=error.log
binlog_format=ROW
wsrep_cluster_address='gcomm://10.25.126.227:6030,10.25.126.227:6031,10.25.126.227:6032,10.25.126.227:6033,10.25.126.227:6034,10.25.126.227:6035,10.25.126.227:6036,10.25.126.227:6037,10.25.126.227:6038,10.25.126.227:6039'
wsrep_provider=/opt/percona-xtraDB-xluster-5.7.14/lib/libgalera_smm.so
wsrep_sst_receive_address=10.25.126.227:6021
wsrep_node_incoming_address=10.25.126.227:6011
wsrep_slave_threads=2
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc2
wsrep_provider_options = "gcache.size=512M;gmcast.listen_addr=tcp://10.25.126.227:6031;"
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:passw0rd
pxc_strict_mode=ENFORCING
innodb_autoinc_lock_mode=2
default_storage_engine=InnoDB

10.启动第二个节点


[mysql@10-25-126-227 percona-xtraDB-xluster-5.7.14]$ pwd
/opt/percona-xtraDB-xluster-5.7.14
[mysql@10-25-126-227 percona-xtraDB-xluster-5.7.14]$ ./bin/mysqld_safe --defaults-file=/data/conf/for_pxc/p2.cnf &

注意第二个节点不需要你去初始化,pxc自动去把第一个节点拷贝过来

QQ图片20161221140643

11.复制其他节点的配置文件


[mysql@10-25-126-227 for_pxc]$ pwd
/data/conf/for_pxc
[mysql@10-25-126-227 for_pxc]$ for i in {3..9};do cp p2.cnf p$i.cnf;done

12.修改其他节点配置文件


/*修改端口*/
for i in {3..9};do let temp=$i-1;sed -i "s/port = 6021/port = 602$temp/g" p$i.cnf;done
/*修改socket*/
for i in {3..9};do sed -i "s/socket=\/data\/for_pxc\/p2\/p2.sock/socket=\/data\/for_pxc\/p$i\/p$i.sock/g" p$i.cnf;done
/*修改datadir */
for i in {3..9};do sed -i  "s/datadir=\/data\/for_pxc\/p2/datadir=\/data\/for_pxc\/p$i/g" p$i.cnf;done
/*修改server-id*/
for i in {3..9};do sed -i   "s/server-id=2/server-id=$i/g" p$i.cnf;done
/* wsrep_sst_receive_address */
for i in {3..9};do let temp=$i-1; sed -i  "s/wsrep_sst_receive_address=10.25.126.227:6021/wsrep_sst_receive_address=10.25.126.227:602$temp/g" p$i.cnf;done
/*修改wsrep_node_incoming_address */
for i in {3..9};do let temp=$i-1; sed -i  "s/wsrep_node_incoming_address=10.25.126.227:6011/wsrep_node_incoming_address=10.25.126.227:601$temp/g" p$i.cnf;done
/*修改wsrep_node_name */
for i in {3..9};do sed -i   "s/wsrep_node_name=pxc2/wsrep_node_name=pxc$i/g" p$i.cnf;done
/*修改wsrep_provider_options */
for i in {3..9};do let temp=$i-1; sed -i "s/listen_addr=tcp:\/\/10.25.126.227:6031/listen_addr=tcp:\/\/10.25.126.227:603$temp/g" p$i.cnf;done

13.依次启动其他节点


for i in {3..9};do ./bin/mysqld_safe --defaults-file=/data/conf/for_pxc/p$i.cnf & done

QQ图片20161221140846

Write a Reply or Comment

电子邮件地址不会被公开。 必填项已用*标注