CentOS 7.4部署PXC 5.7

为什么80%的码农都做不了架构师?>>>   hot3.png

Percona XtraDB Cluster(下文简称PXC集群)提供了MySQL高可用的一种实现方法。PXC集群以节点组成(推荐至少3节点,便于故障恢复,后面会讨论两节点的情况),每个节点都是基于常规的 MySQL/Percona Server,意味着你可以从集群中分离出某节点单独使用。集群中每个节点都包含完整的数据。 PXC集群主要由两部分组成:Percona Server with XtraDB和Write Set Replication patches(使用了Galera library,一个通用的用于事务型应用的同步、多主复制插件)。

PXC的特性和优点:

  • 支持多主复制
  • 支持并行复制
  • 读写强一致性
  • 作为高可用方案,相比其他方案其结构和实施相对简单明了

PXC的局限和劣势:

  • 由于PXC集群内部一致性控制的机制,事务有可能被终止,原因如下:集群允许在两个节点上通知执行操作同一行的两个事务,但是只有一个能执行成功,另一个 会被终止,同时集群会给被终止的客户端返回死锁错误(Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)).
  • 写入效率取决于节点中最弱的一台,因为PXC集群采用的是强一致性原则,一个更改操作在所有节点都成功才算执行成功。
  • 所有表都要设置主键。
  • 不支持LOCK TABLE等显式锁操作。
  • 集群吞吐量/性能取决于短板。

PXC会使用大概是4个端口号

  • 3306 数据库对外服务的端口号
  • 4444 请求SST SST: 指数据一个镜象传输 xtrabackup , rsync ,mysqldump
  • 4567 : 组成员之间进行沟通的一个端口号
  • 4568 : 传输IST用的。相对于SST来说的一个增量。

环境信息

操作系统:CentOS Linux release 7.5.1804 (Core)

主机信息

172.16.2.115 dbs01 172.16.2.116 dbs02 172.16.2.117 dbs03

操作步骤

1、关闭selinux以及防火墙,实验环境和线上环境的数据库一般都是内网,所以关掉,如果必须要打开firewalld的话,要开放上面提到的几个端口

# sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
# setenforce 0
# systemctl stop firewalld
# systemctl disable firewalld

如果不关闭防火墙,需要把端口开放

# firewall-cmd --add-port=3306/tcp --permanent
# firewall-cmd --add-port=4444/tcp --permanent
# firewall-cmd --add-port=4567/tcp --permanent
# firewall-cmd --add-port=4568/tcp --permanent
# firewall-cmd --reload

2、安装percona源以及PXC,安装前要确保本机上没有安装其他版本的MySQL

# mv /etc/my.cnf /etc/my.cnf-bak
# yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
# yum -y install Percona-XtraDB-Cluster-57

3、启动服务,并设置root密码

# systemctl start mysql

# 获取初始密码
# grep 'temporary password' /var/log/mysqld.log

# 登陆mysql用户修改root密码
mysql> alter user root@localhost identified by 'ZJ74fj39pR';
mysql> quit

以上步骤在每个节点都要操作

第一个节点(172.16.2.115),修改配置文件/etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://172.16.2.115,172.16.2.116,172.16.2.117
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# Slave thread to use
wsrep_slave_threads= 8
wsrep_log_conflicts
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node IP address
wsrep_node_address=172.16.2.115
# Cluster name
wsrep_cluster_name=pxc-cluster
#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=dbs01
#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING
# SST method
wsrep_sst_method=xtrabackup-v2
#Authentication for SST method
wsrep_sst_auth="sstuser:s3cretPass"

其中要注意的有以下几点:

  1. wsrep_cluster_address=gcomm://172.16.2.115,172.16.2.116,172.16.2.117这里要填上我们的集群中的IP地址
  2. wsrep_node_address=172.16.2.115修改IP地址
  3. wsrep_cluster_name=pxc-cluster定义集群的名称,可以不作修改
  4. wsrep_node_name=dbs01修改主机名
  5. wsrep_sst_auth="sstuser:s3cretPass"用于数据同步,打开注释并在数据库建立该用户
  6. 有必要可以在配置文件加上下面参数打开调试模式:wsrep_provider_options = "debug=1;gcache.size=1G"

启动集群第一个节点

# systemctl start [email protected]

登陆mysql用户,并开启wsrep_causal_reads

mysql> set wsrep_causal_reads=1;

创建用户并授权

mysql> CREATE USER 'sstuser'@'172.16.%' IDENTIFIED BY 's3cretPass';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'sstuser'@'172.16.%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

查看集群相关信息

# 查看wsrep的相关参数
mysql> show status like 'wsrep%';

# 查看集群的成员数
mysql> show status like 'wsrep_cluster_size';

第二个节点(172.16.2.116),修改配置文件/etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://172.16.2.115,172.16.2.116
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# Slave thread to use
wsrep_slave_threads= 8
wsrep_log_conflicts
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node IP address
wsrep_node_address=172.16.2.116
# Cluster name
wsrep_cluster_name=pxc-cluster
#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=dbs02
#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING
# SST method
#wsrep_sst_method=rsync
wsrep_sst_method=xtrabackup-v2
#Authentication for SST method
wsrep_sst_auth="sstuser:s3cretPass"
wsrep_provider_options = "debug=1;gcache.size=1G"
  • 这里的wsrep_cluster_address=gcomm://172.16.2.115,172.16.2.116不能把117写上,因为117并未启动,在把172.16.2.117启动后,可再写进配置文件
  • wsrep_sst_method=xtrabackup-v2这里第二个节点用xtrabackup-v2需要检查下面两个包是否已经安装了,我在实验环境中死活启动不了,后来改成rsync方法就可以启动,再后来又改回xtrabackup-v2又没报错了
rpm -qa | grep percona-xtrabackup
rpm -qa | grep perl-Time-HiRes

登陆root用户添加sst用户并重启数据库

mysql> CREATE USER 'sstuser'@'172.16.%' IDENTIFIED BY 's3cretPass';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'sstuser'@'172.16.%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit

# 重启数据库,这里不用mysql@bootstrap,后面的节点都只启动mysql服务
# systemctl restart mysql

第三个节点(172.16.2.117),修改配置文件/etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://172.16.2.115,172.16.2.116,172.16.2.117
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# Slave thread to use
wsrep_slave_threads= 8
wsrep_log_conflicts
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node IP address
wsrep_node_address=172.16.2.117
# Cluster name
wsrep_cluster_name=pxc-cluster
#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=dbs03
#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING
# SST method
#wsrep_sst_method=rsync
wsrep_sst_method=xtrabackup-v2
#Authentication for SST method
wsrep_sst_auth="sstuser:s3cretPass"
wsrep_provider_options = "debug=1;gcache.size=1G"

和第二个节点116一样,创建授权sst用户,重启mysql服务。

当所有节点都加入后,在第一个节点115上重启服务

systemctl stop mysql@bootstrap
systemctl start mysql

如果后续还需要加入第四个节点,像上面一样修改配置文件:/etc/percona-xtradb-cluster.conf.d/wsrep.cnf,把所有的IP地址都写在wsrep_cluster_address参数后面,还有wsrep_node_address和wsrep_node_name,再到数据库创建sst用户重启即可,其他节点都不需要任何改动。

转载于:https://my.oschina.net/ldhqy/blog/2354331

你可能感兴趣的:(CentOS 7.4部署PXC 5.7)