mariadb galera cluster集群其他节点启动方式
[root@node2 ~]# service mysql start
Starting MySQL....SST in progress, setting sleep higher. SUCCESS!
[root@node3 ~]# service mysql start
Starting MySQL....SST in progress, setting sleep higher. SUCCESS!
#!/bin/bash
a=$1
first=0
while true
do
if [ $# -eq 0 ];then
echo "需要一个参数"
exit 1
fi
if [ $first -eq 0 ];then
mysql -uroot -pmysql -e "CREATE DATABASE IF NOT EXISTS testdb CHARACTER SET utf8;"
mysql -uroot -pmysql testdb -e "CREATE TABLE IF NOT EXISTS students
(StuID INT PRIMARY KEY,Name char(10) NOT NULL,Age TINYINT NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8;"
first=1
fi
mysql -uroot -pmysql testdb -e "INSERT INTO students (StuID,Name,Age) values
($a,'name$a',$a % 100);"
a=$(($a+1))
sleep 1
done
查看数据是否能同步
至此mariadb galera cluster集群就构建完成
授权root用户远程登陆(生产环境中建议使用普通用户,而不是root用户)
MariaDB [test]> grant all on *.* to 'root'@'192.168.43.%' identified by '';
建议每个访问IP单独授权 grant all on *.* to 'root'@'192.168.43.206' identified by 'mysql';
[root@node4 keepalived]# vim check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ]; then
systemctl restart haproxy
echo "Start haproxy" &> /dev/null
sleep 3
if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then
systemctl stop keepalived
echo "Stop keepalived" &> /dev/null
fi
fi
[root@node4 keepalived]# chmod +x check_haproxy.sh
[root@node4 keepalived]# scp ./* node5:/etc/keepalived/
[root@node4 keepalived]# systemctl start keepalived
[root@node4 keepalived]# ip a
2: eno16777736:
inet 192.168.43.204/24 brd 192.168.43.255 scope global eno16777736
inet 192.168.43.50/24 scope global secondary eno16777736:1
[root@node5 ~]# cd /etc/keepalived/
[root@node5 keepalived]# ls
check_haproxy.sh keepalived.conf keepalived.conf.bak
[root@node5 keepalived]# vim keepalived.conf
! Configuration File for keepalived
[root@node5 keepalived]# vim check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ]; then
systemctl start haproxy
echo "Start haproxy" &> /dev/null
sleep 3
if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then
#systemctl stop keepalived
echo "Stop keepalived" &> /dev/null
fi
fi
[root@node5 keepalived]# systemctl start keepalived
[root@node5 keepalived]# ip a
2: eno16777736:
inet 192.168.43.205/24 brd 192.168.43.255 scope global eno16777736
(说明:没有192.168.43.50/24)
三、使用mysql客户端进行连接
[root@node6 ~]# yum -y install mariadb
[root@node6 ~]# mysql -uroot -h192.168.43.50
MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
| tb2 |
+----------------+
2 rows in set (0.01 sec)
MariaDB [test]> select * from tb2;
+----+-------+
| id | name |
+----+-------+
| 2 | hi |
| 5 | hello |
| 8 | Mike |
| 11 | Jack |
| 12 | what |
| 15 | is |
| 18 | that |
+----+-------+
7 rows in set (0.01 sec)
MariaDB [test]> desc tb2;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 6
Current database: test
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(30) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.06 sec)
MariaDB [test]> select * from tb2;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 6
Current database: test
+----+-------+
| id | name |
+----+-------+
| 2 | hi |
| 5 | hello |
| 8 | Mike |
| 11 | Jack |
| 12 | what |
| 15 | is |
| 18 | that |
| 20 | how |
| 23 | do |
| 26 | you |
| 29 | do |
+----+-------+
11 rows in set (0.04 sec)