在CentOS上搭建MySQL集群可以采用多种高可用架构,如MMM(Master-Master Replication)、MHA(Master High Availability)、Galera Cluster、InnoDB Cluster等。以下是使用Galera Cluster和MySQL Router实现MySQL高可用集群的步骤:
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/selinux=enforcing/selinux=disabled/g' /etc/selinux/config
setenforce 0
reboot
galera-3-25.3.25-2.el7.x86_64.rpm
、mysql-wsrep-5.7-5.7.24-25.16.el7.x86_64.rpm
等。在所有节点上安装MySQL服务器:
sudo yum install mysql-server
配置主服务器:
/etc/my.cnf
文件,添加以下内容:[mysqld]
server-id=1
log_bin=/var/log/mysql/mysql-bin.log
binlog_do_db=example_database
sudo systemctl restart mysqld
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
SHOW MASTER STATUS;
配置从服务器:
/etc/my.cnf
文件,添加以下内容:[mysqld]
server-id=2
relay-log=/var/log/mysql/mysql-relay-bin.log
read-only=1
sudo systemctl restart mysqld
CHANGE MASTER TO
MASTER_HOST='master_ip',
MASTER_USER='replication',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=1641;
START SLAVE;
SHOW SLAVE STATUS\G;
sudo yum install galera
mysql -u root -p
CREATE INSTANCE --cluster="galera_cluster" node_address='192.168.1.100' port=4444;
mysqlcluster join --cluster="galera_cluster" node_address='192.168.1.101' port=4444;
mysqlcluster join --cluster="galera_cluster" node_address='192.168.1.102' port=4444;
wget https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-8.0.23-el7-x86_64.tar.gz
tar -zxvf mysql-router-8.0.23-el7-x86_64.tar.gz
mv mysql-router-8.0.23-el7-x86_64 mysql-router-8.0
export PATH=$PATH:/opt/apps/mysql-router-8.0/bin/
mysqlrouter.cnf
:[routing:primary]
bind_address = 0.0.0.0
bind_port = 7001
max_connections = 1024
available_masters = 192.168.1.100:3306,192.168.1.101:3306,192.168.1.102:3306
routing_strategy = first-available
[routing:secondary]
bind_address = 0.0.0.0
bind_port = 7002
max_connections = 1024
available_masters = 192.168.1.100:3306,192.168.1.101:3306,192.168.1.102:3306
routing_strategy = first-available
mysqld --defaults-file=/opt/apps/mysql-router-8.0/etc/mysqlrouter.cnf
通过MySQL Router连接到集群,并进行读写测试,确保数据同步正常。
以上步骤提供了在CentOS上搭建MySQL高可用集群的基本流程,具体配置可能需要根据实际情况进行调整。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 怎样优化centos的存储空间