一、环境准备
pxcnode71 pxcnode72 pxcnode73 三台KVM(虚拟机)
因为PXC是开源软件所以需要去官网偷,和MHA功能一样但是简便高效(目前企业很多都在向PXC数据库服务器集群转移)
官网:http://galeracluster.com
#PXE所需要的软件提前下载到了PXC目录里
[student@wxk 12306]$ ls /linux-soft/03/PXC/
libev-4.15-1.el6.rf.x86_64.rpm Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar
percona-release-0.1-4.noarch.rpm qpress-1.1-14.11.x86_64.rpm
percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm
1.真机上往KVM传输rpm包
#传送PXE所需包
[student@wxk 12306]$for i in 1 2 3
do
scp -r /linux-soft/03/PXC [email protected]$i:/root/
done
2.以下操作均在71上即可
#生成秘钥
[root@pxcnode71 ~]#ssh-keygen
#传输秘钥
for i in 1 2 3
do
ssh-copy-id 192.168.4.7$i
done
#写入hosts
[root@pxcnode71 ~]#echo "192.168.4.71 pxcnode71
192.168.4.72 pxcnode72
192.168.4.73 pxcnode73" >> /etc/hosts
#传输给各个pxc服务器
[root@pxcnode71 ~]#for i in 1 2 3
do
scp /etc/hosts pxcnode7$i:/etc/
done
#一键配置71、72、73装包服务
[root@pxcnode71]# for i in 1 2 3; do ssh pxcnode7$i 'cd /root/PXC&& rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm&&yum -y install percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm&&rpm -ivh qpress-1.1-14.11.x86_64.rpm&& tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar&&yum -y install Percona-XtraDB-Cluster-*.rpm'; done
二、修改配置文件
1.以pxcnode71为例(72、73与71配置同步)
************配置文件介绍及修改
[root@pxcnode71 ~]# ls /etc/percona-xtradb-cluster.conf.d/ #配置文件所在目录
mysqld.cnf mysqld_safe.cnf wsrep.cnf
#mysqld.cnf 数据库服务运行参数配置文件
#mysqld_safe.cnf Percona Server 5.7配置文件
#wsrep.cnf PXC集群配置文件
[root@pxcnode71 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
....
[mysqld]
server-id=1 #id不允许重复(三台依次是1、2、3)
datadir=/var/lib/mysql #数据库目录
socket=/var/lib/mysql/mysql.sock #socket文件(在我博文--主从服务搭建一文中有介绍)
log-error=/var/log/mysqld.log #日志文件
pid-file=/var/run/mysqld/mysqld.pid #pid文件
log-bin #启用binlog日志
log_slave_updates #启用链式复制
expire_logs_days=7 #日志文件保留天数
....
[root@pxcnode71 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid #pid文件位置记名字
socket = /var/lib/mysql/mysql.sock #socket文件名及位置
[root@pxcnode71 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
...
wsrep_cluster_address=gcomm://192.168.4.72,192.168.4.73,192.168.4.71 #集群成员(本机ip放最后)
wsrep_node_address=192.168.4.71 #本地主机ip
wsrep_cluster_name=pxc-cluster #集群名称,3台必须相同(做实验使用默认即可)
wsrep_node_name=pxcnode71 #本地主机名字
wsrep_sst_auth="sstuser:123qqq...A" #SST数据库同步授权用户(用户名:密码)
...
[root@pxcnode71 ~]# systemctl start [email protected] #启动服务(只在71上打即可,时间较长耐心等待)
#71、72只用执行systemctl start mysql 命令启动数据库即可(PXC内置数据服务名为mysql没有d,时间较长)
[root@pxcnode71 ~]# p=`grep password /var/log/mysqld.log |sed -n '1p'|awk '{print $11}'` #获取初始密码并赋值给p
[root@pxcnode71 ~]# mysql -p$p
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 11
Server version: 5.7.25-28-57-log
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> alter user user() identified by '123456'; #修改密码(PXC默认没有密码安全策略限制)
Query OK, 0 rows affected (0.20 sec)
mysql> grant reload,lock tables,replication client,process on *.* to sstuser@"localhost" identified by '123qqq...A'; #为sstuser用户授权
Query OK, 0 rows affected, 1 warning (0.20 sec)
2.启动服务(73同步72操作即可)
[root@pxcnode72 ~]# systemctl start mysql
[root@pxcnode72 ~]# netstat -ntlupa|grep :3306 #查看是否存在3306
tcp6 0 0 :::3306 :::* LISTEN 27182/mysqld
[root@pxcnode72 ~]# netstat -ntlupa|grep :4567 #查看集群通讯端口(4567)是否启动
tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 27182/mysqld
tcp 0 0 192.168.4.72:4567 192.168.4.73:44198 ESTABLISHED 27182/mysqld
tcp 0 0 192.168.4.72:41938 192.168.4.71:4567 ESTABLISHED 27182/mysqld
[root@pxcnode72 ~]# mysql -p123456 #密码为71所修改的root密码
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 13
Server version: 5.7.25-28-57-log Percona XtraDB Cluster (GPL), Release rel28, Revision a2ef85f, WSREP version 31.35, wsrep_31.35
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> select user,host from mysql.user; #查询在71中所授权的用户是否同步过来(sstuser)
#注意:sstuser用户授权的是71本地用户所以72&73上无法使用
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
| sstuser | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
三、配置服务
1.测试配置
[root@pxcnode71 ~]#mysql -p123456
mysql> show status like '%wsrep%'; #查看集群信息(71 72 73任意一台都可查看)
+----------------------------------+-------------------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------------------+
| wsrep_local_state_uuid | 53e171b5-d2fc-11e9-a3c0-93e02e91faf9 |
| wsrep_protocol_version | 9 |
| wsrep_last_applied | 2 |
| wsrep_last_committed | 2 |
| wsrep_replicated | 2 |
| wsrep_replicated_bytes | 544 |
| wsrep_repl_keys | 2 |
| wsrep_repl_keys_bytes | 64 |
| wsrep_repl_data_bytes | 346 |
| wsrep_repl_other_bytes | 0 |
| wsrep_received | 7 |
| wsrep_received_bytes | 718 |
| 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 | 2 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_recv_queue_avg | 0.142857 |
| 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 | [ 173, 173 ] |
| wsrep_flow_control_interval_low | 173 |
| wsrep_flow_control_interval_high | 173 |
| wsrep_flow_control_status | OFF |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| 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 | 2216 |
| 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.4.73:3306,192.168.4.72:3306,192.168.4.71:3306 |
| wsrep_cluster_weight | 3 |
| 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 | 53d67639-d2fc-11e9-adc8-cea258915c56 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 53e171b5-d2fc-11e9-a3c0-93e02e91faf9 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 2 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy |
| wsrep_provider_version | 3.35(rddf9876) |
| wsrep_ready | ON |
+----------------------------------+-------------------------------------------------------+
71 rows in set (0.01 sec)
#挑几个常看的:
#wsrep_incoming_addresses 成员列表(192.168.4.73:3306,192.168.4.72:3306,192.168.4.71:3306)
#wsrep_cluster_size 集群服务器台数(3)
#wsrep_cluster_status 集群链接状态(Primary)
#wsrep_connected 连接状态(ON)
#wsrep_ready 服务状态(ON)
mysql> create database gamedb; #三台任意体态操作都可以(测试创库表)
Query OK, 1 row affected (0.25 sec)
mysql> create table gamedb.stuinfo(
-> id int primary key auto_increment,
-> name char(15),
-> age tinyint unsigned,
-> class char(9));
Query OK, 0 rows affected (1.35 sec)
mysql> grant all on gamedb.* to yaya@"%" identified by '123456';
Query OK, 0 rows affected, 1 warning (0.31 sec)
mysql> desc gamedb.stuinfo; #查看表结构
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(15) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| class | char(9) | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> insert into gamedb.stuinfo(name,age,class)values('csdnak',1,'1906');
Query OK, 1 row affected (0.15 sec)
mysql> insert into gamedb.stuinfo(name,age,class)values('ak',22,'1906');
Query OK, 1 row affected (0.49 sec)
mysql> insert into gamedb.stuinfo(name,age,class)values('plj',28,'1906');
Query OK, 1 row affected (0.07 sec)
mysql> select * from gamedb.stuinfo; #查询插入信息(72 73也能查到)
+----+--------+------+-------+
| id | name | age | class |
+----+--------+------+-------+
| 3 | csdnak | 1 | 1906 |
| 6 | ak | 22 | 1906 |
| 9 | plj | 28 | 1906 |
+----+--------+------+-------+
3 rows in set (0.01 sec)
2.测试自动恢复功能
#断开72服务(断开任意一台都可以除了71,因为71根本没起mysql服务)
[root@pxcnode72 ~]# systemctl stop mysql
[root@pxcnode71 ~]# mysql -p123456
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 15
Server version: 5.7.25-28-57-log Percona XtraDB Cluster (GPL), Release rel28, Revision a2ef85f, WSREP version 31.35, wsrep_31.35
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 status like '%wsrep_incoming%';
+--------------------------+-------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------+
| wsrep_incoming_addresses | 192.168.4.73:3306,192.168.4.71:3306 | #发现回少一条72的(自动断开)
+--------------------------+-------------------------------------+
1 row in set (0.01 sec)
#进入72服务器恢复服务
[root@pxcnode72 ~]# systemctl restart mysql #重新启动
[root@pxcnode72 ~]# mysql -p123456 #进入数据库查看是否自动恢复成功
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 11
Server version: 5.7.25-28-57-log Percona XtraDB Cluster (GPL), Release rel28, Revision a2ef85f, WSREP version 31.35, wsrep_31.35
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 status like '%wsrep_incoming%';
+--------------------------+-------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------+
| wsrep_incoming_addresses | 192.168.4.73:3306,192.168.4.71:3306,192.168.4.72:3306 | #此时发现已经恢复
+--------------------------+-------------------------------------------------------+
1 row in set (0.01 sec)
#以上就是PXC部署过程