环境描述:
OS: CentoOS6.6_X64
Node1:172.16.10.1
Node2:172.16.10.2
VIP:172.16.10.10
1、配置主机互信
Node1:
[root@node1 ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:11:21:56
inet addr:172.16.100.1 Bcast:172.16.100.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe11:2156/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:144 errors:0 dropped:0 overruns:0 frame:0
TX packets:114 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14467 (14.1 KiB) TX bytes:11961 (11.6 KiB)
[root@node1 ~]# ntpdate 172.16.100.30
25 Jul 11:34:52 ntpdate[2123]: step time server 172.16.100.30 offset -28852.934223 sec
[root@node1 ~]# crontab -e
15 5 * * * /usr/bin/crontab 172.16.100.30
[root@node1 ~]# hostname node1.Smoke.com
[root@node1 ~]# hostname
node1.Smoke.com
[root@node1 ~]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1.Smoke.com
[root@node1 ~]# uname -n
node1.Smoke.com
[root@node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.100.1 node1.Smoke.com node1
172.16.100.2 node2.Smoke.com node2
[root@node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generating public/private rsa key pair.
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
4c:9e:47:8a:a1:ee:d3:64:d5:e0:ab:21:fd:e6:43:19 root@node1.Smoke.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
| ...o. |
| . *E+. |
| ....S+. |
| .. = +. |
| .= = |
| .. o + |
| .. o.. |
+-----------------+
[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.100.2
The authenticity of host '172.16.100.2 (172.16.100.2)' can't be established.
RSA key fingerprint is cc:c3:c7:45:9e:40:60:dd:03:13:b3:37:9a:ff:0e:8a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.100.2' (RSA) to the list of known hosts.
root@172.16.100.2's password:
Now try logging into the machine, with "ssh 'root@172.16.100.2'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
Node2:
[root@node2 ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:E8:20:12
inet addr:172.16.100.2 Bcast:172.16.100.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fee8:2012/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:589 errors:0 dropped:0 overruns:0 frame:0
TX packets:782 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:45720 (44.6 KiB) TX bytes:175486 (171.3 KiB)
[root@node2 ~]# ntpdate 172.16.100.30
25 Jul 11:46:17 ntpdate[2216]: step time server 172.16.100.30 offset -28853.089885 sec
[root@node2 ~]# crontab -e
15 5 * * * /usr/bin/crontab 172.16.100.30
[root@node2 ~]# hostname node2.Smoke.com
[root@node2 ~]# hostname
node2.Smoke.com
[root@node2 ~]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node2.Smoke.com
[root@node2 ~]# uname -n
node2.Smoke.com
[root@node2 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.100.1 node1.Smoke.com node1
172.16.100.2 node2.Smoke.com node2
[root@node2 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
51:55:ee:dd:5b:f7:b9:b1:f7:0e:de:b9:34:55:85:01 root@node2.Smoke.com
The key's randomart image is:
+--[ RSA 2048]----+
| ..Eooo.|
| . .. .|
| . . .|
| . . .o|
| S . *|
| B|
| .*.|
| ..oB|
| .*B|
+-----------------+
[root@node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.100.1
The authenticity of host '172.16.100.1 (172.16.100.1)' can't be established.
RSA key fingerprint is 85:d3:16:71:c5:c5:d6:dd:dd:93:53:10:a9:63:4d:ca.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.100.1' (RSA) to the list of known hosts.
root@172.16.100.1's password:
Now try logging into the machine, with "ssh 'root@172.16.100.1'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
2、安装mysql-server,我这里使用mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz二进制文件安装,请最好使用高于5.6版本的mysql,因为支持server-id;
Node1:
[root@node1 ~]# ls anaconda-ks.cfg install.log install.log.syslog mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz [root@node1 ~]# mkdir -pv /mydata/data mkdir: 已创建目录 "/mydata" mkdir: 已创建目录 "/mydata/data" [root@node1 ~]# groupadd -r -g 306 mysql [root@node1 ~]# useradd -g 306 -r -u 306 mysql [root@node1 ~]# chown -R mysql.mysql /mydata/data/ [root@node1 ~]# tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@node1 ~]# cd /usr/local/ [root@node1 local]# ls bin etc games include lib lib64 libexec mysql-5.6.12-linux-glibc2.5-x86_64 sbin share src [root@node1 local]# ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql "mysql" -> "mysql-5.6.12-linux-glibc2.5-x86_64" [root@node1 local]# cd mysql [root@node1 mysql]# ll 总用量 76 drwxr-xr-x. 2 root root 4096 7月 25 12:03 bin -rw-r--r--. 1 7161 wheel 17987 1月 23 2013 COPYING drwxr-xr-x. 4 root root 4096 7月 25 12:03 data drwxr-xr-x. 2 root root 4096 7月 25 12:03 docs drwxr-xr-x. 3 root root 4096 7月 25 12:03 include -rw-r--r--. 1 7161 wheel 7468 1月 23 2013 INSTALL-BINARY drwxr-xr-x. 3 root root 4096 7月 25 12:03 lib drwxr-xr-x. 4 root root 4096 7月 25 12:03 man drwxr-xr-x. 10 root root 4096 7月 25 12:03 mysql-test -rw-r--r--. 1 7161 wheel 2552 1月 23 2013 README drwxr-xr-x. 2 root root 4096 7月 25 12:03 scripts drwxr-xr-x. 28 root root 4096 7月 25 12:03 share drwxr-xr-x. 4 root root 4096 7月 25 12:03 sql-bench drwxr-xr-x. 3 root root 4096 7月 25 12:03 support-files [root@node1 mysql]# chown -R root.mysql ./* [root@node1 mysql]# ll 总用量 76 drwxr-xr-x. 2 root mysql 4096 7月 25 12:03 bin -rw-r--r--. 1 root mysql 17987 1月 23 2013 COPYING drwxr-xr-x. 4 root mysql 4096 7月 25 12:03 data drwxr-xr-x. 2 root mysql 4096 7月 25 12:03 docs drwxr-xr-x. 3 root mysql 4096 7月 25 12:03 include -rw-r--r--. 1 root mysql 7468 1月 23 2013 INSTALL-BINARY drwxr-xr-x. 3 root mysql 4096 7月 25 12:03 lib drwxr-xr-x. 4 root mysql 4096 7月 25 12:03 man drwxr-xr-x. 10 root mysql 4096 7月 25 12:03 mysql-test -rw-r--r--. 1 root mysql 2552 1月 23 2013 README drwxr-xr-x. 2 root mysql 4096 7月 25 12:03 scripts drwxr-xr-x. 28 root mysql 4096 7月 25 12:03 share drwxr-xr-x. 4 root mysql 4096 7月 25 12:03 sql-bench drwxr-xr-x. 3 root mysql 4096 7月 25 12:03 support-files [root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data [root@node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@node1 mysql]# chkconfig --add mysqld [root@node1 mysql]# vim my.cnf datadir = /mydata/data innodb_file_per_table = ON server-id = 1 socket = /tmp/mysql.sock log-bin = mysql-bin [root@node1 bin]# netstat -tnlp 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 2346/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1026/master tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 2275/sshd tcp 0 0 :::22 :::* LISTEN 2346/sshd tcp 0 0 ::1:25 :::* LISTEN 1026/master tcp 0 0 ::1:6011 :::* LISTEN 2275/sshd tcp 0 0 :::3306 :::* LISTEN 12146/mysqld [root@node1 bin]# vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin [root@node1 bin]# . /etc/profile.d/mysql.sh [root@node1 bin]# mysqladmin -u root password smoke520 [root@node1 mysql]# vim my.cnf sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES datadir = /mydata/data innodb_file_per_table = ON server-id = 1 socket = /tmp/mysql.sock log-bin = mysql-bin log-error = /mydata/data/node1.Smoke.com.error.log skip-slave-start = 1 auto_increment_offset = 2 auto_increment_increment = 10 [root@node1 mysql]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
Node2:
[root@node2 ~]# ls anaconda-ks.cfg install.log install.log.syslog mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz [root@node2 ~]# mkdir -pv /mydata/data mkdir: 已创建目录 "/mydata" mkdir: 已创建目录 "/mydata/data" [root@node2 ~]# groupadd -r -g 306 mysql [root@node2 ~]# useradd -g 306 -r -u 306 mysql [root@node2 ~]# chown -R mysql.mysql /mydata/data/ [root@node2 ~]# tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@node2 ~]# cd /usr/local/ [root@node2 local]# ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql "mysql" -> "mysql-5.6.12-linux-glibc2.5-x86_64" [root@node2 local]# cd mysql [root@node2 mysql]# chown -R root.mysql ./* [root@node2 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data [root@node2 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@node2 mysql]# chkconfig --add mysqld [root@node2 mysql]# vim my.cnf sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES datadir = /mydata/data innodb_file_per_table = ON server-id = 2 socket = /tmp/mysql.sock log-bin = mysql-bin log-error = /mydata/data/node2.Smoke.com.error.log skip-slave-start = 1 auto_increment_offset = 2 auto_increment_increment = 10 [root@node2 mysql]# service mysqld start Starting MySQL.. SUCCESS! [root@node2 mysql]# vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin [root@node2 mysql]# . /etc/profile.d/mysql.sh [root@node2 mysql]# mysqladmin -u root password smoke520
Node1:
[root@node1 mysql]# mysql -u root -psmoke520
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.12-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 425 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'172.16.100.%' IDENTIFIED BY 'replication';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
CHANGE MASTER TO
master_host='172.16.100.2',
master_user='replication',
master_password='replication',
master_log_file='mysql-bin.000001',
master_log_pos=572;
Query OK, 0 rows affected, 2 warnings (0.08 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.04 sec)
Node2:
[root@node2 mysql]# mysql -u root -psmoke520
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.12-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 572 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'172.16.100.%' IDENTIFIED BY 'replication';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO
-> master_host='172.16.100.1',
-> master_user='replication',
-> master_password='replication',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=425;
Query OK, 0 rows affected, 2 warnings (0.08 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)
Node1:
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.100.2
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 572
Relay_Log_File: node1-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 572
Relay_Log_Space: 456
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: 2aeb7f76-5245-11e6-ab53-000c29e82012
Master_Info_File: /mydata/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
Node2:
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.100.1
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 425
Relay_Log_File: node2-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 425
Relay_Log_Space: 456
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 77c4f525-5240-11e6-ab35-000c29112156
Master_Info_File: /mydata/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
Node1:
mysql> USE test; Database changed mysql> SHOW TABLES; Empty set (0.00 sec) mysql> CREATE TABLE user(number INT(10),name VARCHAR(255)); Query OK, 0 rows affected (0.12 sec) mysql> INSERT INTO user VALUES(01,'zhangsan'); Query OK, 1 row affected (0.01 sec) mysql> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | user | +----------------+ 1 row in set (0.00 sec)
Node2:
mysql> USE test; Database changed mysql> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | user | +----------------+ 1 row in set (0.00 sec) mysql> SELECT number,name FROM user; +--------+----------+ | number | name | +--------+----------+ | 1 | zhangsan | +--------+----------+ 1 row in set (0.00 sec)
3、安装keep-alive;
我这里使用keep-alive版本为keepalived-1.2.7.tar;
[root@node1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog keepalived-1.2.7.tar.gz mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz
[root@node1 ~]# yum install -y pcre-devel openssl-devel popt-devel
[root@node1 ~]# tar xf keepalived-1.2.7.tar.gz
[root@node1 ~]# cd keepalived-1.2.7
[root@node1 keepalived-1.2.7]# ./configure --prefix=/usr/local/keepalived
[root@node1 keepalived-1.2.7]# make && make install
[root@node1 keepalived-1.2.7]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@node1 keepalived-1.2.7]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@node1 keepalived-1.2.7]# mkdir /etc/keepalived/
[root@node1 keepalived-1.2.7]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@node1 keepalived-1.2.7]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@node1 keepalived-1.2.7]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalive@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.100.10
}
}
virtual_server 172.16.100.10 3306 {
delay_loop 2
#lb_algo rr
#lb_kind NAT
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 172.16.100.1 3306 {
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
[root@node1 keepalived-1.2.7]# vim /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@node1 keepalived-1.2.7]# chmod +x /usr/local/keepalived/mysql.sh
Node2:
[root@node2 ~]# ls
anaconda-ks.cfg install.log install.log.syslog keepalived-1.2.7.tar.gz mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz
[root@node2 ~]# yum install -y pcre-devel openssl-devel popt-devel
[root@node2 ~]# tar xf keepalived-1.2.7.tar.gz
[root@node2 ~]# cd keepalived-1.2.7
[root@node2 keepalived-1.2.7]# ./configure --prefix=/usr/local/keepalived
[root@node2 keepalived-1.2.7]# make && make install
[root@node2 keepalived-1.2.7]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@node2 keepalived-1.2.7]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@node2 keepalived-1.2.7]# mkdir /etc/keepalived/
[root@node2 keepalived-1.2.7]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@node2 keepalived-1.2.7]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@node2 keepalived-1.2.7]# scp root@node1:/etc/keepalived/keepalived.conf /etc/keepalived/
The authenticity of host 'node1 (172.16.100.1)' can't be established.
RSA key fingerprint is 85:d3:16:71:c5:c5:d6:dd:dd:93:53:10:a9:63:4d:ca.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node1' (RSA) to the list of known hosts.
keepalived.conf 100% 806 0.8KB/s 00:00
[root@node2 keepalived-1.2.7]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalive@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.100.10
}
}
virtual_server 172.16.100.10 3306 {
delay_loop 2
#lb_algo rr
#lb_kind NAT
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 172.16.100.2 3306 {
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
[root@node2 keepalived-1.2.7]# vim /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@node2 keepalived-1.2.7]# chmod +x /usr/local/keepalived/mysql.sh
Node1:
[root@node1 keepalived-1.2.7]# /etc/init.d/keepalived start 正在启动 keepalived: [确定] [root@node1 keepalived-1.2.7]# mysql -uroot -psmoke520 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 5.6.12-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT ALL ON *.* to 'root'@'172.16.100.%' identified by 'smoke520'; Query OK, 0 rows affected (0.03 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.02 sec)
Node2:
[root@node2 keepalived-1.2.7]# /etc/init.d/keepalived start 正在启动 keepalived: [确定] [root@node2 keepalived-1.2.7]# mysql -uroot -psmoke520 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 18 Server version: 5.6.12-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT ALL ON *.* TO 'root'@'172.16.100.%' identified by 'smoke520'; Query OK, 0 rows affected (0.00 sec)
4、测试:
开机一台mysql-client访问172.16.100.10;
[root@localhost ~]# mysql -uroot -psmoke520 -h172.16.100.10 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 292 Server version: 5.6.12-log MySQL Community Server (GPL) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW VARIABLES LIKE 'server_id'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 1 | +---------------+-------+ 1 row in set (0.01 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.01 sec) mysql> 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 mysql> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | user | +----------------+ 1 row in set (0.01 sec) mysql> SELECT * FROM user; +--------+----------+ | number | name | +--------+----------+ | 1 | zhangsan | +--------+----------+ 1 row in set (0.01 sec) mysql> \q Bye
查看node1主机相关信息;
[root@node1 ~]# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 00:0c:29:11:21:56 brd ff:ff:ff:ff:ff:ff
inet 172.16.100.1/24 brd 172.16.100.255 scope global eth0
inet 172.16.100.10/32 scope global eth0
inet6 fe80::20c:29ff:fe11:2156/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:11:21:60 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.137/24 brd 192.168.111.255 scope global eth1
inet6 fe80::20c:29ff:fe11:2160/64 scope link
valid_lft forever preferred_lft forever
[root@node1 ~]# tail /var/log/messages
Jul 25 20:06:06 node1 kernel: IPVS: Scheduler module ip_vs_ not found
Jul 25 20:12:14 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 25 20:12:15 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 25 20:12:15 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 25 20:12:15 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.100.10
Jul 25 20:12:15 node1 Keepalived_healthcheckers[17719]: Netlink reflector reports IP 172.16.100.10 added
Jul 25 20:12:20 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.100.10
Jul 25 20:12:44 node1 dhclient[2001]: DHCPREQUEST on eth1 to 192.168.111.254 port 67 (xid=0x765dffe3)
Jul 25 20:12:44 node1 dhclient[2001]: DHCPACK from 192.168.111.254 (xid=0x765dffe3)
Jul 25 20:12:46 node1 dhclient[2001]: bound to 192.168.111.137 -- renewal in 778 seconds.
关闭node1主机上的mysql服务器:
[root@node1 ~]# service mysqld stop Shutting down MySQL.. SUCCESS! [root@node1 ~]# ps aux | grep keep root 17783 0.0 0.0 103260 872 pts/0 S+ 20:19 0:00 grep keep [root@node1 ~]# tail /var/log/messages Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: TCP connection to [172.16.100.1]:3306 failed !!! Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: Removing service [172.16.100.1]:3306 from VS [172.16.100.10]:3306 Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: IPVS: Service not defined Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: Executing [/usr/local/keepalived/mysql.sh] for service [172.16.100.1]:3306 in VS [172.16.100.10]:3306 Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: Lost quorum 1-0=1 > 0 for VS [172.16.100.10]:3306 Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: Remote SMTP server [127.0.0.1]:25 connected. Jul 25 20:18:52 node1 Keepalived[17717]: Stopping Keepalived v1.2.7 (07/25,2016) Jul 25 20:18:52 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) sending 0 priority Jul 25 20:18:52 node1 Keepalived_healthcheckers[17719]: IPVS: No such service Jul 25 20:18:52 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) removing protocol VIPs.
通过mysql-client继续访问172.16.100.10;
[root@localhost ~]# mysql -uroot -psmoke520 -h172.16.100.10 ERROR 2003 (HY000): Can't connect to MySQL server on '172.16.100.10' (113) [root@localhost ~]# mysql -uroot -psmoke520 -h172.16.100.10 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.12-log MySQL Community Server (GPL) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW VARIABLES LIKE 'server_id'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 2 | +---------------+-------+ 1 row in set (0.01 sec) mysql> 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 mysql> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | user | +----------------+ 1 row in set (0.01 sec) mysql> SELECT * FROM user; +--------+----------+ | number | name | +--------+----------+ | 1 | zhangsan | +--------+----------+ 1 row in set (0.01 sec) mysql> \q Bye
浙公网安备 33010602011771号