一、MySQL安装配置
Server1和Server2安装MySQL,安装版本保持一致,包括MySQL用户UID、安装目录、数据文件目录都要完成相同。
[root@Server1 ~]# id mysql
uid=498(mysql) gid=500(mysql) groups=500(mysql)
[root@Server1 ~]# ls /usr/local/mysql/
bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
[root@Server2 ~]# id mysql
uid=498(mysql) gid=500(mysql) groups=500(mysql)
[root@Server2 ~]# ls /usr/local/mysql/
bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files 只需在主节点执行安装系统数据库的脚本$basedir/scripts/mysql_install_db
[root@Server1 ~]# mount /dev/sdb1 /data/
[root@Server1 ~]# cd /usr/local/mysql/scripts/
[root@Server1 scripts]# ./mysql_install_db --user=mysql --data=/data/mysql/mysql --basedir=/usr/local/mysql --no-defaults
5、启动并测试keepalived集群资源转换功能
启动Server1的keepalived服务,查看keepalived的日志
Jan 22 16:14:36 Server1 Keepalived[21561]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 22 16:14:36 Server1 Keepalived[21562]: Starting Healthcheck child process, pid=21563
Jan 22 16:14:36 Server1 Keepalived[21562]: Starting VRRP child process, pid=21564
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 10.17.83.174 added
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 192.168.6.174 added
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP fe80::250:56ff:feb5:725f added
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP fe80::250:56ff:feb5:5a15 added
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Registering Kernel netlink reflector
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Registering Kernel netlink command channel
Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP 10.17.83.174 added
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP 192.168.6.174 added
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP fe80::250:56ff:feb5:725f added
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP fe80::250:56ff:feb5:5a15 added
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Registering Kernel netlink reflector
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Registering Kernel netlink command channel
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Registering gratuitous ARP shared channel
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Truncating auth_pass to 8 characters
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) removing protocol VIPs.
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: Sync group MySQL_HA_G1 has only 1 virtual router(s) - removing
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: Using LinkWatch kernel netlink reflector...
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Entering BACKUP STATE
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jan 22 16:14:51 Server1 Keepalived_healthcheckers[21563]: Using LinkWatch kernel netlink reflector...
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: pid 21567 exited with status 1
Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP_Script(chage_status) succeeded
Jan 22 16:14:57 Server1 Keepalived_vrrp[21564]: VRRP_Script(chk_mysql) succeeded
Jan 22 16:14:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Transition to MASTER STATE
Jan 22 16:14:59 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 100 to 120
Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Entering MASTER STATE
Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) setting protocol VIPs.
Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:15:00 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 192.168.6.176 added
Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.6.176
Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: Sending gratuitous ARP on eth1 for 192.168.6.176
然后再启动Server2的keepalived服务,查看keepalived日志
Jan 22 16:14:57 localhost Keepalived[26882]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 22 16:14:57 localhost Keepalived[26882]: daemon is already running
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) removing protocol VIPs.
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: Sync group MySQL_HA_G1 has only 1 virtual router(s) - removing
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: Using LinkWatch kernel netlink reflector...
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Entering BACKUP STATE
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jan 22 16:15:01 localhost Keepalived_healthcheckers[26874]: Using LinkWatch kernel netlink reflector...
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP_Script(chage_status) succeeded
Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: pid 26885 exited with status 1
Jan 22 16:15:07 localhost Keepalived_vrrp[26875]: VRRP_Script(chk_mysql) succeeded
Jan 22 16:15:09 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 90 to 110
查看Server2的资源和服务状态
# 是否挂载了存储
[root@Server2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 47G 2.5G 43G 6% /
tmpfs tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 ext4 190M 35M 145M 20% /boot
#是否有VIP
[root@Server2 ~]# ip addr | grep eth1
3: eth1: mtu 1500 qdisc mq state UP qlen 1000
inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1
#是否有MySQL服务
[root@Server2 ~]# ps -ef | grep mysql
root 27652 1807 0 16:24 pts/1 00:00:00 grep mysql
[root@Server2 ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
三、测试keepalived自动转移MySQL服务 1、Server1上停止MySQL服务,查看Server2是否接管MySQL服务
查看Server1的keepalived日志
Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Script(chk_mysql) failed
Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 120 to 100
Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Received advert with higher priority 110, ours 100
Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Entering BACKUP STATE
Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) removing protocol VIPs.
Jan 22 16:29:58 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 192.168.6.176 removed
Jan 22 16:30:04 Server1 Keepalived_vrrp[21564]: pid 23227 exited with status 1
Jan 22 16:30:10 Server1 Keepalived_vrrp[21564]: pid 23233 exited with status 1
Jan 22 16:30:16 Server1 Keepalived_vrrp[21564]: VRRP_Script(chk_mysql) succeeded
Jan 22 16:30:16 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 100 to 120
查看Server2的keepalived日志
Jan 22 16:15:09 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 90 to 110
Jan 22 16:17:01 localhost Keepalived_vrrp[26875]: pid 27059 exited with status 126
Jan 22 16:29:57 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) forcing a new MASTER election
Jan 22 16:29:59 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Transition to MASTER STATE
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Entering MASTER STATE
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) setting protocol VIPs.
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:30:01 localhost Keepalived_healthcheckers[26874]: Netlink reflector reports IP 192.168.6.176 added
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.6.176
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:30:01 localhost kernel: EXT4-fs (sdb1): warning: mounting fs with errors, running e2fsck is recommended
Jan 22 16:30:01 localhost kernel: EXT4-fs (sdb1): mounted filesystem with ordered data mode. Opts:
Jan 22 16:30:06 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
Jan 22 16:30:06 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.6.176
Jan 22 16:30:06 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
2、查看Server2是否正常接管MySQL服务
Server2
[root@Server2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 47G 2.5G 43G 6% /
tmpfs tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 ext4 190M 35M 145M 20% /boot
/dev/sdb1 ext4 50G 263M 47G 1% /data
[root@Server2 ~]# ip a | grep eth1
3: eth1: mtu 1500 qdisc mq state UP qlen 1000
inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1
inet 192.168.6.176/24 scope global secondary eth1
[root@Server2 ~]# ps -ef | grep mysql
root 28124 1 0 16:30 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql --pid-file=/data/mysql/mysql/Server2.pid
mysql 28327 28124 0 16:30 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql/Server2.err --pid-file=/data/mysql/mysql/Server2.pid --socket=/tmp/mysql.sock --port=3306
root 28685 1807 0 16:33 pts/1 00:00:00 grep mysql Server1
[root@Server1 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 47G 2.5G 43G 6% /
tmpfs tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 ext4 190M 35M 145M 20% /boot
[root@Server1 ~]# ip addr | grep eth1
3: eth1: mtu 1500 qdisc mq state UP qlen 1000
inet 192.168.6.174/24 brd 192.168.6.255 scope global eth1
[root@Server1 ~]# ps -ef | grep mysql
root 23617 25788 0 16:34 pts/1 00:00:00 grep mysql
3、再测试Server2异常时,是否将服务转移至Server1上
[root@Server2 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! Server1