21edqwq 发表于 2016-7-27 10:28:09

Keepalive实现mysql双主热备

环境描述:
OS: CentoOS6.6_X64
Node1:172.16.10.1
Node2:172.16.10.2
VIP:172.16.10.10
1、配置主机互信
Node1:

1
2
3
4
5
6
7
8
9
# ifconfig eth0
eth0      Link encap:EthernetHWaddr 00:0C:29:11:21:56
          inet addr:172.16.100.1Bcast:172.16.100.255Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe11:2156/64 Scope:Link
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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)





1
2
# ntpdate 172.16.100.30
25 Jul 11:34:52 ntpdate: step time server 172.16.100.30 offset -28852.934223 sec





1
2
# crontab -e
15 5 * * * /usr/bin/crontab 172.16.100.30





1
# hostname node1.Smoke.com





1
2
# hostname
node1.Smoke.com





1
2
3
# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1.Smoke.com





1
2
# uname -n
node1.Smoke.com





1
2
3
4
5
# 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





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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..       |
+-----------------+





1
2
3
4
5
6
7
8
9
# 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:

1
2
3
4
5
6
7
8
9
# ifconfig eth0
eth0      Link encap:EthernetHWaddr 00:0C:29:E8:20:12
          inet addr:172.16.100.2Bcast:172.16.100.255Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fee8:2012/64 Scope:Link
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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)





1
2
# ntpdate 172.16.100.30
25 Jul 11:46:17 ntpdate: step time server 172.16.100.30 offset -28853.089885 sec





1
2
# crontab -e
15 5 * * * /usr/bin/crontab 172.16.100.30





1
# hostname node2.Smoke.com





1
2
# hostname
node2.Smoke.com





1
2
3
# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node2.Smoke.com





1
2
# uname -n
node2.Smoke.com





1
2
3
4
5
# 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





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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|
+-----------------+





1
2
3
4
5
6
7
8
9
# 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:

1
2
# ls
anaconda-ks.cfginstall.loginstall.log.syslogmysql-5.6.12-linux-glibc2.5-x86_64.tar.gz





1
2
3
# mkdir -pv /mydata/data
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/data"





1
# groupadd -r -g 306 mysql





1
# useradd -g 306 -r -u 306 mysql





1
# chown -R mysql.mysql /mydata/data/





1
# tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/





1
# cd /usr/local/





1
2
# ls
binetcgamesincludeliblib64libexecmysql-5.6.12-linux-glibc2.5-x86_64sbinsharesrc





1
2
# ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql
"mysql" -> "mysql-5.6.12-linux-glibc2.5-x86_64"





1
# cd mysql





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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 wheel7468 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 wheel2552 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





1
# chown -R root.mysql ./*





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# ll
总用量 76
drwxr-xr-x.2 root mysql4096 7月25 12:03 bin
-rw-r--r--.1 root mysql 17987 1月23 2013 COPYING
drwxr-xr-x.4 root mysql4096 7月25 12:03 data
drwxr-xr-x.2 root mysql4096 7月25 12:03 docs
drwxr-xr-x.3 root mysql4096 7月25 12:03 include
-rw-r--r--.1 root mysql7468 1月23 2013 INSTALL-BINARY
drwxr-xr-x.3 root mysql4096 7月25 12:03 lib
drwxr-xr-x.4 root mysql4096 7月25 12:03 man
drwxr-xr-x. 10 root mysql4096 7月25 12:03 mysql-test
-rw-r--r--.1 root mysql2552 1月23 2013 README
drwxr-xr-x.2 root mysql4096 7月25 12:03 scripts
drwxr-xr-x. 28 root mysql4096 7月25 12:03 share
drwxr-xr-x.4 root mysql4096 7月25 12:03 sql-bench
drwxr-xr-x.3 root mysql4096 7月25 12:03 support-files





1
#scripts/mysql_install_db --user=mysql --datadir=/mydata/data





1
# cp support-files/mysql.server /etc/init.d/mysqld





1
# chkconfig --add mysqld





1
2
3
4
5
6
# vim my.cnf
datadir = /mydata/data
innodb_file_per_table = ON
server-id = 1
socket = /tmp/mysql.sock
log-bin = mysql-bin





1
2
3
4
5
6
7
8
9
10
# 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





1
2
# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin





1
# . /etc/profile.d/mysql.sh





1
# mysqladmin -u root password smoke520





1
2
3
4
5
6
7
8
9
10
11
# 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





1
2
3
# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!




Node2:

1
2
# ls
anaconda-ks.cfginstall.loginstall.log.syslogmysql-5.6.12-linux-glibc2.5-x86_64.tar.gz





1
2
3
# mkdir -pv /mydata/data
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/data"





1
# groupadd -r -g 306 mysql





1
# useradd -g 306 -r -u 306 mysql





1
# chown -R mysql.mysql /mydata/data/





1
# tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/





1
# cd /usr/local/





1
2
# ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql
"mysql" -> "mysql-5.6.12-linux-glibc2.5-x86_64"





1
# cd mysql





1
# chown -R root.mysql ./*





1
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data





1
# cp support-files/mysql.server /etc/init.d/mysqld





1
# chkconfig --add mysqld





1
2
3
4
5
6
7
8
9
10
11
# 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





1
2
# service mysqld start
Starting MySQL.. SUCCESS!





1
2
# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin





1
# . /etc/profile.d/mysql.sh





1
# mysqladmin -u root password smoke520




Node1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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> GRANTREPLICATION 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;

1
2
# ls
anaconda-ks.cfginstall.loginstall.log.syslogkeepalived-1.2.7.tar.gzmysql-5.6.12-linux-glibc2.5-x86_64.tar.gz





1
# yum install -y pcre-devel openssl-devel popt-devel





1
# tar xf keepalived-1.2.7.tar.gz





1
# cd keepalived-1.2.7





1
# ./configure --prefix=/usr/local/keepalived





1
# make && make install





1
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/





1
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/





1
# mkdir /etc/keepalived/





1
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/





1
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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
      }
}





1
2
3
# vim /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived





1
# chmod +x /usr/local/keepalived/mysql.sh




Node2:

1
2
# ls
anaconda-ks.cfginstall.loginstall.log.syslogkeepalived-1.2.7.tar.gzmysql-5.6.12-linux-glibc2.5-x86_64.tar.gz





1
# yum install -y pcre-devel openssl-devel popt-devel





1
# tar xf keepalived-1.2.7.tar.gz





1
# cd keepalived-1.2.7





1
# ./configure --prefix=/usr/local/keepalived





1
# make && make install





1
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/





1
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/





1
# mkdir /etc/keepalived/





1
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/





1
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/





1
2
3
4
5
6
# 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





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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
      }
    }





1
2
3
# vim /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived





1
# chmod +x /usr/local/keepalived/mysql.sh




Node1:

1
2
# /etc/init.d/keepalived start
正在启动 keepalived:                                    [确定]





1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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:

1
2
# /etc/init.d/keepalived start
正在启动 keepalived:                                    [确定]





1
2
3
4
5
6
7
8
9
10
11
12
# 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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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主机相关信息;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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





1
2
3
4
5
6
7
8
9
10
11
# 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: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 25 20:12:15 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 25 20:12:15 node1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 25 20:12:15 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.100.10
Jul 25 20:12:15 node1 Keepalived_healthcheckers: Netlink reflector reports IP 172.16.100.10 added
Jul 25 20:12:20 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.100.10
Jul 25 20:12:44 node1 dhclient: DHCPREQUEST on eth1 to 192.168.111.254 port 67 (xid=0x765dffe3)
Jul 25 20:12:44 node1 dhclient: DHCPACK from 192.168.111.254 (xid=0x765dffe3)
Jul 25 20:12:46 node1 dhclient: bound to 192.168.111.137 -- renewal in 778 seconds.




关闭node1主机上的mysql服务器:

1
2
# service mysqld stop
Shutting down MySQL.. SUCCESS!





1
2
# ps aux | grep keep
root      177830.00.0 103260   872 pts/0    S+   20:19   0:00 grep keep





1
2
3
4
5
6
7
8
9
10
11
# tail /var/log/messages
Jul 25 20:18:52 node1 Keepalived_healthcheckers: TCP connection to :3306 failed !!!
Jul 25 20:18:52 node1 Keepalived_healthcheckers: Removing service :3306 from VS :3306
Jul 25 20:18:52 node1 Keepalived_healthcheckers: IPVS: Service not defined
Jul 25 20:18:52 node1 Keepalived_healthcheckers: Executing for service :3306 in VS :3306
Jul 25 20:18:52 node1 Keepalived_healthcheckers: Lost quorum 1-0=1 > 0 for VS :3306
Jul 25 20:18:52 node1 Keepalived_healthcheckers: Remote SMTP server :25 connected.
Jul 25 20:18:52 node1 Keepalived: Stopping Keepalived v1.2.7 (07/25,2016)
Jul 25 20:18:52 node1 Keepalived_vrrp: VRRP_Instance(VI_1) sending 0 priority
Jul 25 20:18:52 node1 Keepalived_healthcheckers: IPVS: No such service
Jul 25 20:18:52 node1 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.




通过mysql-client继续访问172.16.100.10;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# mysql -uroot -psmoke520 -h172.16.100.10
ERROR 2003 (HY000): Can't connect to MySQL server on '172.16.100.10' (113)
# 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



页: [1]
查看完整版本: Keepalive实现mysql双主热备