在CentOS上实现MySQL的高可用性,可以采用多种方案。以下是一些常见的方案:
MySQL的主从复制是一种常见的高可用性解决方案,通过将数据从一个MySQL服务器(主服务器)复制到一个或多个MySQL服务器(从服务器),可以实现读写分离和故障转移。
配置主服务器:
/etc/my.cnf或/etc/mysql/my.cnf文件,添加以下配置:[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = your_database_name
systemctl restart mysqld
CREATE USER 'replicator'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';
FLUSH PRIVILEGES;
SHOW MASTER STATUS;
配置从服务器:
/etc/my.cnf或/etc/mysql/my.cnf文件,添加以下配置:[mysqld]
server-id = 2
relay_log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
replicate_do_db = your_database_name
systemctl restart mysqld
CHANGE MASTER TO
MASTER_HOST='master_ip',
MASTER_USER='replicator',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=107;
START SLAVE;
MySQL Group Replication是MySQL 5.7及以上版本提供的一种高可用性和复制解决方案,支持多主模式和单主模式。
安装MySQL Group Replication插件:
yum install mysql-group-replication-plugin
配置MySQL:
/etc/my.cnf或/etc/mysql/my.cnf文件,添加以下配置:[mysqld]
server-id = 1
gtid_mode = ON
enforce_gtid_consistency = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
transaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeeee"
loose-group_replication_start_on_boot = OFF
loose-group_replication_ssl_mode = REQUIRED
loose-group_replication_recovery_use_ssl = 1
loose-group_replication_local_address = "192.168.1.1:33061"
loose-group_replication_group_seeds = "192.168.1.1:33061,192.168.1.2:33061"
loose-group_replication_bootstrap_group = OFF
启动MySQL服务:
systemctl start mysqld
加入组复制:
SET GLOBAL group_replication_bootstrap_group=OFF;
START GROUP_REPLICATION;
Percona XtraDB Cluster是基于Galera Cluster的高可用性和复制解决方案,支持多主模式。
安装Percona XtraDB Cluster:
yum install percona-xtradb-cluster-57
配置MySQL:
/etc/my.cnf或/etc/mysql/my.cnf文件,添加以下配置:[mysqld]
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.1.1,192.168.1.2,192.168.1.3
wsrep_node_address=192.168.1.1
wsrep_node_name=node1
wsrep_sst_method=xtrabackup-v2
启动MySQL服务:
systemctl start mysqld
加入集群:
SET GLOBAL wsrep_cluster_address="gcomm://192.168.1.1,192.168.1.2,192.168.1.3";
MHA是一个用于自动化MySQL主从切换的工具,可以在主服务器故障时自动将从服务器提升为主服务器。
安装MHA Manager和Node:
yum install mha4mysql-manager mha4mysql-node
配置MHA Manager:
/etc/masterha_default.cnf文件,配置MHA Manager的连接信息。启动MHA Manager:
masterha_manager --conf=/etc/masterha_default.cnf --remove_dead_master_conf --master_workdir=/var/log/masterha/app1
配置MHA Node:
/etc/masterha_default.cnf文件,配置MHA Node的连接信息。启动MHA Node:
masterha_check_ssh --conf=/etc/masterha_default.cnf
masterha_check_repl --conf=/etc/masterha_default.cnf
选择哪种高可用性方案取决于你的具体需求和环境。MySQL Replication是最基础的方案,适合大多数场景。MySQL Group Replication和Percona XtraDB Cluster提供了更高级的功能和更好的性能,但配置和管理相对复杂。MHA则是一个自动化工具,可以简化主从切换的过程。