一、环境

CentOS release 6.9

Percona-XtraDB-Cluster-5.7.19-29.22.tar.gz

boost_1_59_0.tar.gz

percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm
节点 mysql服务端口 gmcast.listen端口
node01:192.168.1.210:6600 6600 6630
node02:192.168.1.210:6602 6602 6632
node03:gardb 5567

二、下载依赖包

yum install -y git scons gcc gcc-c++ openssl check cmake bison \

boost-devel asio-devel libaio-devel ncurses-devel readline-devel pam-devel 

yum -y install  libtool ncurses-devel libgcrypt-devel  libev-devel \

git scons  gcc  gcc-c++  openssl  check  cmake  bison boost-devel  \

asio-devel libaio-devel  ncurses-devel  readline-devel  pam-devel socat \

libaio  automake autoconf vim redhat-lsb check-devel

yum -y install libnl-devel openssl-devel libnfnetlink-devel ipvsadm \

popt-devel libnfnetlink kernel-devel popt-static iptraf

yum install numactl libev socat perl-DBD-mysql perl-Time-HiRes

 配置epel库
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo 

三、编译安装:

3.1 编译安装percona-xtrabackup

cd /usr/local/percona-xtrabackup-2.4.5/
cmake ./ -DBUILD_CONFIG=xtrabackup_release \
-DWITH_MAN_PAGES=OFF \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST="/usr/local/boost" 
make -j 4 && make install

3.2 编译安装Percona-XtraDB-Cluster-5.7.19-29.22

编译安装前最好要卸载掉本地的mysql数据库

 rpm-qa | grep mysql (注意大小写,如果mysql不行就换MySQL)
 yum -y remove mysql的包名

卸载后/var/lib/mysql中的数据及/etc/my.cnf不会删除,如果确定没用后就手工删除

 rm -f /etc/my.cnf
 rm -rf /var/lib/mysql

创建mysql用户名及文件夹

mkdir /data/6600 -p
mkdir /data/6602 -p
mkdir /data/{6600,6602}/{binlog,slow} -p
useradd mysql
chown -R mysql:mysql /data/

编译garbd,libgalera_smm.so

 cd /usr/local/mysql
 cd "percona-xtradb-cluster-galera"

获取revno值

cat GALERA-REVISION
#revno="GALERA-REVISION"
scons -j4 psi=1 --config=force  revno="8678538"  boost_pool=0 libgalera_smm.so
scons -j4 --config=force revno="8678538" garb/garbd

创建pxc安装目录

mkdir -p /usr/local/mysql/{bin,lib}
chown -R mysql:mysql /usr/local/mysql

拷贝编译结果文件到pxc安装目录

 cp garb/garbd /usr/local/mysql/bin
 cp libgalera_smm.so /usr/local/mysql/lib

检查复制的文件是否正常

 ls -l /usr/local/mysql/bin/garbd
 ls -l /usr/local/mysql/lib/libgalera_smm.so

-DMYSQL_SERVER_SUFFIX 值获取

cd /usr/local/Percona-XtraDB-Cluster-5.7.19-29.22
WSREP_VERSION="$(grep WSREP_INTERFACE_VERSION wsrep/wsrep_api.h | cut -d '"' -f2).$(grep 'SET(WSREP_PATCH_VERSION'  "cmake/wsrep.cmake" | cut -d '"' -f2)"
echo $WSREP_VERSION

-DCOMPILATION_COMMENT 值获取

cd /usr/local/Percona-XtraDB-Cluster-5.7.19-29.22

source VERSION

MYSQL_VERSION="$MYSQL_VERSION_MAJOR.$MYSQL_VERSION_MINOR.$MYSQL_VERSION_PATCH"
echo $MYSQL_VERSION

REVISION="$(cd "$SOURCEDIR"; grep '^short: ' Docs/INFO_SRC |sed -e 's/short: //')"

echo "Percona XtraDB Cluster binary (GPL) $MYSQL_VERSION-$WSREP_VERSION Revision $REVISION"

编译Percona-XtraDB-Cluster-5.7.16-27.19

cd /usr/local/mysql
cmake ./
-DBUILD_CONFIG=mysql_release \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DWITH_EMBEDDED_SERVER=OFF \
-DFEATURE_SET=community \
-DENABLE_DTRACE=OFF \
-DWITH_SSL=system -DWITH_ZLIB=system \
-DCMAKE_INSTALL_PREFIX="/usr/local/mysql" \
-DMYSQL_DATADIR="/usr/local/mysql/data" \
-DMYSQL_SERVER_SUFFIX="29.22" \
-DWITH_INNODB_DISALLOW_WRITES=ON \
-DWITH_WSREP=ON \
-DWITH_UNIT_TESTS=0 \
-DWITH_READLINE=system \
-DWITHOUT_TOKUDB=ON \
-DWITHOUT_ROCKSDB=ON \
-DCOMPILATION_COMMENT="Percona XtraDB Cluster binary (GPL) 5.7.19-29.22 Revision c10027a" \
-DWITH_PAM=ON \
-DWITH_INNODB_MEMCACHED=ON \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST="/usr/local/boost" \
-DWITH_SCALABILITY_METRICS=ON

make && make install

3.3一键安装脚本

#!/bin/sh
Home_Dir=/home/admin/tools
Intall_Dir=/usr/local/mysql
DATA_DIR=/usr/local/mysql/data

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

yum -y install  libtool ncurses-devel libgcrypt-devel  libev-devel \
git scons  gcc  gcc-c++  openssl  check  cmake  bison boost-devel  \
asio-devel libaio-devel  ncurses-devel  readline-devel  pam-devel socat \
libaio  automake autoconf  redhat-lsb check-devel curl  curl-devel xinetd

yum -y install libnl-devel openssl-devel libnfnetlink-devel ipvsadm \
popt-devel libnfnetlink kernel-devel popt-static iptraf

yum -y install numactl libev  perl-DBD-mysql perl-Time-HiRes

mkdir -p /usr/local/boost
tar xf ${Home_Dir}/boost_1_59_0.tar.gz -C /usr/local/boost
####安装xtrabackup我用的RPM包
rpm -ivh ${Home_Dir}/percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm

tar xf ${Home_Dir}/Percona-XtraDB-Cluster-5.7.19-29.22.tar.gz

mkdir -p ${Intall_Dir}/{binlog,slow}
mkdir -p ${Intall_Dir}/{bin,lib}

useradd mysql
chown -R mysql:mysql ${Intall_Dir}

cd ${Home_Dir}/Percona-XtraDB-Cluster-5.7.19-29.22
cd "percona-xtradb-cluster-galera"

Revno=`cat GALERA-REVISION`
scons -j4 psi=1 --config=force  revno="$Revno"  boost_pool=0 libgalera_smm.so
scons -j4 --config=force revno="$Revno" garb/garbd

cp garb/garbd ${Intall_Dir}/bin
cp libgalera_smm.so ${Intall_Dir}/lib

cd ${Home_Dir}/Percona-XtraDB-Cluster-5.7.19-29.22
WSREP_VERSION="$(grep WSREP_INTERFACE_VERSION wsrep/wsrep_api.h | cut -d '"' -f2).$(grep 'SET(WSREP_PATCH_VERSION'  "cmake/wsrep.cmake" | cut -d '"' -f2)"

source ./VERSION

MYSQL_VERSION="$MYSQL_VERSION_MAJOR.$MYSQL_VERSION_MINOR.$MYSQL_VERSION_PATCH"

REVISION="$(cd "$SOURCEDIR"; grep '^short: ' Docs/INFO_SRC |sed -e 's/short: //')"

DCT=`echo "Percona XtraDB Cluster binary (GPL) $MYSQL_VERSION-$WSREP_VERSION Revision $REVISION"`

cd ${Home_Dir}/Percona-XtraDB-Cluster-5.7.19-29.22
cmake ./ -DBUILD_CONFIG=mysql_release \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DWITH_EMBEDDED_SERVER=OFF \
-DFEATURE_SET=community \
-DENABLE_DTRACE=OFF \
-DWITH_SSL=system -DWITH_ZLIB=system \
-DCMAKE_INSTALL_PREFIX="$Intall_Dir" \
-DMYSQL_DATADIR="$DATA_DIR" \
-DMYSQL_SERVER_SUFFIX="$WSREP_VERSION" \
-DWITH_INNODB_DISALLOW_WRITES=ON \
-DWITH_WSREP=ON \
-DWITH_UNIT_TESTS=0 \
-DWITH_READLINE=system \
-DWITHOUT_TOKUDB=ON \
-DWITHOUT_ROCKSDB=ON \
-DCOMPILATION_COMMENT="$DCT" \
-DWITH_PAM=ON \
-DWITH_INNODB_MEMCACHED=ON \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST="/usr/local/boost" \
-DWITH_SCALABILITY_METRICS=ON

make && make install

有关参数的详解可以看我上篇博客

四、编译配置文件

4.1 编译配置文件
第一个实例:6600

vim /data/6600/my.cnf
[client]
socket=/data/6600/mysql.sock
port            = 6600
default-character-set = utf8

[mysqld]
socket=/data/6600/mysql.sock
basedir = /data
datadir = /data/6600/data
port = 6600
server_id = 16600
pid-file = /data/6600/mysql_6600.pid
key_buffer_size =128M
max_allowed_packet = 16M
table_open_cache = 512
max_connections = 200
sort_buffer_size = 20M
read_buffer_size = 20M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
character-set-server=utf8

innodb_buffer_pool_size = 256M
innodb_log_file_size = 48M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table=1
lower_case_table_names=1
innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5120M

explicit_defaults_for_timestamp=TRUE
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,only_full_group_by
skip-name-resolve

log-bin = /data/6600/binlog/mysql-bin
expire_logs_days = 7
#binlog-do-db=plateform_stable,finance_stable,location_log_stable
#binlog-ignore-db=mysql,information_schema,test

binlog_format=ROW
log_slave_updates
symbolic-links=0
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
log-error=/data/6600/mysqld_6600-error.log

wsrep_cluster_name = pxc
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_cluster_address='gcomm://192.168.1.210:6630,192.168.1.210:6632'
wsrep_sst_receive_address=192.168.1.210:6620
wsrep_node_incoming_address=192.168.1.210:6600
wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.1.210:6030;ist.recv_addr=192.168.1.210:6631;"
wsrep_slave_threads= 4
wsrep_log_conflicts
wsrep_node_address=192.168.1.210:6600
#wsrep_node_name=pxc-cluster-node-
wsrep_sst_method=xtrabackup-v2
#wsrep_sst_method=mysqldump
wsrep_sst_auth=sstuser:sstpasswd
#pxc_strict_mode=ENFORCING
wsrep_log_conflicts
wsrep_retry_autocommit=8
secure_file_priv=""
innodb_page_cleaners=8

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
port            = 6600
default-character-set = utf8
socket=/data/6600/mysql.sock

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

第二个实例:6602

[client]
socket=/data/6602/mysql.sock
port            = 6602
default-character-set = utf8

[mysqld]
socket=/data/6602/mysql.sock
basedir = /data
datadir = /data/6602/data
port = 6602
server_id = 6602
pid-file = /data/6602/mysql_6602.pid
key_buffer_size =128M
max_allowed_packet = 16M
table_open_cache = 512
max_connections = 200
sort_buffer_size = 20M
read_buffer_size = 20M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
character-set-server=utf8

innodb_buffer_pool_size = 256M
innodb_log_file_size = 48M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table=1
lower_case_table_names=1
innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5120M

explicit_defaults_for_timestamp=TRUE
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,only_full_group_by
skip-name-resolve

log-bin = /data/6602/binlog/mysql-bin
expire_logs_days = 7
#binlog-do-db=plateform_stable,finance_stable,location_log_stable
#binlog-ignore-db=mysql,information_schema,test

binlog_format=ROW
log_slave_updates
symbolic-links=0
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
log-error=/data/6602/mysqld_6602-error.log

wsrep_cluster_name = pxc
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_cluster_address='gcomm://192.168.1.210:6630,192.168.1.210:6632'
wsrep_sst_receive_address=192.168.1.210:6622
wsrep_node_incoming_address=192.168.1.210:6602
wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.1.210:6632;ist.recv_addr=192.168.1.210:6633;"
wsrep_slave_threads= 4
wsrep_log_conflicts
wsrep_node_address=192.168.1.210:6602
#wsrep_node_name=pxc-cluster-node-
wsrep_sst_method=xtrabackup-v2
#wsrep_sst_method=mysqldump
wsrep_sst_auth=sstuser:sstpasswd
#pxc_strict_mode=ENFORCING
wsrep_log_conflicts
wsrep_retry_autocommit=8
secure_file_priv=""
innodb_page_cleaners=8

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
port            = 6602
default-character-set = utf8
socket=/data/6602/mysql.sock

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

五、初始化启动pxc主节点

5.1 初始化pxc

初始化6600

chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/6600{,6602}
/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/6600/data --user=mysql  --initialize 

初始化6602

/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/6602/data --user=mysql  --initialize 

初始化错误可以清空msyql/data/目录下的文件
注意记录初始化密码

5.2 启动mysql
启动6600实例

/usr/local/mysql/bin/mysqld --defaults-file=/data/6602/my.cnf  --wsrep-new-cluster &

注意主节点启动要加上--wsrep-new-cluster,其他节点不用。

启动6600实例

/usr/local/mysql/bin/mysqld --defaults-file=/data/6602/my.cnf &

5.3 更改密码

echo "export .:/usr/local/mysql/bin:$PATH" >>/etc/profile
source /etc/profile

用刚刚初始化的密码进去MySQL实例6600

mysql -uroot -p${mypasswd} -S /data/6600/mysql.sock

第一次进去会提示改密码

 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

用刚刚初始化的密码进去MySQL实例6602

mysql -uroot -p${mypasswd} -S /data/6602/mysql.sock

第一次进去会提示改密码

 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

5.4 为wsrep_sst_auth传输创建用户
随便选择一个节点创建

mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'sstpassword';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;

六、使用garbd

pxc集群最少是要3台,仲裁者garbd,用来解决

cd /usr/local/mysql/bin/
    ./garbd --group=pxc-cluster --address=gcomm://192.168.1.210:6630,192.168.1.210:6632 --option=gmcast.listen_addr=tcp://192.168.1.210:5567 -d -l /tmp/garbd.log
    #全部起完了之后就可以看到节点数和集群

七、检查

#查看MySQL 节点数
 mysql> show global status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)

是三个表示pxc集群搭建成功,可以在一个节点上创建一张表,其他节点可以查询的到说明集群运行正常。

查看进程数

ps -ef|grep mysql
mysql    14316     1  0 Jun28 ?        00:06:18 /usr/local/mysql/bin/mysqld --defaultsfile=/data/6600/my.cnf --wsrep-new-cluster
mysql    15611     1  0 Jun28 ?        00:03:37 /usr/local/mysql/bin/garbd --group=pxc --address=gcomm://192.168.1.210:6630,192.168.1.210:6632 --option=gmcast.listen_addr=tcp://192.168.1.210:5567 -d -l /tmp/garbd.log
mysql    23001     1  0 Jun29 ?        00:04:55 /usr/local/mysql/bin/mysqld --defaults-file=/data/6602/my.cnf