1、數據mariadb集羣搭建分爲兩個內容
一、負載均衡搭建
二、mariadb galera cluster
2、負載均衡搭建
負載均衡集羣是 load balance 集羣的簡寫,翻譯成中文就是負載均衡集羣。經常使用的負載均衡開源軟件有nginx、lvs、haproxy,商業的硬件負載均衡設備F五、Netscale。這裏主要是學習 LVS 並對其進行了詳細的總結記錄。本次採用lvs的DR模式
2.1 安裝keepalive和lvs安裝包
yum install -y ##### keepalived 安裝keepalive
yum install -y ipvsadm ##### 安裝裝lvs
2.二、編輯keepalive的配置,keepalive與網絡的vrrp原理同樣
vi /etc/keepalive/keepalived.conf
vrrp_instance VI_1 {
state MASTER
! nopreempt
interface chkconfig keepalived on
garp_master_delay 10
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass zabbix
}
virtual_ipaddress {
192.168.1.157
}
}node
virtual_server 192.168.1.157 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCPmysql
real_server 192.168.1.154 3306 { weight 1 TCP_CHECK { connect_port 3306 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.1.155 3306 { weight 1 TCP_CHECK { connect_port 3306 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }
real_server 192.168.1 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}nginx
}
}sql
2.3.開啓keepalive、ipvsam服務
systemctl start keepalived
systemctl start ipvsam
2.四、驗證
ipvsam 查看負載狀況
3、mariadb 集羣
3.1 數據庫安裝
分別在三臺主機安裝mariadb的數據,本次安裝10.3.14版本的數據,較爲穩定的版本
vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB-10.3.14
baseurl=http://yum.mariadb.org/10.3.14/centos7-amd64shell
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1數據庫
[root@localhost /]# yum install mariadb-server -y
Loaded plugins: langpacks, product-id, subscription-manager
Complete!
[root@localhost /]# rpm -qa | grep mariadb
mariadb-server-5.5.50-1.el7_2.x86_64
mariadb-libs-5.5.50-1.el7_2.x86_64
mariadb-5.5.50-1.el7_2.x86_64
mariadb-devel-5.5.50-1.el7_2.x86_64
啓動mariadb服務程序並添加到開機啓動項中:
[root@localhost /]# systemctl start mariadb
[root@localhost /]# systemctl enable mariadb
[root@localhost /]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 22418/mysqld
[root@localhost /]# ps -ef | grep mariadb
mysql 22418 22259 0 00:39 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 22459 1092 0 00:40 pts/0 00:00:00 grep --color=auto mariadb
[root@localhost /]# ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
爲了保證數據庫的安全性,必定要進行初始化工做:
第1步:設定root用戶密碼。
第2步:刪除匿名賬號。
第3步:禁止root用戶從遠程登錄。
第4步:刪除test數據庫並取消對其的訪問權限。
第5步:刷新受權表,讓初始化後的設定當即生效。
初始化數據庫服務程序:
[root@localhost /]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 當前數據庫密碼爲空,直接敲擊回車。
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password: 輸入要爲root用戶設置的數據庫密碼。
Re-enter new password: 重複再輸入一次密碼。
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y(刪除匿名賬號)
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y(禁止root用戶從遠程登錄)
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y(刪除test數據庫並取消對其的訪問權限)bootstrap
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.socentos
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://192.168.1.192.168.1.155,192.168.1.156,"安全
wsrep_sst_method=rsyncbash
wsrep_node_address="192.168.1.156" #####其餘節點須要修改
wsrep_node_name="Node1" #####其餘節點須要修改
第1個節點先啓動 使用galera_new_cluster
其餘節點使用 systemctl start mariadb
3.4 安裝zabbix-server-mysql
爲了將zabbix的數據文件導入mysql中
password
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'password';
mysql> quit;
導入初始架構和數據,系統將提示您輸入新建立的密碼。
5、配置vip
由於負載均衡lvs的DR原理,每臺數據庫都必須配置vip地址,並不對外進行廣播
#!/bin/bash
#description:start realserver
vip1=192.168.1.157
case $1 in
start)
echo "Start Realserver"
/sbin/ifconfig lo:0 $vip1 broadcast $vip1 netmask 255.255.255.255 up
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo "Stop Realserver"
/sbin/ifconfig lo:0 down
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 (start | stop)"
exit 1
esac
最後運行shell腳本
常見故障
搭建galera cluster的時候設置sst爲xtrabackup,啓動node1報錯。[ERROR] WSREP: It may not be safe to bootstrap the cluster from this node. It was not the last one to leave the cluster and may not contain all the updates. To force cluster bootstrap with this node, edit the grastate.dat file manually and set safe_to_bootstrap to 1 .解決方式:grastate.dat file of the node you intend to use as the first node.須要把該文件刪除 從新啓動便可。