十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
环境:
操作系统centos7.4,mysql 安装在/data/mysql下,mysql属于mysql用户,pxc-5.7.19
1.1 下载pxc-5.7.19
mkdir -p /home/sunx/tools
wget -P /home/sunx/tools
https://www.percona.com/downloads/Percona-XtraDB-Cluster-57/Percona-XtraDB-Cluster-5.7.19-29.22/source/tarball/Percona-XtraDB-Cluster-5.7.19-29.22.tar.gz
1. 2 下载boost
mkdir -p /usr/local/boost
wget -P /usr/local/boost https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/download
1.3 下载percona-xtrabackup-2.4.5
wget -P /home/sunx/tools https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.5/source/tarball/percona-xtrabackup-2.4.5.tar.gz
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-develyum -y install libnl-devel openssl-devel libnfnetlink-devel ipvsadm \
popt-devel libnfnetlink kernel-devel popt-static iptrafyum -y install numactl libev perl-DBD-mysql perl-Time-HiRes
配置epel库
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
3.1 编译安装percona-xtrabackup
cd /usr/local/src/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/mysql
ln -s /data/mysql /usr/local/mysql
mkdir /usr/local/mysql/{binlog,slow} -p
useradd mysql
chown -R mysql:mysql /usr/local/mysql
编译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}
拷贝编译结果文件到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
4.1 编译配置文件
vim /usr/local/mysql/my.cnf
[client]
socket=/tmp/mysql.sock
port = 3306
default-character-set = utf8
[mysqld]
socket=/tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 10215
pid-file = /usr/local/mysql/mysql.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 = /usr/local/mysql/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=/usr/local/mysql/data/mysqld-error.log
wsrep_cluster_name = ycpxc ###指定您的群集的逻辑名称。 它必须对群集中的所有节点都相同。
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_cluster_address='gcomm://192.168.10.215,192.168.10.214,192.168.10.213'###指定群集中节点的IP地址,节点加入群集至少需要一个,但建议列出所有节点的地址。
wsrep_sst_receive_address=192.168.10.215###用于SST传送的端口
wsrep_node_incoming_address=192.168.10.215###定义节点期望客户端连接的IP地址和端口。
wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.10.215;ist.recv_addr=192.168.10.215;"
###gmcast.listen
###来自其他节点的地址Galera侦听连接。可以用来覆盖默认端口倾听,这是获得从连接地址。
###ist.recv_addr
###地址监听增量状态传输。
wsrep_slave_threads= 4
wsrep_log_conflicts
wsrep_node_address=192.168.10.215###集群节点地址
wsrep_sst_method=xtrabackup-v2###默认情况下,Percona XtraDB群集使用Percona XtraBackup进行状态快照传输(SST)。
wsrep_sst_auth=sstuser:passw0rd ###将SST的身份验证凭据指定为:。 您必须创建此用户
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 = 3306
default-character-set = utf8
socket=/tmp/mysql.sock
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
注意:在[client]、[mysqld]、[mysql]最好都要指定sock的位置,避免初始化和启动时报错去找默认的配置文件。
4.2pxc环境所涉及的端口
#mysql实例端口:3306.
#pxc cluster相互通讯的端口:4567
Port for group communication, default 4567. It can be changed by the option:
wsrep_provider_options ="gmcast.listen_addr=tcp://0.0.0.0:4567; "#用于SST传送的端口:4444
Port for State Transfer, default 4444. It can be changed by the option:
wsrep_sst_receive_address=10.11.12.205:4444#用于IST传送的端口:4568
Port for Incremental State Transfer, default port for group communication + 1 (4568). It can be changed by the option:
wsrep_provider_options = "ist.recv_addr=10.11.12.206:4568; "
4.1 为wsrep_sst_auth传输创建用户
当引导第一个节点并为其提供必要的权限时:
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'passw0rd';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON . TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;
5.1 初始化pxc
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --initialize
初始化错误可以清空msyql/data/目录下的文件
注意记录密码
5.2 启动mysql
/usr/local/mysql/bin/mysqld --defaults-file=./my.cnf --wsrep-new-cluster &
注意主节点启动要加上--wsrep-new-cluster,其他节点不用。
用刚刚初始化的密码进去MySQL
/usr/local/mysql/bin/mysql -uroot -p${mypasswd}
5.3 更改密码
第一次进去会提示改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
6.1 节点配置文件
192.168.10.214
安装方式都是和主节点是一样的,注意要卸载掉原来的mysql,更改配置文件的集群信息和server_id 即可。
只列出差异部分:
server_id = 10214
wsrep_sst_receive_address=192.168.10.214:6615###用于SST传送的端口
wsrep_node_incoming_address=192.168.10.214###定义节点期望客户端连接的IP地址和端口。
wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.10.214;ist.recv_addr=192.168.10.214:6618;"
wsrep_node_address=192.168.10.214###集群节点地址
192.168.10.213
server_id = 10213
wsrep_sst_receive_address=192.168.10.213###用于SST传送的端口
wsrep_node_incoming_address=192.168.10.213###定义节点期望客户端连接的IP地址和端口。
wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.10.213;ist.recv_addr=192.168.10.213;"
wsrep_node_address=192.168.10.213###集群节点地址
6.2 启动其他节点
#其它节点直接启动不用加--wsrep-new-cluster
/usr/local/mysql/bin/mysqld --defaults-file=../my.cnf
pxc集群最少是要3台,如果没有,可以使用仲裁者garbd,用来解决
可以找在一个网段的服务器上。也可以就在已有的节点上但是注意默认的端口不要冲突
7.1 #启用garbd
cd /usr/local/mysql/bin/
./garbd --group=pxc-cluster --address=gcomm://192.168.10.215,192.168.10.214 --option=gmcast.listen_addr=tcp://192.168.10.213 -d -l /tmp/garbd.log
#全部起完了之后就可以看到节点数和集群
7.2如果要在一台装有pxc 的服务器上起garbd就要更改默认的pxc集群的通信端口
cd /usr/local/mysql/bin/
./garbd --group=pxc-cluster --address=gcomm://192.168.10.215,192.168.10.214 --option=gmcast.listen_addr=tcp://192.168.10.214: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集群搭建成功,可以在一个节点上创建一张表,其他节点可以查询的到说明集群运行正常。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。