LinuxServicesIaaSOpenStack-Pike(2.搭建高可用的MySQL数据库)

Posted 炼狱腾蛇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LinuxServicesIaaSOpenStack-Pike(2.搭建高可用的MySQL数据库)相关的知识,希望对你有一定的参考价值。

1. 简介

1.1. 官方文档:

数据库架构:https://docs.openstack.org/ha-guide/shared-database.html

1.2. 本次使用的的是Percona XtraDB Cluster,5.7版本

https://www.percona.com/doc/percona-xtradb-cluster/5.7/index.html

1.3. 安装文件下载:

https://www.percona.com/downloads/Percona-XtraDB-Cluster-57/LATEST/

或者使用yum安装,本文中使用yum安装

https://www.percona.com/doc/percona-repo-config/yum-repo.html

这还有Docker的安装说明,等以后Docker文档中再详细说明

https://www.percona.com/doc/percona-xtradb-cluster/5.7/install/docker.html

2. 安装

 

功能与组件 机器名 服务IP 管理IP Processor Cores RAM Storage 备注
mysql PXC HCTJOSMYSQL01 10.30.2.25 172.16.0.25        
Mysql PXC HCTJOSMYSQL02 10.30.2.26 172.16.0.26        
Mysql PXC HCTJOSMYSQL03 10.30.2.27 172.16.0.27        

 

2.1. 安装yum源

yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm

在RHEL7.4上虽然已经配置了EPEL源,但是仍然会出现找不到libev.so.4()的错误,请使用下面地址直接安装

https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libev-4.15-3.el7.x86_64.rpm

安装软件吧

yum install Percona-XtraDB-Cluster-57

先别着急启动,配置一个独立的分区用来存储数据库文件

vgcreate vg_mysql /dev/sdb
lvcreate -n lv_mysql -l +100%FREE vg_mysql
mkfs.xfs /dev/mapper/vg_mysql-lv_mysql

写进/etc/fstab

#Disk for mysql data
/dev/mapper/vg_mysql-lv_mysql    /data_mysql        xfs    defaults    0 0

重新挂载,在分区内建立两个文件夹,mysql和backup

mkdir -pv /data_mysql
mount -a
mkdir -pv /data_mysql/{mysql,backup}

修改/etc/percona-xtradb-cluster.conf.d/mysqld.cnf

datadir=/data_mysql/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 [email protected]"%" 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

集群的配置文档在这:https://www.percona.com/doc/percona-xtradb-cluster/5.7/configure.html#configure

 首先修改/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://hctjosmysql01.hccos.cn,hctjosmysql02.hccos.cn,hctjosmysql03.hccos.cn # 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=hctjoscontroller01.homecredit.cn # Cluster name wsrep_cluster_name=pxc-cluster-openstack #If wsrep_node_name is not specified, then system hostname will be used wsrep_node_name=pxc-cluster-openstack-node-1 #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 [email protected]

在其他节点上修改/etc/percona-xtradb-cluster.conf.d/wsrep.cnf

wsrep_node_name=pxc-cluster-openstack-node-2
wsrep_node_address=hctjoscontroller02.homecredit.cn
wsrep_node_name=pxc-cluster-openstack-node-3
wsrep_node_address=hctjoscontroller03.homecredit.cn

 在其他结点上修改/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‘;
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)

 注意:如果cluster已经启动,主节点down机想重新加入这个集群,直接启动mysql即可

systemctl start mysql

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

systemctl start [email protected]





以上是关于LinuxServicesIaaSOpenStack-Pike(2.搭建高可用的MySQL数据库)的主要内容,如果未能解决你的问题,请参考以下文章