其他分享
首页 > 其他分享> > Percona XtraDB Cluster(PXC 5.7)实战

Percona XtraDB Cluster(PXC 5.7)实战

作者:互联网

一、环境准备

1.1、环境准备

192.168.247.121  pxc-01  2vcpu 2G 100G centos7.9
192.168.247.122  pxc-02  2vcpu 2G 100G centos7.9
192.168.247.123  pxc-03  2vcpu 2G 100G centos7.9
192.168.247.124  pxc-04  2vcpu 2G 100G centos7.9

1.2、初始化虚机环境

#!/bin/bash
#安装所需要的包
yum install -y wget curl chrony net-tools 

#配置时间同步
cat > /etc/chrony.conf <<EOF
server ntp.aliyun.com iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
logchange 0.5
logdir /var/log/chrony
EOF

#启动chronyd服务
systemctl enable chrony && systemctl start chrony

#关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

#设置时区
timedatectl set-timezone "Asia/Shanghai"

#清清除iptables防火墙规则
iptables -F && iptables -X && iptables -Z

1.3、在四台节点配置hosts、pxc.repo

cat >> /etc/hosts <<EOF
192.168.247.121 pxc-01
192.168.247.122 pxc-02
192.168.247.123 pxc-03
192.168.247.124 pxc-04
EOF

配置pxc源 cat /etc/yum.repos.d/pxc.repo [percona] name=percona_repo baseurl = https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch enabled = 1 gpgcheck = 0 yum makecache

二、安装配置pxc服务

2.1、在前三个节点安装pxc5.7

[root@pxc-01 ~]# yum install -y  Percona-XtraDB-Cluster-57
[root@pxc-02 ~]# yum install -y  Percona-XtraDB-Cluster-57
[root@pxc-03 ~]# yum install -y  Percona-XtraDB-Cluster-57

2.2、配置pxc集群节点

pxc-01配置

[root@pxc-01 ~]# cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=121
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[root@pxc-01 ~]# 
[root@pxc-01 ~]# egrep -v "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.247.121,192.168.247.122,192.168.247.123
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.247.121
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-1
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"
[root@pxc-01 ~]# 

pxc-02配置

[root@pxc-02 ~]# cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=122
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[root@pxc-02 ~]# 
[root@pxc-02 ~]# egrep -v "^$|^#" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.247.121,192.168.247.122,192.168.247.123
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.247.122
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-2
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"
[root@pxc-02 ~]# 

pxc-03配置

[root@pxc-03 ~]# cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=123
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[root@pxc-03 ~]# 
[root@pxc-03 ~]# egrep -v "^$|^#" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.247.121,192.168.247.122,192.168.247.123
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.247.123
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-3
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"
[root@pxc-03 ~]# 

2.3、启动第一个节点

systemctl start mysql@bootstrap.service

[root@pxc-01 ~]# ss -ntul
Netid  State      Recv-Q Send-Q                                              Local Address:Port                                                             Peer Address:Port              
udp    UNCONN     0      0                                                       127.0.0.1:323                                                                         *:*                  
udp    UNCONN     0      0                                                           [::1]:323                                                                      [::]:*                  
tcp    LISTEN     0      128                                                             *:22                                                                          *:*                  
tcp    LISTEN     0      100                                                     127.0.0.1:25                                                                          *:*                  
tcp    LISTEN     0      128                                                          [::]:22                                                                       [::]:*                  
tcp    LISTEN     0      100                                                         [::1]:25                                                                       [::]:*                  
[root@pxc-01 ~]# systemctl start mysql@bootstrap.service
[root@pxc-01 ~]# ss -ntul
Netid  State      Recv-Q Send-Q                                              Local Address:Port                                                             Peer Address:Port              
udp    UNCONN     0      0                                                       127.0.0.1:323                                                                         *:*                  
udp    UNCONN     0      0                                                           [::1]:323                                                                      [::]:*                  
tcp    LISTEN     0      128                                                             *:22                                                                          *:*                  
tcp    LISTEN     0      128                                                             *:4567                                                                        *:*                  
tcp    LISTEN     0      100                                                     127.0.0.1:25                                                                          *:*                  
tcp    LISTEN     0      128                                                          [::]:22                                                                       [::]:*                  
tcp    LISTEN     0      100                                                         [::1]:25                                                                       [::]:*                  
tcp    LISTEN     0      80                                                           [::]:3306                                                                     [::]:*                  
[root@pxc-01 ~]# 

2.4、查看root密码

[root@pxc-01 ~]# grep "temporary password" /var/log/mysqld.log 
2022-09-05T13:07:00.041247Z 1 [Note] A temporary password is generated for root@localhost: 9lnSeeBzg4*z
[root@pxc-01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.39-42-57-log

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

2.5、修改数据密码

alter user 'root'@'localhost' identified by 'magedu';
CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cretPass';
GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';

mysql> alter user 'root'@'localhost' identified by 'magedu';
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cretPass';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> 

2.6、查看相关状态变量

mysql> show status like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | a72ed4d9-2d1b-11ed-8c3a-cf03895c255d |
| wsrep_protocol_version           | 9                                    |
| wsrep_last_applied               | 3                                    |
| wsrep_last_committed             | 3                                    |
| wsrep_replicated                 | 3                                    |
| wsrep_replicated_bytes           | 760                                  |
| wsrep_repl_keys                  | 3                                    |
| wsrep_repl_keys_bytes            | 96                                   |
| wsrep_repl_data_bytes            | 463                                  |
| wsrep_repl_other_bytes           | 0                                    |
| wsrep_received                   | 2                                    |
| wsrep_received_bytes             | 157                                  |
| wsrep_local_commits              | 0                                    |
| wsrep_local_cert_failures        | 0                                    |
| wsrep_local_replays              | 0                                    |
| wsrep_local_send_queue           | 0                                    |
| wsrep_local_send_queue_max       | 1                                    |
| wsrep_local_send_queue_min       | 0                                    |
| wsrep_local_send_queue_avg       | 0.000000                             |
| wsrep_local_recv_queue           | 0                                    |
| wsrep_local_recv_queue_max       | 1                                    |
| wsrep_local_recv_queue_min       | 0                                    |
| wsrep_local_recv_queue_avg       | 0.000000                             |
| wsrep_local_cached_downto        | 1                                    |
| wsrep_flow_control_paused_ns     | 0                                    |
| wsrep_flow_control_paused        | 0.000000                             |
| wsrep_flow_control_sent          | 0                                    |
| wsrep_flow_control_recv          | 0                                    |
| wsrep_flow_control_interval      | [ 100, 100 ]                         |
| wsrep_flow_control_interval_low  | 100                                  |
| wsrep_flow_control_interval_high | 100                                  |
| wsrep_flow_control_status        | OFF                                  |
| wsrep_flow_control_active        | false                                |
| wsrep_flow_control_requested     | false                                |
| wsrep_cert_deps_distance         | 1.000000                             |
| wsrep_apply_oooe                 | 0.000000                             |
| wsrep_apply_oool                 | 0.000000                             |
| wsrep_apply_window               | 1.000000                             |
| wsrep_apply_waits                | 0                                    |
| wsrep_commit_oooe                | 0.000000                             |
| wsrep_commit_oool                | 0.000000                             |
| wsrep_commit_window              | 1.000000                             |
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
| wsrep_cert_index_size            | 1                                    |
| wsrep_cert_bucket_count          | 22                                   |
| wsrep_gcache_pool_size           | 2200                                 |
| wsrep_causal_reads               | 0                                    |
| wsrep_cert_interval              | 0.000000                             |
| wsrep_open_transactions          | 0                                    |
| wsrep_open_connections           | 0                                    |
| wsrep_ist_receive_status         |                                      |
| wsrep_ist_receive_seqno_start    | 0                                    |
| wsrep_ist_receive_seqno_current  | 0                                    |
| wsrep_ist_receive_seqno_end      | 0                                    |
| wsrep_incoming_addresses         | 192.168.247.121:3306                 |
| wsrep_cluster_weight             | 1                                    |
| wsrep_desync_count               | 0                                    |
| wsrep_evs_delayed                |                                      |
| wsrep_evs_evict_list             |                                      |
| wsrep_evs_repl_latency           | 0/0/0/0/0                            |
| wsrep_evs_state                  | OPERATIONAL                          |
| wsrep_gcomm_uuid                 | a7290a87-2d1b-11ed-905b-eb41545e93c1 |
| wsrep_gmcast_segment             | 0                                    |
| wsrep_cluster_conf_id            | 1                                    |
| wsrep_cluster_size               | 1                                    |
| wsrep_cluster_state_uuid         | a72ed4d9-2d1b-11ed-8c3a-cf03895c255d |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
| wsrep_local_bf_aborts            | 0                                    |
| wsrep_local_index                | 0                                    |
| wsrep_provider_name              | Galera                               |
| wsrep_provider_vendor            | Codership Oy <info@codership.com>    |
| wsrep_provider_version           | 3.61(rf47405c)                       |
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
75 rows in set (0.01 sec)

mysql> 

2.7、启动其他pxc节点

[root@pxc-02 ~]# systemctl start mysql
[root@pxc-03 ~]# systemctl start mysql

查看集群节点

 2.8、在pxc-01上创建新的数据库

[root@pxc-01 ~]# mysql -uroot -pmagedu
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.39-42-57-log Percona XtraDB Cluster (GPL), Release rel42, Revision 2186a14, WSREP version 31.61, wsrep_31.61

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> 
mysql> create database testdb1;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb1            |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

在 pxc-02查看数据是否同步

在pxc-03查看数据是否同步

2.9、利用xshell软件,同时在三个节点数据库写

 

 

这时发现只有一个节点成功,其他节点都提示失败,说明pxc集群所有节点可以同时进行数据库写操作

四、pxc集群节点扩容

4.1、将pxc-04节点加入集群

[root@pxc-04 ~]# yum install -y Percona-XtraDB-Cluster-57

[root@pxc-04 ~]# cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=124
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[root@pxc-04 ~]# egrep -v "^$|^#" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.247.121,192.168.247.122,192.168.247.123,192.168.247.124
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.247.124
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-4
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"
[root@pxc-04 ~]# 

4.2、启动mysql服务

4.3、查看集群节点信息

更改其他机器wsrep配置文件

 [root@pxc-01 ~]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_cluster_address=gcomm://192.168.247.121,192.168.247.122,192.168.247.123,192.168.247.124
[root@pxc-02 ~]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
[root@pxc-03 ~]# vi /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

 五、模拟故障

5.1、依次停止pxc-02、pxc-03

 5.2、此时集群wrsep_cluster_size数变为3

5.3、 停止pxc-03

 

 5.4、分别启动pxc-02、pxc-03

 

 

 5.5、创建新的数据库,检查是否同步

 5.6、测试数据库写

cat testlog.sql

create table testlog (id int auto_increment primary key,name char(10),salary int default 20);

delimiter $$

create procedure  sp_testlog() 
begin  
declare i int;
set i = 1; 
while i <= 100000 
do  insert into testlog(name,salary) values (concat('wang',FLOOR(RAND() * 100000)),FLOOR(RAND() * 1000000)); 
set i = i +1; 
end while; 
end$$

delimiter ;

pxc写10万条数据测试

[root@pxc-01 ~]# mysql -uroot -pmagedu
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.39-42-57-log Percona XtraDB Cluster (GPL), Release rel42, Revision 2186a14, WSREP version 31.61, wsrep_31.61

Copyright (c) 2009-2022 Percona LLC and/or its affiliates
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db                 |
| mysql              |
| performance_schema |
| sys                |
| testdb1            |
| testdb2            |
| testdb3            |
| testdb4            |
+--------------------+
9 rows in set (0.00 sec)

mysql> use db
Database changed
mysql> source /root/testlog.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+--------------+
| Tables_in_db |
+--------------+
| testlog      |
+--------------+
1 row in set (0.00 sec)

mysql> call sp_testlog;
Query OK, 1 row affected (4 min 32.10 sec)

mysql> select count(*) from testlog;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+
1 row in set (0.01 sec)

mysql> 

单机测试

[root@mysql-8-0-26 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| employees          |
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use hellodb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| emp               |
| scores            |
| students          |
| t1                |
| t2                |
| teachers          |
| testlog           |
| toc               |
+-------------------+
11 rows in set (0.00 sec)

mysql> drop tables testlog;
Query OK, 0 rows affected (0.02 sec)

mysql> source /root/testlog.sql
Query OK, 0 rows affected (0.01 sec)

ERROR 1304 (42000): PROCEDURE sp_testlog already exists
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| emp               |
| scores            |
| students          |
| t1                |
| t2                |
| teachers          |
| testlog           |
| toc               |
+-------------------+
11 rows in set (0.00 sec)

mysql> select count(*) from testlog
    -> ;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.01 sec)

mysql> call sp_testlog;
Query OK, 1 row affected (1 min 18.78 sec)

mysql> select count(*) from testlog;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+
1 row in set (0.00 sec)

mysql> 

总结: pxc集群对比单机,pxc高可用性占优势,但是写性能比较差,因为写需要进行数据校验,每写一条,需要进行一一同步。

5.7、pxc集群多机测试

pxc集群支持多机同时写,不会发生冲突~

标签:log,5.7,wsrep,cluster,Cluster,Percona,mysql,pxc,root
来源: https://www.cnblogs.com/cyh00001/p/16659790.html