purpose:
After a hang Mysql, automatically connected to another identical database
Media Download
1.mysql5.6 https://download.csdn.net/download/qq_35653822/11172509
2.keepalive https://download.csdn.net/download/qq_35653822/11192115
A main master copy
Require three IP
master1
master2
VIP
Vip is the virtual ip which I need to practice is between ping mater1 to master2 just a no to ping the ip (normal to the company's network and staff contact)
1.1 modify the configuration file
1.1.1 modify master1 etc under the my.cnf configuration file
Add [mysqld] at
server-id=1
log-bin=/usr/local/mysql/data/mysql1-bin
relay_log=/usr/local/mysql/data/relay-log
auto-increment-increment = 2
auto-increment-offset = 1
1.1.2 modify master2 etc under the my.cnf configuration file
Add [mysqld] at
server-id=2
log-bin=/usr/local/mysql/data/mysql2-bin
relay_log=/usr/local/mysql/data/relay-log
auto-increment-increment = 2
auto-increment-offset = 2
After completion of the configuration file to change the restart mysql
1)service mysql stop
2)service mysql start
1.2 to the other authorized
The Lord is copied each other's main
1.2.1 master1 authorized to master2
1) First log in as root
mysql -uroot -ppasswprd
2) Authorization
GRANT ALL PRIVILEGES ON *.* TO 'username'@'ip' IDENTIFIED BY 'password' WITH GRANT OPTION;
or
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO username@ip IDENTIFIED BY 'password';
3)刷新
FLUSH PRIVILEGES;
4) Check the configuration of master1
They are the log name and pos
5) link master1 on master2
If the slave has been opened to execute stop slave
Log name and master1 the top five are the other side of the arrow corresponding to master1 the ip master1 to master2 authorized user name and password master1 of position
6)start slave;
7) Check status
SHOW SLAVE STATUS\G
Success is configured to yes
1.2.2 master2 the same way to empower master1 repeat 1.2.1 master2
This master-master replication is completed
II. Configuring the Network Environment
Editing two machine / etc / sysconfig / network-scripts / ifcfg-eth1 or / etc / sysconfig / network-scripts / ifcfg-eth0
Do not add instructions below #
Configuration is as follows
master1
DEVICE = eth0 # NIC can see the ip addr show appearance is eth0 or eth1
the TYPE = Ethernet
ONBOOT = yes
NM_CONTROLLED = yes
BOOTPROTO = none
IPADDR # = 172.21.1.80 this machine ip
NETMASK = 255.255.255.0
GATEWAY = 172.21.1.1
IPV6INIT NO =
USERCTL = NO
master2
DEVICE = eth0 # NIC can see the ip addr show appearance is eth0 or eth1
the TYPE = Ethernet
ONBOOT = yes
NM_CONTROLLED = yes
BOOTPROTO = none
IPADDR # = 172.21.1.85 this machine ip
NETMASK = 255.255.255.0
GATEWAY = 172.21.1.1
IPV6INIT NO =
USERCTL = NO
III. Installation keepalive
In addition to the two databases ip, ip need keepalive also called virtual vip his role is to monitor the two databases master1 hung up and went Master2 master2 hung up and went master1
Both machines must be installed keepalive
1) extracting tar -zxvf keepalived-1.1.17.tar.gz
2)cd keepalived-1.1.17
3)make && make install
4) a copy of the configuration file and create a configuration file directory to ask
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
5) After master1 the Keepalive configuration file (# explanations do not add to the posted no default)
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] #-mail
}
notification_email_from [email protected] #-mail
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id mysql65 # Two machines can not same
}
vrrp_script check_run {
script "/opt/mysql_check.sh" # mysql hung up after the implementation of the process to kill the script keepalive
interval 1
wegiht 2
}
vrrp_instance VI_1 {
state backup # are both set to from
interface eth1 # gateway via ip addr show
virtual_router_id 80 # unanimously two machines
priority priority 100 #
nopreempt # Two machines are non-preemptive mode
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 # two machines the same password
}
virtual_ipaddress {
172.88.8.86 #VIP
}
track_script {
check_run # monitor function
}
}
virtual_server 172.88.8.86 3306 {# virtual ip and port
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 30
protocol TCP
real_server 127.0.0.1 3306 {# local ip
weight 2
notify_down /opt/mysql_check.sh
TCP_CHECK {
connect_timeout 3 # connection time
nb_get_retry 3 # reconnection times
delay_before_retry 3 # reconnect time interval
connect_port 3306 # port health checks
}
}
}
6) mysql_check.sh opt placed under
/ Opt / mysql / bin / mysql path is Mysql
#!/bin/bash
#MYSQL_HOST=172.21.1.229
#MYSQL_USER=root
#MYSQL_PASSWORD=123456
/opt/mysql/bin/mysql -h 127.0.0.1 -u root -p123456 -e "show status;" >/dev/null 2>&1
#$mysqlclient --host=$host --port=$port --user=$user --password=$password -e "show databases;" > /dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully "
exit 0
else
#echo " $host mysql login faild"
/etc/init.d/keepalived stop
exit 2
be
IV. Commitment keepalive
1) Start keepalived
service keepalived start
2) Stop keepalived
service keepalived stop
V. add mysql and keepalived to boot from Kai
1)chkconfig --add mysqld
2)chkconfig --level 345 mysqld on
3)chkconfig --add keepalived
4)chkconfig --level 345 keepalived on
VI. Verification
1) add tables, data and other operations on a database, another database have the same effect
2) the implementation of VIP observe whether ip addr show also appears in two servers appear if only one server authentication is successful or failed
3) perform service mysqld stop stop Mysql service execution service keepalived status to see if keepalived is operating normally stop running if the failure is still running
4) After turning off mysql or keepalived, by Mysql navicat vip access, and you can not link it failed
After 5) stopped a mysql or keepalive, perform the ip addr show to see vip in which server, then shut down the server starts, if there is no drift back vip is successful, if it fails drift back
6) Check whether to add to the boot from Kai
chkconfig --list mysqld
chkconfig --list keepalived