1. 程式人生 > >MYSQL雙主高可用方案部署實例

MYSQL雙主高可用方案部署實例

tro dmi admin route pts service firewall oop sql

king01與king02互為master-slave

[root@king01 ~]# mysql -uroot -pabcd.1234

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.130.202

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000004

Read_Master_Log_Pos: 120

Relay_Log_File: relay-bin.000012

Relay_Log_Pos: 283

Relay_Master_Log_File: mysql-bin.000004

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

。。。。。。。


[root@king02 ~]# mysql -uroot -pabcd.1234

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.201

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000009

Read_Master_Log_Pos: 120

Relay_Log_File: relay-bin.000013

Relay_Log_Pos: 283

Relay_Master_Log_File: mysql-bin.000009

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

。。。。。。。


king01安裝keepalived

[root@king01 ~]# yum install -y libnl libnl-devel

[root@king01 ~]# cd /usr/local/src

[root@king01 src]# tar zxvf keepalived-1.2.19.tar.gz

[root@king01 src]# cd keepalived-1.2.19

[root@king01 keepalived-1.2.19]# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64

[root@king01 keepalived-1.2.19]# make && make install

[root@king01 keepalived-1.2.19]# ln -s /usr/local/sbin/keepalived /sbin/

[root@king01 ~]# chkconfig --add keepalived

[root@king01 ~]# chkconfig --level 35 keepalived on

[root@king01 ~]# chkconfig --list |grep keepalived

keepalived 0:off 1:off 2:off 3:on 4:off 5:on 6:off


[root@king01 ~]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.15

smtp_connect_timeout 30

router_id king01

}

vrrp_script check_mysql {

script "/usr/local/mysql/scripts/check_mysql.sh"

interval 2

}

vrrp_instance v01 {

state BACKUP

interface eth0

virtual_router_id 200

priority 100

advert_int 1

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.1.200

}

track_script {

check_mysql

}

}


[root@king01 ~]# vi /usr/local/mysql/scripts/check_mysql.sh

#!/bin/bash

MYSQL=/usr/local/mysql/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

MYSQL_PASSWORD=abcd.1234

$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1

if [ $? == 0 ]

then

echo " $host mysql login successfully "

exit 0

else

echo " $host mysql login faild"

exit 1

fi


[root@king01 ~]# chmod a+x /usr/local/mysql/scripts/check_mysql.sh


[root@king01 ~]# service keepalived start

Starting keepalived: [ OK ]


[root@king01 ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 08:00:27:2b:d6:17 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.201/24 brd 192.168.130.255 scope global eth0

inet 192.168.1.200/32 scope global eth0

inet6 fe80::a00:27ff:fe2b:d617/64 scope link

valid_lft forever preferred_lft forever


[root@king01 ~]# tail -n 100 /var/log/messages

Feb 23 05:09:08 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Transition to MASTER STATE

Feb 23 05:09:09 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Entering MASTER STATE

Feb 23 05:09:09 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) setting protocol VIPs.

Feb 23 05:09:09 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Sending gratuitous ARPs on eth0 for 192.168.1.200

Feb 23 05:09:09 king01 Keepalived_healthcheckers[11670]: Netlink reflector reports IP 192.168.1.200 added

Feb 23 05:09:14 king01 Keepalived_vrrp[11671]: VRRP_Instance(v01) Sending gratuitous ARPs on eth0 for 192.168.1.200


MYSQL雙主高可用方案部署實例