以下是在CentOS上搭建MySQL集群的步骤:
systemctl stop firewalld
,systemctl disable firewalld
,sed -i 's/selinuxenforcing/selinuxdisabled/g' /etc/selinux/config
,setenforce 0
,然后重启。在所有节点上执行:sudo yum install mysql-server -y
。
编辑/etc/my.cnf
,添加:
[mysqld]
server-id=1
log-bin=/var/log/mysql/mysql-bin.log
binlog_format=ROW
gtid_mode=ON
enforce_gtid_consistency=ON
重启MySQL服务:sudo systemctl restart mysqld
。
编辑/etc/my.cnf
,添加:
[mysqld]
server-id=2
relay-log=/var/log/mysql/mysql-relay-bin.log
read-only=1
重启MySQL服务:sudo systemctl restart mysqld
。
在主节点上创建复制用户:CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; FLUSH PRIVILEGES;
。
在从节点上执行:CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107; START SLAVE;
。
安装Galera:sudo yum install galera
。
在每个节点上创建/etc/my.cnf.d/galera.cnf
,添加:
[mysqld]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="my_galera_cluster"
wsrep_cluster_address="gcomm://node1,node2,node3"
wsrep_sst_method=rsync
在每个节点上启动MySQL服务:sudo systemctl start mysqld
。
在任意节点上执行:mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
,若输出结果为节点数,则集群搭建成功。