UP | HOME

CentOS7安装MariaDB-Galera-Cluster

假设3台数据库服务器(db1为主节点,db2,db3为从节点)

安装MariaDB Galera Cluster

$ yum install MariaDB-Galera-server galera socat

初始化数据库(只需要主节点执行)

$ /etc/init.d/mysql start
$ /usr/bin/mysql_secure_installation
$ /etc/init.d/mysql stop

如果安装了MariaDB-server就先删除

$ yum remove MariaDB-server

登陆数据库,设置用于集群同步的用户和密码(只需要主节点执行)

$ mysql -uroot -p123456
  MariaDB[] > GRANT USAGE ON *.* to sst@'%' IDENTIFIED BY '123456';
  MariaDB[] > GRANT ALL PRIVILEGES on *.* to sst@'%';
  MariaDB[] > FLUSH PRIVILEGES;
  MariaDB[] > quit

iptables开放系统端口3306,4444和4567

$ iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 4444 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 4567 -j ACCEPT
$ iptables -F

关闭SELinux

$ vi /etc/selinux/config
    SELINUX=disabled
$ setenforce 0

添加sst同步账号(只需要主节点执行)

GRANT ALL PRIVILEGES ON *.* TO 'sst'@'1.1.1.1' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'sst'@'1.1.1.2' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'sst'@'1.1.1.3' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

配置MariaDB Galera(三台机器同样配置)

$ cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
$ vi /etc/my.cnf.d/wsrep.cnf
    datadir=/var/lib/mysql
    wsrep_provider=/usr/lib64/galera/libgalera_smm.so
    wsrep_cluster_name="my_wsrep_cluster"
    wsrep_cluster_address="gcomm://1.1.1.1,1.1.1.2,1.1.1.3"
    wsrep_node_name=db1         # 在db2和db3上不要忘记修改名字
    wsrep_node_address=1.1.1.1  # 在db2和db3上不要忘记修改地址
    wsrep_sst_method=rsync      # 建议选择xtrabackup或xtrabackup-v2同步方式,xtrabackup方式需要安装percona-xtrabackup
    wsrep_sst_auth=sst:123456

主节点启动

$ /etc/init.d/mysql start --wsrep-new-cluster

从节点启动

$ /etc/init.d/mysql start

查看Galera状态

$ mysql -uroot -p123456 -e"show status like 'wsrep%';"
  wsrep_local_state_comment | Synced <-- cluster is synced
  wsrep_incoming_addresses  | 1.1.1.1:3306 <-- node db1 is a provider
  wsrep_cluster_size        | 1 <-- cluster consists of 1 node
  wsrep_ready               | ON <-- good :)

InnoDB 相关参数

$ vi /etc/my.cnf.d/wsrep.cnf
    innodb_autoinc_lock_mode=2
$ vi /etc/my.cnf.d/wsrep.cnf
    innodb_flush_log_at_trx_commit=2
$ vi /etc/my.cnf.d/wsrep.cnf
    sync_binlog=100

判断复制过程是否出现问题

$ mysql -uroot -p123456 -e"show status like 'wsrep%';"
  wsrep_flow_control_paused

找出慢节点

$ mysql -uroot -p123456 -e"show status like 'wsrep%';"
  wsrep_flow_control_sent
  wsrep_local_recv_queue_avg

最后,任意数据库节点宕机(不论主从节点)都以从节点方式重启即可。

Date: 2019-01-17 Thu 10:40

Author: shixiongfei

Created: 2020-09-05 Sat 14:40

Emacs 27.1 (Org mode 9.3)