Linux下Keepalived+MySQL实现高可用(3)

配置完毕后可以查看下keealived有没有检测到本机的MySQL。(启动的时候先启动mysql在启动keepalived,要不mysql没起来启动keepalived会被脚本杀死。)
先看下mysql-ha1(192.168.5.234)
[root@mysql-ha1 ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@mysql-ha1 ~]# /etc/init.d/keepalived start
正在启动 keepalived:
[root@mysql-ha1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.5.55:mysql wrr persistent 50
-> 192.168.5.234:mysql Local 1 0 0

然后再看下mysql-ha1(192.168.5.155)
[root@mysql-ha2 ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@mysql-ha2 ~]# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@mysql-ha2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.5.55:3306 wrr persistent 50
-> 192.168.5.155:3306 Local 1 0 0

测试只开启mysql-ha1(192.168.5.234)mysql服务跟keepalived用客户端链接虚拟ip
[root@mysql-ha1 ~]# ps aux | grep keepalived
root 2977 0.0 0.0 41796 936 ? Ss 23:14 0:00 keepalived -D
root 2978 0.0 0.1 43900 2156 ? S 23:14 0:00 keepalived -D
root 2979 0.0 0.0 43900 1576 ? S 23:14 0:00 keepalived -D
root 2982 0.0 0.0 103240 860 pts/0 S+ 23:16 0:00 grep keepalived
[root@mysql-ha1 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2917/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd
tcp 0 0 :::22 :::* LISTEN 1041/sshd
tcp 0 0 ::1:25 :::* LISTEN 1416/master

mysql-ha2(192.168.5.155)
[root@mysql-ha2 ~]# ps aux | grep keepalived
root 10576 0.0 0.0 5980 756 pts/4 S+ 15:24 0:00 grep keepalived
[root@mysql-ha2 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4776/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1101/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1114/qpidd
tcp 0 0 :::22 :::* LISTEN 4776/sshd
tcp 0 0 ::1:25 :::* LISTEN 1101/master

然后用客户端:192.168.5.10连接
C:\Users\Administrator>mysql -uduyunlong -p123456 -h192.168.5.55
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 5.5.22-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.02 sec)
mysql>

然后把mysql-ha1(192.168.5.234)mysql服务关掉,开启mysql-ha2(192.168.5.155)上的mysql服务跟keepalived。然后不退出mysql,继续查看数据库:
mysql-ha1(192.168.5.234)
[root@mysql-ha1 ~]# service mysqld stop
Shutting down MySQL... SUCCESS!

mysql-ha2(192.168.5.155)
12345 [root@mysql-ha2 ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@mysql-ha2 ~]# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@mysql-ha2 ~]#

客户端:192.168.5.10
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: *** NONE ***
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql>

切换很快,大概在2到3秒之间!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/4bd5d33826336f63eb070fc964142eee.html