根据官网,从MariaDB 10.1版本开始,版本已自带Galera集群方案插件。
安装环境的操作系统为CentOS 7,openstack的虚拟主机。
百度搜索openstack环境下使用keepalive有些问题,所以直接使用nginx的stream轮询数据库集群。
graph LR
Nginx-->MariaDB-a01
Nginx-->MariaDB-a02
Nginx-->MariaDB-a03
下面从单机版安装开始。
yum install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all;yum makecache
使用国内镜像
vi /etc/yum.repos.d/MariaDB.repo
黏贴下面内容,保存并退出 :wq! ;刷新,yum clean all;yum makecache
[mariadb]
name = MariaDB
baseurl = https://ipv4.mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64
gpgkey=https://ipv4.mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
vi /etc/yum.conf
增加本地代理(本地环境不能访问外网)
proxy=http://10.18.xx.xx:808
export http_proxy=http://10.18.xxx.xxx:808
export https_proxy=http://10.18.xxx.xxx:808
yum install MariaDB-server MariaDB-client -y
service mariadb start
mysql_secure_installation
按提示配置
这里记录如何重置数据库,之前搭建有时配置错等问题
* 关闭mysql
[root@tyrzpt2 etc]# ps -ef|grep mysql
mysql 3548 1 0 Jul26 ? 00:01:26 /usr/sbin/mysqld --wsrep-new-cluster --wsrep_start_position=00000000-0000-0000-0000-000000000000:-1
root 14693 1739 0 11:42 pts/0 00:00:00 grep --color=auto mysql
[root@tyrzpt2 etc]# kill -9 3548
yum -y remove Maria*
rm -rf /var/lib/mysql/*
rm -rf /etc/my.cnf.d/
rm -rf /etc/my.cnf
修改/etc/hosts,增加以下配置
132.xx.xx.11 mariadb-a01
132.xx.xx.18 mariadb-a02
132.xx.xx.35 mariadb-a03
将配置发送给集群其他机器
scp /etc/hosts root@132.xx.xx.18:/etc/hosts
scp /etc/hosts root@132.xx.xx.35:/etc/hosts
修改/etc/my.cnf.d/server.cnf
mariadb-a01机器配置如下,其他主机做相应修改
[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
lower_case_table_names = 1
server_id=6011 #自定义,或者ip地址后两位的组合
[galera]
wsrep_causal_reads=ON
wsrep_provider_options="gcache.size=4G"
wsrep_certify_nonPK=ON
query_cache_size=0
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name=MariaDB-Galera-Cluster #集群名称
wsrep_cluster_address="gcomm://132.xx.xx.11,132.xx.xx.18,132.xx.xx.35" #整个集群的IP地址
wsrep_node_name=mariadb-a01 #hostname,对应前面网路配置/etc/hosts
wsrep_node_address=132.97.54.11 #机器IP地址
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=8
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=2G
wsrep_sst_method=rsync #同步方式
# mariadb-a01第一个启动,命令为
galera_new_cluster
# 分别启动mariadb-a02,mariadb-a03,命令为
service mariadb start
#查看日志
service mariadb status
数据库客户端如navicat,连接mariadb-a01,查看集群状态
show status like ‘wsrep%’;
wsrep_cluster_size=3 表示当前集群数量
mysql -p
MariaDB [(none)]> show status like 'wsrep%';
+------------------------------+-------------------------------------------------------+
| Variable_name | Value |
+------------------------------+-------------------------------------------------------+
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 249 |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_cert_index_size | 62 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 5 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 37040e50-90c1-11e8-8a67-5fb4478cc753 |
| wsrep_cluster_status | Primary |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_connected | ON |
nginx做为入口点,将请求轮询转发给任一数据库。
机器IP:132.xx.xx.100:3399
yum install nginx -y
vi /etc/nginx/nginx.conf
events {
……………………
}
stream {
upstream MariaDB_Cluster {
server mariadb-a01:3306;
server mariadb-a02:3306;
server mariadb-a03:3306;
}
#转发数据库端口
server {
listen 3306;
proxy_pass MariaDB_Cluster;
proxy_connect_timeout 1s; # detect failure quickly
}
}
http {
……………………
}
navicat客户端连接132.xx.xx.100:3399,新建查询,hostname会变化
SHOW VARIABLES WHERE Variable_name = ‘hostname’;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname | a02 |
+---------------+-------+