​Keepalive实现mysql双主热备

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

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
[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:0overruns:0 frame:0
           TX packets:114 errors:0 dropped:0overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:14467 (14.1 KiB)  TX bytes:11961 (11.6 KiB)

向ntp服务器同步时间

1
2
[root@node1~] # ntpdate 172.16.100.30
25 Jul11:34:52 ntpdate[2123]: step  time  server 172.16.100.30 offset -28852.934223 sec

创建同步时间任务计划

[root@node1~]# crontab -e

1
15 5 * * *  /usr/sbin/ntpdate  172.16.100.30

修改主机名称

1
[root@node1~] # hostname node1.Smoke.com
1
2
[root@node1~] # hostname
node1.Smoke.com
1
2
3
[root@node1~] # vim /etc/sysconfig/network
NETWORKING= yes
HOSTNAME=node1.Smoke.com
1
2
[root@node1~] # uname -n
node1.Smoke.com

添加host文件主机名对应的IP地址

1
2
3
4
5
[root@node1~] # vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4localhost4.localdomain4
::1         localhost localhost.localdomainlocalhost6 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
[root@node1~] # ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generatingpublic /private  rsa key pair.
Createddirectory  '/root/.ssh' .
Youridentification has been saved  in  /root/ . ssh /id_rsa .
Yourpublic key has been saved  in  /root/ . ssh /id_rsa .pub.
The keyfingerprint is:
4c:9e:47:8a:a1:ee:d3:64:d5:e0:ab:21:fd:e6:43:19root@node1.Smoke.com
Thekey's randomart image is:
+--[ RSA2048]----+
|                 |
|        .       |
|      ...o.     |
|     . *E+.     |
|    ....S+.     |
|   .. = +.      |
|    .= =        |
|   .. o +       |
|    .. o..      |
+-----------------+

将公钥传给node2主机

1
2
3
4
5
6
7
8
9
10
11
[root@node1~] # ssh-copy-id -i .ssh/id_rsa.pub root@172.16.100.2
Theauthenticity of host  '172.16.100.2 (172.16.100.2)'  can't be established.
RSA key fingerprintis cc:c3:c7:45:9e:40:60: dd :03:13:b3:37:9a:ff:0e:8a.
Are yousure 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'spassword:
Now trylogging into the machine, with  "ssh 'root@172.16.100.2'" , and checkin:
  
   . ssh /authorized_keys
  
to makesure we haven 't added extra keys that you weren' t expecting.

Node2:

1
2
3
4
5
6
7
8
9
[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:0overruns:0 frame:0
           TX packets:782 errors:0 dropped:0 overruns:0carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:45720 (44.6 KiB)  TX bytes:175486 (171.3 KiB)
1
2
[root@node2~] # ntpdate 172.16.100.30
25 Jul11:46:17 ntpdate[2216]: step  time  server 172.16.100.30 offset -28853.089885 sec
1
2
[root@node2~] # crontab -e
15 5 * * *  /usr/sbin/ntpdate  172.16.100.30
1
[root@node2~] # hostname node2.Smoke.com
1
2
[root@node2~] # hostname
node2.Smoke.com
1
2
3
[root@node2~] # vim /etc/sysconfig/network
NETWORKING= yes
HOSTNAME=node2.Smoke.com
1
2
[root@node2~] # uname -n
node2.Smoke.com
1
2
3
4
5
[root@node2~] # vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4localhost4.localdomain4
::1         localhost localhost.localdomainlocalhost6 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
[root@node2~] # ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generatingpublic /private  rsa key pair.
Youridentification has been saved  in  /root/ . ssh /id_rsa .
Yourpublic key has been saved  in  /root/ . ssh /id_rsa .pub.
The keyfingerprint is:
51:55:ee: dd :5b:f7:b9:b1:f7:0e:de:b9:34:55:85:01root@node2.Smoke.com
Thekey's randomart image is:
+--[ RSA2048]----+
|          ..Eooo.|
|         .  .. .|
|        .    . .|
|         .  . .o|
|        S    . *|
|                B|
|              .*.|
|             ..oB|
|              .*B|
+-----------------+
1
2
3
4
5
6
7
8
9
10
11
[root@node2~] # ssh-copy-id -i .ssh/id_rsa.pub root@172.16.100.1
Theauthenticity of host  '172.16.100.1 (172.16.100.1)'  can't be established.
RSA keyfingerprint is 85:d3:16:71:c5:c5:d6: dd : dd :93:53:10:a9:63:4d:ca.
Are yousure 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'spassword:
Now trylogging into the machine, with  "ssh 'root@172.16.100.1'" , and checkin:
  
   . ssh /authorized_keys
  
to makesure 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
[root@node1~] # ls
anaconda-ks.cfg   install .log  install .log.syslog  mysql-5.6.12-linux-glibc2.5-x86_64. tar .gz
1
2
3
[root@node1~] # mkdir -pv /mydata/data
mkdir : 已创建目录  "/mydata"
mkdir : 已创建目录  "/mydata/data"
1
[root@node1~] # groupadd -r -g 306 mysql
1
[root@node1~] # useradd -g 306 -r -u 306 mysql
1
[root@node1~] # chown -R mysql.mysql /mydata/data/
1
[root@node1~] # tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
1
[root@node1~] # cd /usr/local/
1
2
[root@node1local] # ls
bin  etc games  include  lib lib64  libexec  mysql-5.6.12-linux-glibc2.5-x86_64  sbin share  src
1
2
[root@node1local] # ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql
"mysql" ->  "mysql-5.6.12-linux-glibc2.5-x86_64"
1
[root@node1local] # cd mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@node1mysql] # 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
1
[root@node1mysql] # chown -R root.mysql ./*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@node1mysql] # 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月  2512:03 support-files
1
[root@node1mysql] #  scripts/mysql_install_db--user=mysql --datadir=/mydata/data
1
[root@node1mysql] # cp support-files/mysql.server /etc/init.d/mysqld
1
[root@node1mysql] # chkconfig --add mysqld
1
2
3
4
5
6
[root@node1mysql] # 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
[root@node1bin] # netstat -tnlp
ActiveInternet connections (only servers)
ProtoRecv-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
[root@node1bin] # vim /etc/profile.d/mysql.sh
exportPATH=$PATH: /usr/local/mysql/bin
1
[root@node1bin] # . /etc/profile.d/mysql.sh
1
[root@node1bin] # mysqladmin -u root password smoke520
1
2
3
4
5
6
7
8
9
10
11
[root@node1mysql] # 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
[root@node1mysql] # service mysqld restart
Shuttingdown MySQL.. SUCCESS!
StartingMySQL. SUCCESS!

Node2:

1
2
[root@node2~] # ls
anaconda-ks.cfg   install .log  install .log.syslog mysql-5.6.12-linux-glibc2.5-x86_64. tar .gz
1
2
3
[root@node2~] # mkdir -pv /mydata/data
mkdir : 已创建目录  "/mydata"
mkdir : 已创建目录  "/mydata/data"
1
[root@node2~] # groupadd -r -g 306 mysql
1
[root@node2~] # useradd -g 306 -r -u 306 mysql
1
[root@node2~] # chown -R mysql.mysql /mydata/data/
1
[root@node2~] # tar xf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
1
[root@node2~] # cd /usr/local/
1
2
[root@node2local] # ln -sv mysql-5.6.12-linux-glibc2.5-x86_64 mysql
"mysql" ->  "mysql-5.6.12-linux-glibc2.5-x86_64"
1
[root@node2local] # cd mysql
1
[root@node2mysql] # chown -R root.mysql ./*
1
[root@node2mysql] # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
1
[root@node2mysql] # cp support-files/mysql.server /etc/init.d/mysqld
1
[root@node2mysql] # chkconfig --add mysqld
1
2
3
4
5
6
7
8
9
10
11
[root@node2mysql] # 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
[root@node2mysql] # service mysqld start
StartingMySQL.. SUCCESS!
1
2
[root@node2mysql] # vim /etc/profile.d/mysql.sh
exportPATH=$PATH: /usr/local/mysql/bin
1
[root@node2mysql] # . /etc/profile.d/mysql.sh
1
[root@node2mysql] # 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
31
32
33
34
35
36
37
38
[root@node1mysql]# mysql -u root -psmoke520
Warning:Using a  password  on  the command line interface can be insecure.
Welcometo the MySQL monitor.  Commands  end  with or  \g.
YourMySQL  connection  id  is  2
Serverversion: 5.6.12-log MySQL Community Server (GPL)
  
Copyright(c) 2000, 2013, Oracle  and / or  its affiliates.  All  rights reserved.
  
Oracleis 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 inset (0.00 sec)
  
mysql> GRANT  REPLICATION SLAVE  ON  *.*  TO   'replication' @ '172.16.100.%'  IDENTIFIED  BY  'replication' ;
QueryOK, 0  rows  affected (0.00 sec)
  
mysql>FLUSH  PRIVILEGES ;
QueryOK, 0  rows  affected (0.00 sec)
  
mysql>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;
QueryOK, 0  rows  affected, 2 warnings (0.08 sec)
  
mysql>START SLAVE;
QueryOK, 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
31
32
33
34
35
36
37
38
[root@node2mysql]# mysql -u root -psmoke520
Warning:Using a  password  on  the command line interface can be insecure.
Welcometo the MySQL monitor.  Commands  end  with or  \g.
YourMySQL  connection  id  is  3
Serverversion: 5.6.12-log MySQL Community Server (GPL)
  
Copyright(c) 2000, 2013, Oracle  and / or  its affiliates.  All  rights reserved.
  
Oracleis 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 inset (0.00 sec)
  
mysql> GRANT  REPLICATION SLAVE  ON  *.*  TO  'replication' @ '172.16.100.%'  IDENTIFIED  BY 'replication' ;
QueryOK, 0  rows  affected (0.01 sec)
  
mysql>FLUSH  PRIVILEGES ;
QueryOK, 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;
QueryOK, 0  rows  affected, 2 warnings (0.08 sec)
  
mysql>START SLAVE;
QueryOK, 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 formaster  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 readall 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 inset (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 formaster  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 readall 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 inset (0.00 sec)

Node1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql>USE test;
Databasechanged
mysql>SHOW TABLES;
Emptyset (0.00 sec)
mysql> CREATE  TABLE  user (number  INT (10), name  VARCHAR (255));
QueryOK, 0  rows  affected (0.12 sec)
mysql> INSERT  INTO  user  VALUES (01, 'zhangsan' );
QueryOK, 1 row affected (0.01 sec)
mysql>SHOW TABLES;
+ ----------------+
|Tables_in_test |
+ ----------------+
| user            |
+ ----------------+
1 row inset (0.00 sec)

Node2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql>USE test;
Databasechanged
mysql>SHOW TABLES;
+ ----------------+
|Tables_in_test |
+ ----------------+
| user            |
+ ----------------+
1 row inset (0.00 sec)
mysql> SELECT  number, name  FROM  user ;
+ --------+----------+
| number|  name      |
+ --------+----------+
|      1 | zhangsan |
+ --------+----------+
1 row inset (0.00 sec)


3、安装keep-alive;

我这里使用keep-alive版本为keepalived-1.2.7.tar;

1
2
[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
1
[root@node1~] # yum install -y pcre-devel openssl-devel popt-devel
1
[root@node1~] # tar xf keepalived-1.2.7.tar.gz
1
[root@node1~] # cd keepalived-1.2.7
1
[root@node1keepalived-1.2.7] # ./configure --prefix=/usr/local/keepalived
1
[root@node1keepalived-1.2.7] # make && make install
1
[root@node1keepalived-1.2.7] # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
1
[root@node1keepalived-1.2.7] # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
1
[root@node1keepalived-1.2.7] # mkdir /etc/keepalived/
1
[root@node1keepalived-1.2.7] # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
1
[root@node1keepalived-1.2.7] # 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
44
45
46
47
48
[root@node1keepalived-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_instanceVI_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
[root@node1keepalived-1.2.7] # vim /usr/local/keepalived/mysql.sh
#!/bin/bash
pkillkeepalived
1
[root@node1keepalived-1.2.7] # chmod +x /usr/local/keepalived/mysql.sh

Node2:

1
2
[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
1
[root@node2~] # yum install -y pcre-devel openssl-devel popt-devel
1
[root@node2~] # tar xf keepalived-1.2.7.tar.gz
1
[root@node2~] # cd keepalived-1.2.7
1
[root@node2keepalived-1.2.7] # ./configure --prefix=/usr/local/keepalived
1
[root@node2keepalived-1.2.7] # make && make install
1
[root@node2keepalived-1.2.7] # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
1
[root@node2keepalived-1.2.7] # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
1
[root@node2keepalived-1.2.7] # mkdir /etc/keepalived/
1
[root@node2keepalived-1.2.7] # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
1
[root@node2keepalived-1.2.7] # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
1
2
3
4
5
6
[root@node2keepalived-1.2.7] # scp root@node1:/etc/keepalived/keepalived.conf /etc/keepalived/
Theauthenticity of host  'node1 (172.16.100.1)'  can't be established.
RSA keyfingerprint is 85:d3:16:71:c5:c5:d6: dd : dd :93:53:10:a9:63:4d:ca.
Are yousure 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
44
45
46
47
[root@node2keepalived-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_instanceVI_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_server172.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
[root@node2keepalived-1.2.7] # vim /usr/local/keepalived/mysql.sh
#!/bin/bash
pkillkeepalived
1
[root@node2keepalived-1.2.7] # chmod +x /usr/local/keepalived/mysql.sh

Node1:

1
2
[root@node1keepalived-1.2.7] # /etc/init.d/keepalived start
正在启动keepalived:                                      [确定]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@node1keepalived-1.2.7]# mysql -uroot -psmoke520
Warning:Using a  password  on  the command line interface can be insecure.
Welcometo the MySQL monitor.  Commands  end  with or  \g.
YourMySQL  connection  id  is  17
Serverversion: 5.6.12-log MySQL Community Server (GPL)
  
Copyright(c) 2000, 2013, Oracle  and / or  its affiliates.  All  rights reserved.
  
Oracleis 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' ;
QueryOK, 0  rows  affected (0.03 sec)
  
mysql>FLUSH  PRIVILEGES ;
QueryOK, 0  rows  affected (0.02 sec)

Node2:

1
2
[root@node2keepalived-1.2.7] # /etc/init.d/keepalived start
正在启动keepalived:                                      [确定]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@node2keepalived-1.2.7]# mysql -uroot -psmoke520
Warning:Using a  password  on  the command line interface can be insecure.
Welcometo the MySQL monitor.  Commands  end  with or  \g.
YourMySQL  connection  id  is  18
Serverversion: 5.6.12-log MySQL Community Server (GPL)
  
Copyright(c) 2000, 2013, Oracle  and / or  its affiliates.  All  rights reserved.
  
Oracleis 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' ;
QueryOK, 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
47
48
49
50
51
52
53
54
55
root@localhost~]# mysql -uroot -psmoke520 -h172.16.100.10
Welcometo the MySQL monitor.  Commands  end  with or  \g.
YourMySQL  connection  id  is  292
Serverversion: 5.6.12-log MySQL Community Server (GPL)
  
Copyright(c) 2000, 2011, Oracle  and / or  its affiliates.  All  rights reserved.
  
Oracleis 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 inset (0.01 sec)
  
mysql>SHOW DATABASES;
+ --------------------+
| Database            |
+ --------------------+
|information_schema |
|mysql              |
|performance_schema |
|test               |
+ --------------------+
4 rowsin  set  (0.01 sec)
  
mysql>USE test;
Readingtable information  for  completion  of  table  and  column  names
You canturn  off  this feature  to  get a quicker startup  with  -A
  
Databasechanged
mysql>SHOW TABLES;  
+ ----------------+
|Tables_in_test |
+ ----------------+
| user            |
+ ----------------+
1 row inset (0.01 sec)
  
mysql> SELECT  FROM  user ;
+ --------+----------+
| number|  name      |
+ --------+----------+
|      1 | zhangsan |
+ --------+----------+
1 row inset (0.01 sec)
  
mysql>\q
Bye

查看node1主机相关信息;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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 brd00: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 qlen1000
     link /ether  00:0c:29:11:21:56 brdff:ff:ff:ff:ff:ff
     inet 172.16.100.1 /24  brd 172.16.100.255scope global eth0
     inet 172.16.100.10 /32  scope global eth0
     inet6 fe80::20c:29ff:fe11:2156 /64  scopelink
        valid_lft forever preferred_lft forever
3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000
     link /ether  00:0c:29:11:21:60 brdff:ff:ff:ff:ff:ff
     inet 192.168.111.137 /24  brd 192.168.111.255scope global eth1
     inet6 fe80::20c:29ff:fe11:2160 /64  scopelink
        valid_lft forever preferred_lft forever
1
2
3
4
5
6
7
8
9
10
11
[root@node1~] # tail /var/log/messages
Jul 2520:06:06 node1 kernel: IPVS: Scheduler module ip_vs_ not found
Jul 2520:12:14 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Transition to MASTERSTATE
Jul 2520:12:15 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Entering MASTERSTATE
Jul 2520:12:15 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) setting protocolVIPs.
Jul 2520:12:15 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Sending gratuitousARPs on eth0  for  172.16.100.10
Jul 2520:12:15 node1 Keepalived_healthcheckers[17719]: Netlink reflector reports IP172.16.100.10 added
Jul 2520:12:20 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) Sending gratuitousARPs on eth0  for  172.16.100.10
Jul 2520:12:44 node1 dhclient[2001]: DHCPREQUEST on eth1 to 192.168.111.254 port 67(xid=0x765dffe3)
Jul 2520:12:44 node1 dhclient[2001]: DHCPACK from 192.168.111.254 (xid=0x765dffe3)
Jul 2520:12:46 node1 dhclient[2001]: bound to 192.168.111.137 -- renewal  in  778seconds.

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

1
2
[root@node1~] # service mysqld stop
Shuttingdown MySQL.. SUCCESS!
1
2
[root@node1~] # ps aux | grep keep
root      17783 0.0  0.0 103260   872 pts /0    S+   20:19   0:00  grep  keep
1
2
3
4
5
6
7
8
9
10
11
[root@node1~] # tail /var/log/messages
Jul 2520:18:52 node1 Keepalived_healthcheckers[17719]: TCP connection to[172.16.100.1]:3306 failed !!!
Jul 2520:18:52 node1 Keepalived_healthcheckers[17719]: Removing service[172.16.100.1]:3306 from VS [172.16.100.10]:3306
Jul 2520:18:52 node1 Keepalived_healthcheckers[17719]: IPVS: Service not defined
Jul 2520: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 2520:18:52 node1 Keepalived_healthcheckers[17719]: Lost quorum 1-0=1 > 0 forVS [172.16.100.10]:3306
Jul 2520:18:52 node1 Keepalived_healthcheckers[17719]: Remote SMTP server[127.0.0.1]:25 connected.
Jul 2520:18:52 node1 Keepalived[17717]: Stopping Keepalived v1.2.7 (07 /25 ,2016)
Jul 2520:18:52 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) sending 0 priority
Jul 2520:18:52 node1 Keepalived_healthcheckers[17719]: IPVS: No such service
Jul 2520:18:52 node1 Keepalived_vrrp[17720]: VRRP_Instance(VI_1) removing protocolVIPs.

通过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
[root@localhost~]# mysql -uroot -psmoke520 -h172.16.100.10
ERROR2003 (HY000): Can 't connect to MySQL server on ' 172.16.100.10 ' (113)
[root@localhost~]# mysql -uroot -psmoke520 -h172.16.100.10
Welcometo the MySQL monitor.  Commands end with; or \g.
YourMySQL connection id is 4
Serverversion: 5.6.12-log MySQL Community Server (GPL)
  
Copyright(c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  
Oracleis 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 inset (0.01 sec)
  
mysql>USE test;
Readingtable information  for  completion  of  table  and  column  names
You canturn  off  this feature  to  get a quicker startup  with  -A
  
Databasechanged
mysql>SHOW TABLES;
+ ----------------+
|Tables_in_test |
+ ----------------+
| user            |
+ ----------------+
1 row inset (0.01 sec)
  
mysql> SELECT  FROM  user ;
+ --------+----------+
| number|  name      |
+ --------+----------+
|      1 | zhangsan |
+ --------+----------+
1 row inset (0.01 sec)
  
mysql>\q
Bye







      本文转自灬落魄灬  51CTO博客,原文链接:http://blog.51cto.com/smoke520/1953664 ,如需转载请自行联系原作者

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
Prometheus 监控 Cloud Native
手把手教你Prometheus + Granafa实现mysql 性能监测部署
数据库性能监控可以说是十分重要,能否自行搭建环境实现像阿里云或是腾讯云那样直观的展示不同维度数据的功能?答案是肯定的。下面详细说明一下安装部署过程以及过程中出现的问题,希望对你有所帮助!
手把手教你Prometheus + Granafa实现mysql 性能监测部署
|
SQL 关系型数据库 MySQL
MySql字符串拆分实现split功能(字段分割转列、转行)
MySql字符串拆分实现split功能(字段分割转列、转行)
MySql字符串拆分实现split功能(字段分割转列、转行)
|
SQL 缓存 关系型数据库
MySQL日志(undo log 和 redo log 实现事务的原子性/持久性/一致性)
MySQL日志(undo log 和 redo log 实现事务的原子性/持久性/一致性)
MySQL日志(undo log 和 redo log 实现事务的原子性/持久性/一致性)
|
关系型数据库 MySQL
Mysql中文汉字转拼音的实现
Mysql中文汉字转拼音的实现
585 0
|
canal 关系型数据库 MySQL
基于 Docker 结合 Canal 实现 MySQL 实时增量数据传输
基于 Docker 结合 Canal 实现 MySQL 实时增量数据传输
900 0
基于 Docker 结合 Canal 实现 MySQL 实时增量数据传输
|
SQL 前端开发 关系型数据库
mysql实现一次将多条不同sql查询结果并封装到一个结果集
最近遇到一个统计查询需求,要求一次性查询多个统计信息,其中两个查询信息不在一个表中,也没有业务关联,表中也没有做连接处理。不考虑产品设计是否合理,完全是实际需求如此,需要一次性查询出来返回给前端进行展示,对于这种“非常规”的统计查询平常肯定会遇见,感觉有点代表性,所以简单记录一下。希望对有相同需求的同学可以作为参考。
mysql实现一次将多条不同sql查询结果并封装到一个结果集
|
Ubuntu 关系型数据库 MySQL
Linux:Ubuntu安装jdk、tomcat、mysql,以及实现Tomcat开机自启动
Linux:Ubuntu安装jdk、tomcat、mysql,以及实现Tomcat开机自启动
332 0
Linux:Ubuntu安装jdk、tomcat、mysql,以及实现Tomcat开机自启动
|
canal SQL 关系型数据库
10.【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-canal简单使用
【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-canal简单使用
10.【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-canal简单使用
|
canal 关系型数据库 MySQL
9.【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-canal安装
canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-canal安装
9.【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-canal安装
|
canal 消息中间件 关系型数据库
8.【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-mysql安装
canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-mysql安装
8.【canal】canal从入门到放弃-mysql+canal+rocketmq实现数据库同步-mysql安装