【MySQL】使用Percona搭建高可用的MySQL数据库

1. 简介

  • Percona是一个公司的名称,主要做MySQL的二次开发
  • 他的集群解决方案叫Percona XtraDB Cluster,简称PXC

2. 架构图

  • 虚线是模拟右侧proxy宕机的情景,红色的虚IP会飘到左边的proxy上
  • application一律使用域名访问数据库,由DNS服务器解析到红黄两个虚拟IP上
  • 由于三个数据库都是可读写的,所以代理到哪个服务器都可以


    image.png

3. 链接

4. 版本

  • PXC:5.7
  • CentOS:7.6

5. Percona安装与配置

  • 配置yum源
yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm
  • 安装
yum -y install Percona-XtraDB-Cluster-57
  • 修改配置文件
# 这是为了把数据文件独立出来,过程就不演示了
sed -i 's$datadir=/var/lib/mysql$datadir=/data/mysql$g' /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 

如果修改数据目录,记得给权限

chown mysql:mysql /data/mysql
  • 起来试试吧,看看密码是啥
systemctl start mysql # 数据库不建议开机启动
grep 'temporary password' /var/log/mysqld.log
  • 用刚才的密码连接数据库
mysql -u root -p
  • 为了将来管理方便,添加了远程的访问权限
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "mysql";
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
  • SST用户也要添加,否则其他结点无法正常连入主节点
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'mysql';
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.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
  • 服务停掉,准备配置集群吧
systemctl stop mysql
  • 首先修改/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
#这里可以写域名或者IP地址
wsrep_cluster_address=gcomm://10.210.149.25,10.210.149.26,10.210.149.27

# 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=10.210.149.25
# Cluster name
wsrep_cluster_name=pxc-cluster

#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=pxc-cluster-node-10-210-149-25

#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:mysql"
  • 启动主节点
systemctl start mysql@bootstrap.service
  • 在其他节点上修改/etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  • 在其他结点上修改/etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=2
[mysqld]
server-id=3
  • 在其他上启动mysql
systemctl start mysql
  • 随便找一个节点检查一下
mysql> show status like 'wsrep_cluster_size';
  • 注意:注意:如果cluster已经启动,主节点down机想重新加入这个集群,直接启动mysql即可
systemctl start mysql

如果这个集群中最后的节点也down机了,在任何一个节点都可以启动集群,重新启动集群需要执行

systemctl start mysql@bootstrap.service

6 Proxy安装与配置

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容