搭建MariaDB Galera Cluster集群 10.3.8
單機安裝與配置(yum)
根據官網,從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
配置MariaDb鏡像
使用國內鏡像
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
配置代理
永久配置yum代理
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
安裝MariaDB
yum install MariaDB-server MariaDB-client -y
配置MariaDB
service mariadb start
mysql_secure_installation
按提示配置
完全刪除MariaDB數據庫(重新設置配置)
這里記錄如何重置數據庫,之前搭建有時配置出錯等問題,需要推倒重來
- 關閉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
- 卸載MariaDB
yum -y remove Maria*
- 刪除數據內容
rm -rf /var/lib/mysql/* rm -rf /etc/my.cnf.d/ rm -rf /etc/my.cnf
Gelera集群搭建
網絡配置准備
修改/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
Galera配置(3台主機)
修改/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的TCP負載均衡配置
nginx做為入口點,將請求輪詢轉發給任一數據庫。
機器IP:132.xx.xx.100:3399
安裝Nginx
yum install nginx -y
nginx配置
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 | +---------------+-------+
轉自:
作者:gdxieyue
鏈接:https://www.jianshu.com/p/a11bbca92004