一、环境初始化

  1.时间同步

配置/etc/chrony.conf,添加如下:
server 0.centos.pool.ntp.org iburst   #指定时钟源
allow 192.168.43.0/24 
重启服务:systemctl restart chronyd.service
开机启动:systemctl  enable chronyd
查看状态:systemctl  status chronyd
验证端口:netstat -lnup|grep 123   (使用udp协议,端口123)
其他节点同步此时钟源:ntpdate 192.168.43.110

 2.host解析配置 

192.168.43.110执行:hostnamectl set-hostname pxc01
192.168.43.120执行:hostnamectl set-hostname pxc02
192.168.43.130执行:hostnamectl set-hostname pxc03
三台机器/etc/hosts配置
192.168.43.110   pxc01
192.168.43.120   pxc02
192.168.43.130   pxc03

  3.防火墙和selinux配置

3306:mysql服务端口
4444:请求全量同步(sst)端口
4567:数据库节点之间通信
4568:请求增量同步(ist)端口
关闭selinux

  4.参数优化,mysql服务需要打开较多的文件数,默认用户只有1024

cat >> /etc/security/limits.conf << EOF
soft nproc 65536
hard nproc 65536
soft nofile 65536
hard nofile 65536
EOF

二、安装Percona-XtraDB-Cluster

  官方安装向导:https://docs.percona.com/percona-xtradb-cluster/8.0/quickstart-overview.html

  1.组件安装:

  下载Percona-XtraDB-Cluster包:https://www.percona.com/downloads#percona-xtradb-cluster

  选择版本和操作系统,下载全部包

  2.解压并安装:  

rpm -ivh *.rpm --nodeps --force

  3.启动mysql,并修改root密码

sudo service mysql start
sudo grep 'temporary password' /var/log/mysqld.log 找到密码
登陆mysql
mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootPass';
mysql> exit
$ sudo service mysql stop 

  4.编辑配置文件/etc/my.cnf:[mysqld]

[mysqld]
server-id=110 #三个节点的server_id不能一样 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid binlog_expire_logs_seconds=604800 wsrep_provider=/usr/lib64/galera4/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.43.110,192.168.43.120,192.168.43.130 binlog_format=ROW wsrep_slave_threads=8 wsrep_log_conflicts innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.43.110   #当前节点地址 wsrep_cluster_name=pxc-cluster wsrep_node_name=pxc01          #当前节点主机名 pxc_strict_mode=ENFORCING wsrep_sst_method=xtrabackup-v2 wsrep_provider_options=“socket.ssl_key=server-key.pem;socket.ssl_cert=server-cert.pem;socket.ssl_ca=ca.pem” [sst]
encrypt=4
ssl-key=server-key.pem #可以指定绝对路径/etc/mysql/certs/,使用自定义证书,下同。如果不指定,则会自动在mysql的数据目录下生成证书文件
ssl-ca=ca.pem
ssl-cert=server-cert.pem

  将配置复制到其他节点,并修改相应参数

scp /etc/my.cnf pxc02:/etc
scp /etc/my.cnf pxc03:/etc

   附加:生成自定义证书到/etc/mysql/certs/下(本实验没有使用自定义证书)

三台都执行:mkdir -p /etc/mysql/certs/
进入/etc/mysql/certs目录下:
(1)生成ca密钥文件
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 -key ca-key.pem -out ca.pem
(2)生成服务端密钥文件
openssl req -newkey rsa:2048 -days 3600 -nodes -keyout server-key.pem -out server-req.pem
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 3600 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
(3)生成客户端密钥文件
openssl req -newkey rsa:2048 -days 3600 -nodes -keyout client-key.pem -out client-req.pem
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 3600 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
(4)验证证书
openssl verify -CAfile ca.pem server-cert.pem client-cert.pem
显示如下:
server-cert.pem: OK 
client-cert.pem: OK
(5)输出证书信息:
openssl x509 -in server-cert.pem -text -noout
注意:Common Name (eg, your name or your server's hostname) []: 字段不能一样,否则验证报错 (
5)将证书文件复制到其他节点 scp -r /etc/mysql/certs pxc02:/etc/mysql scp -r /etc/mysql/certs pxc03:/etc/mysql 三台都执行:chown -R mysql:mysql /etc/mysql

三、启动服务

  启动第一个节点:systemctl  start mysql@bootstrap.service

四、启动其他节点:

  pxc01:进入数据库数据目录/var/lib/mysql下,复制*.pem到其他节点  

scp *.pem 192.168.43.120:/var/lib/mysql/
scp *.pem 192.168.43.130:/var/lib/mysql/
两台都要执行:chown -R mysql:mysql /etc/mysql

  pxc02和pxc03

systemctl start mysql

五、验证结果: