Linux/网络架构能力提升个人学习

MySQL高可用PXC(Percona XtraDB Clust

2021-06-16  本文已影响0人  一个小运维

概述

PXC介绍
PXC特点
相应端口

部署PXC

服务器角色
初始环境准备
[root@mysql1 ~]# for i in {1..3}
> do
> echo -e "192.168.1.1$i\tmysql$i" >> /etc/hosts
> done
[root@mysql1 ~]# yum clean all
[root@mysql1 ~]# yum remove -y mysql-community-*
[root@mysql1 ~]# yum install -y qpress-1.1-14.11 Percona-XtraDB-Cluster-*
配置服务
[root@mysql1 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
... ...
[mysqld]
server-id=11
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
... ...
[root@mysql1 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket   = /var/lib/mysql/mysql.sock
nice     = 0
[root@mysql1 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
# 修改以下内容:
wsrep_cluster_address=gcomm://192.168.1.11,192.168.1.12,192.168.1.13  # 集群成员
wsrep_node_address=192.168.1.11   # 本节点IP地址
wsrep_cluster_name=pxc-cluster    # 集群名
wsrep_node_name=mysql1            # 本节点名
wsrep_sst_auth="sstuser:TEST2021@guodong.com"   # SST数据同步授权用户及密码
启动服务
# 首次启动服务时间比较长
[root@mysql1 ~]# systemctl  start mysql@bootstrap.service
[root@mysql1 ~]# grep password /var/log/mysqld.log
[root@mysql1 ~]# mysql -uroot -p'!Yo.e(qv:0pH'
mysql> alter user  root@'localhost' identified by 'TEST2021@guodong.com';
mysql> grant reload, lock tables,replication client,process on *.*  to
    -> sstuser@'localhost' identified by 'TEST2021@guodong.com';
Query OK, 0 rows affected, 1 warning (0.20 sec)
[root@mysql2 ~]# systemctl start mysql
[root@mysql2 ~]# netstat -utnlp  | grep :3306
tcp6       0      0 :::3306                 :::*                    LISTEN      18875/mysqld
[root@mysql2 ~]# netstat -utnlp  | grep :4567
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      18875/mysqld

[root@mysql3 ~]# systemctl  start mysql
[root@mysql3 ~]# netstat -utnlp  | grep :3306
tcp6       0      0 :::3306                 :::*                    LISTEN      18465/mysqld
[root@mysql3 ~]# netstat -utnlp  | grep :4567
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      18465/mysqld

测试配置

查看集群信息
[root@mysql2 ~]# mysql -uroot -pTEST2021@guodong.com
mysql> show status like "%wsrep%";  # 找到以下信息
| wsrep_incoming_addresses         | 192.168.1.11:3306,192.168.1.12:3306,192.168.1.13:3306 |
| wsrep_cluster_weight             | 3                                                     |
| wsrep_cluster_status             | Primary                                               |
| wsrep_connected                  | ON                                                    |
| wsrep_ready                      | ON                                                    |
mysql> grant all on db1.* to dbuser1@'%' identified by 'TEST2021@guodong.com';
Query OK, 0 rows affected, 1 warning (0.48 sec)

mysql> show grants for dbuser1;
+--------------------------------------------------+
| Grants for dbuser1@%                             |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'dbuser1'@'%'              |
| GRANT ALL PRIVILEGES ON `db1`.* TO 'dbuser1'@'%' |
+--------------------------------------------------+
2 rows in set (0.00 sec)
[root@node10 ~]# mysql -h192.168.1.11 -udbuser1 -pTEST2021@guodong.com
mysql> create database db1 default charset utf8mb4;
Query OK, 1 row affected (0.57 sec)

mysql> create table db1.students(id int primary key auto_increment, name varchar(20));
Query OK, 0 rows affected (0.92 sec)

mysql> insert into db1.students(name) values ('tom');
Query OK, 1 row affected (0.15 sec)
[root@node10 ~]# mysql -h192.168.1.12 -udbuser1 -pTEST2021@guodong.com
mysql> select * from db1.students;
+----+------+
| id | name |
+----+------+
|  1 | tom  |
+----+------+
1 row in set (0.00 sec)

[root@node10 ~]# mysql -h192.168.1.13 -udbuser1 -pTEST2021@guodong.com
mysql> select * from db1.students;
+----+------+
| id | name |
+----+------+
|  1 | tom  |
+----+------+
1 row in set (0.00 sec)
测试故障自动恢复
[root@mysql1 ~]# systemctl stop mysql@bootstrap.service
[root@mysql2 ~]# mysql -uroot -pTEST2021@guodong.com
mysql> insert into db1.students(name) values('jerry');
Query OK, 1 row affected (0.20 sec)

mysql> select * from db1.students;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  5 | jerry |
+----+-------+
2 rows in set (0.00 sec)
[root@mysql1 ~]# mysql -uroot -pTEST2021@guodong.com
mysql> select * from db1.students;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  5 | jerry |
+----+-------+
2 rows in set (0.00 sec)
重新启动全部关闭后的mysql
# 查看所有的mysql服务器,找到具有最高seqno的节点,把safe_to_boostrap的值改为1
[root@mysql3 ~]# cat /var/lib/mysql/grastate.dat 
# GALERA saved state
version: 2.1
uuid:    a3b378ea-ce50-11eb-b624-0780367f3ddd
seqno:   7
safe_to_bootstrap: 1
[root@mysql3 ~]# systemctl start mysql@bootstrap.service

# 启动其他主机的mysql服务
[root@mysql1 ~]# systemctl start mysql
[root@mysql2 ~]# systemctl start mysql
上一篇下一篇

猜你喜欢

热点阅读