一、环境准备
1、各主机配置静态域名解析:
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.210.131 node1 node1.test.com
192.168.210.130 node2 node2.test.com
192.168.210.132 node3 node3.test.com
2、并配置各节点ssh无密钥登陆
3、安装docker环境
wget http://download2.yunwei.edu/shell/docker.tar.gz
tar zxf docker.tar.gz
cd docker
sh docker
4、下载配置文件并且导入镜像
wget http://download2.yunwei.edu/shell/galera.tar.gz
tar zxf galera.tar.gz
docker load -i mariadb-galera.tar
mkdir mkdir /opt/mariadb
#######将config.tar.gz 解压后,mv到mkdir /opt/mariadb,
tar zxf config.tar.gz && mv config /opt/mariadb/
二、配置集群
#######node1
docker run -d --net host --name galera1
-e WSREP_NODE_ADDRESS=192.168.210.131
-e WSREP_CLUSTER_ADDRESS=gcomm://192.168.210.131:4567,192.168.210.130:4567,192.168.210.132:4567
-p 3306:3306
-p 4567:4567/udp
-p 4567-4568:4567-4568
-p 4444:4444
-v /opt/mariadb/config/mysql:/etc/mysql
-v /opt/mariadb/config/data:/var/lib/mysql:Z
--restart=always
panubo/mariadb-galera mysqld
#######node2
docker run -d --net host --name galera2
-e WSREP_NODE_ADDRESS=192.168.210.130
-e WSREP_CLUSTER_ADDRESS=gcomm://192.168.210.131:4567,192.168.210.130:4567,192.168.210.132:4567
-p 3306:3306
-p 4567:4567/udp
-p 4567-4568:4567-4568
-p 4444:4444
-v /opt/mariadb/config/mysql:/etc/mysql
-v /opt/mariadb/config/data:/var/lib/mysql:Z
--restart=always
panubo/mariadb-galera mysqld
#########node3
docker run -d --net host --name galera3
-e WSREP_NODE_ADDRESS=192.168.210.132
-e WSREP_CLUSTER_ADDRESS=gcomm://192.168.210.131:4567,192.168.210.130:4567,192.168.210.132:4567
-e MYSQL_ROOT_PASSWORD=123
-p 3306:3306
-p 4567:4567/udp
-p 4567-4568:4567-4568
-p 4444:4444
-v /opt/mariadb/config/mysql:/etc/mysql
-v /opt/mariadb/config/data:/var/lib/mysql:Z
--restart=always
panubo/mariadb-galera mysqld --wsrep-new-cluster
三、登陆数据库时,安装mariadb客户端
yum install mariadb -y
mysql -h 192.168.210.131 -P 3306 -u root -p123
检查集群状态:
SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
四、故障恢复:
(1)集群中某slave节点down机,重启该节点后,重启docke容器即可;
(2)集群中master节点down机,重启该master节点后,master-docker容器不能正常启动,因为这是master节点已经切换,
需要将原先master-docker容器删除,再以非wrsep方式启动一个普通容器;
(3)集群全部意外down机,强制找一台节点作为master节点;到如下路径,将 safe_to_bootstrap: 0修改为1(平时集群正常时,每个节点都为0)
[root@con3 data]# pwd
/opt/mariadb/data
[root@con3 data]# cat grastate.dat
# GALERA saved state
version: 2.1
uuid: 6a26abec-e7b5-11e8-b15b-b74046986de4
seqno: -1
safe_to_bootstrap: 0
之后在该节点启动wsrep进程的docker