基于keepalived实现mariadb的高可用

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
网络型负载均衡 NLB,每月750个小时 15LCU
简介:

提示:

上一篇博文己经介绍过了keepalived是什么,有那些参数,也介绍过基于corosync+pacemaker实现mairadb高可用,这次我将介绍一下如何利用keepalived对mariadb实现高可用。

----本文大纲

前言

主机环境

配置过程

测试

-----------

一、前言

说到对mariadb实现高可用,也就是就说,当有任何一个mariadb挂掉之后在还有其它mariadb主机接管业务,完全不会影响到线上的业务,当挂掉的主机修复后重新上线,周而复始的工作,这就要对maridb做主主复制,无论那一个主机重新上线,那可以做到从当前工作主机上同步数据;在以前,要对mariadb实现出现故障自动切换,主要是在主机之间发送心跳信息做健康检测,而keepalived则要做的是,如果当前主机的服务不可用,那么就要对当前的keepalived的优先级先降级,而从其它从节点中选举出一个新的keepalived从做为主,将vip转移到此主机上。

二、主机环境

系统 角色

IP地址

安装的软件
Centos 6.5 x86_64
DBMASTER1

VIP:192.168.1.200

IP:192.168.1.112

maridb-10(通用二进制格式)、ipvsadm、keepalived
Centos 6.5 x86_64
DBMASTER2

VIP:192.168.1.200

IP:192.168.1.113

maridb-10(通用二进制格式)、ipvsadm、keepalived

三、配置过程

1、DBMASTER1

数据库mariadb10通用二进制格式的安装略过(与mysql5.6安装方式完全相同)。

  • 配置文件/etc/my.cnf

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
[root@essun ~] # grep -v "#" /etc/my.cnf |grep -v "^$"
[client]
port        = 3306
socket      =  /tmp/maria .sock
[mysqld]
port        = 3306
socket      =  /tmp/maria .sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
datadir= /mydata/data
server- id =1
log-bin=mysql-bin
binlog_format=ROW
log-slave-updates
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
  • 安装ipvsamd、keepalived

1
#yum install -y ipvsadm keepalived
  • 修改配置文件/etc/keepalived/keepalived.conf

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
! Configuration File  for  keepalived
global_defs {
    notification_email {
      root@localhost
      essun@localhost
    }
    notification_email_from essun@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
     state MASTER
     interface eth0
     virtual_router_id 90
     priority 100
     advert_int 1
     nopreempt
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         192.168.1.200
     }
}
virtual_server 192.168.1.200 3306 {
     delay_loop 6
     lb_algo wrr
     lb_kind dr
     nat_mask 255.255.255.0
     persistence_timeout 50
     protocol TCP
     real_server 192.168.1.112 3306 {
         weight 1
     notify_down  /etc/keepalived/mari .sh  #当maridb停止时,执行的脚本
     TCP_CHECK {
             connect_timeout 3
             nb_get_retry 3
             delay_before_retry 3
         }
     }
}
  • 事件脚本

1
2
3
4
[root@essun keepalived]# cat mari.sh
#!/bin/bash
killall - 9  keepalived
[root@essun keepalived]# chmod +x /etc/keepalived/mari.sh
  • 修改内核参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@essun ~]# tail - 5  /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore =  1
net.ipv4.conf.all.arp_announce =  2
net.ipv4.conf.lo.arp_ignore =  1
net.ipv4.conf.lo.arp_announce =  2
[root@essun ~]# sysctl -p |tail - 5
error:  "net.bridge.bridge-nf-call-ip6tables"  is  an unknown key
error:  "net.bridge.bridge-nf-call-iptables"  is  an unknown key
error:  "net.bridge.bridge-nf-call-arptables"  is  an unknown key
kernel.shmall =  4294967296
net.ipv4.conf.all.arp_ignore =  1
net.ipv4.conf.all.arp_announce =  2
net.ipv4.conf.lo.arp_ignore =  1
net.ipv4.conf.lo.arp_announce =  2
  • 启动服务(mariadb、keepalived)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@essun keepalived]# service mari start
Starting MySQL.........                                    [  OK  ]
[root@essun keepalived]# service keepalived start
Starting keepalived:                                       [  OK  ]
[root@essun keepalived]# ip addr show
1 : lo: <LOOPBACK,UP,LOWER_UP> mtu  16436  qdisc noqueue state UNKNOWN
     link/loopback  00 : 00 : 00 : 00 : 00 : 00  brd  00 : 00 : 00 : 00 : 00 : 00
     inet  127.0 . 0.1 / 8  scope host lo
     inet6 :: 1 / 128  scope host
        valid_lft forever preferred_lft forever
2 : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu  1500  qdisc pfifo_fast state UP qlen  1000
     link/ether  00 :0c: 29 : 98 :b0:ac brd ff:ff:ff:ff:ff:ff
     inet  192.168 . 1.112 / 24  brd  255.255 . 255.255  scope global eth0
     inet  192.168 . 1.200 / 32  scope global eth0
     inet6 fe80::20c:29ff:fe98:b0ac/ 64  scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun keepalived]# ipvsadm -L -n
IP Virtual Server version  1.2 . 1  (size= 4096 )
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP   192.168 . 1.200 : 3306  wrr persistent  50
   ->  192.168 . 1.112 : 3306            Local    1       0           0
  • 查看当前主机上的binlog日志

1
2
3
4
5
6
7
8
MariaDB [(none)]> show master status
     -> ;
+ ------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+ ------------------+----------+--------------+------------------+
| mysql-bin.000021 |      358 |              |                  |
+ ------------------+----------+--------------+------------------+
1 row  in  set  (0.00 sec)
  • 授权复制用户与测试用户

1
2
MariaDB [(none)]>  grant  replication slave,replication client  on  *.*  to  'repluser' @ '192.168.1.113'  identified  by  'replpass' ;
MariaDB [(none)]>  grant  replication slave,replication client  on  *.*  to  'root' @ '192.168.1.%'  identified  by  'mari' ;
  • 查看一下授权表记录

1
2
3
4
5
6
7
8
9
10
11
12
13
MariaDB [(none)]>  select  user ,host, password  from  mysql. user ;
+ -------------+------------------+-------------------------------------------+
user         | host             |  password                                   |
+ -------------+------------------+-------------------------------------------+
| root        | localhost        |                                           |
| root        | master.mysql.com |                                           |
| root        | 127.0.0.1        |                                           |
| root        | ::1              |                                           |
|             | localhost        |                                           |
| repluser    | 192.168.1.113    | *D98280F03D0F78162EBDBB9C883FC01395DEA2BF |
| root        | 192.168.1.%      | *CA3CBE479AD72908BED6733E9D9695CC2DAC4BC5 |
+ -------------+------------------+-------------------------------------------+
12  rows  in  set  (0.00 sec)

2、DBMASTER2 (192.168.1.113)

  • 修改/etc/my.cnf

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
[root@essun ~] # grep -v "#" /etc/my.cnf |grep -v "^$"
[client]
port        = 3306
socket      =  /tmp/maria .sock
[mysqld]
port        = 3306
socket      =  /tmp/maria .sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
datadir= /mydata/data
binlog_format=row
server- id =30
log-bin=mysql-bin
log-slave-updates
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

此处的配置文件只有server-id与DBMASTER是不同的,其它的都一样

  • 安装ipvsadm、keepalived

1
#yum install -y ipvsadm keepalived
  • 修改keepalived配置文件

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
[root@essun ~]# cat /etc/keepalived/keepalived.conf
! Configuration File  for  keepalived
global_defs {
    notification_email {
      root@localhost
      essun@localhost
    }
    notification_email_from essun@localhost
    smtp_server  127.0 . 0.1
    smtp_connect_timeout  30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
     state BACKUP
     interface  eth0
     virtual_router_id  90
     priority  95
     advert_int  1
     nopreempt
     authentication {
         auth_type PASS
         auth_pass  1111
     }
     virtual_ipaddress {
         192.168 . 1.200
     }
}
virtual_server  192.168 . 1.200  3306  {
     delay_loop  6
     lb_algo wrr
     lb_kind dr
     nat_mask  255.255 . 255.0
     persistence_timeout  50
     protocol TCP
     real_server  192.168 . 1.113  3306  {
         weight  1
     notify_down /etc/keepalived/mari.sh
     TCP_CHECK {
             connect_timeout  3
             nb_get_retry  3
             delay_before_retry  3
         }
     }
}
  • 事件脚本

1
2
3
4
[root@essun keepalived] # cat mari.sh
#!/bin/bash
killall -9 keepalived
[root@essun keepalived] # chmod +x /etc/keepalived/mari.sh
  • 修改内核参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@essun ~] # tail -5 /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@essun ~] # sysctl -p |tail -5
error:  "net.bridge.bridge-nf-call-ip6tables"  is an unknown key
error:  "net.bridge.bridge-nf-call-iptables"  is an unknown key
error:  "net.bridge.bridge-nf-call-arptables"  is an unknown key
kernel.shmall = 4294967296
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
  • 启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@essun ~] # service mari start
Starting MySQL....                                         [  OK  ]
[root@essun ~] # service keepalived start
Starting keepalived:                                       [  OK  ]
[root@essun ~] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:d2:39:cb brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.113 /24  brd 255.255.255.255 scope global eth0
     inet6 fe80::20c:29ff:fed2:39cb /64  scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun ~] # ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:3306 wrr persistent 50
   -> 192.168.1.113:3306           Local   1      0          0

由于vip192.168.1.200并不在当前主机,所以当前节点是不会工作的。

  • 查看当前主机的binlog日志

1
2
3
4
5
6
7
MariaDB [(none)]> show master status;
+ ------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+ ------------------+----------+--------------+------------------+
| mysql-bin.000017 |      374 |              |                  |
+ ------------------+----------+--------------+------------------+
1 row  in  set  (0.00 sec)
  • 添加复制与测试用户

1
2
MariaDB [(none)]>  grant  replication slave,replication client  on  *.*  to  'repluser' @ '192.168.1.112'  identified  by  'replpass' ;
MariaDB [(none)]>  grant  replication slave,replication client  on  *.*  to  'root' @ '192.168.1.%'  identified  by  'mari' ;
  • 在DBMASTER1使用change连接DBMASTER2

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
MariaDB [(none)]> change master  to  master_host= '192.168.1.113' ,master_user= 'repluser' ,master_password= 'replpass' ,master_log_file= 'mysql-bin.000017' ,master_log_pos=374;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting  for  master  to  send event
                   Master_Host: 192.168.1.113
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000017
           Read_Master_Log_Pos: 374
                Relay_Log_File: essun-relay-bin.000029
                 Relay_Log_Pos: 661
         Relay_Master_Log_File: mysql-bin.000017
              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: 374
               Relay_Log_Space: 1245
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: Yes
            Master_SSL_CA_File: /etc/slave/cacert.pem
            Master_SSL_CA_Path:
               Master_SSL_Cert: /etc/slave/mysql.crt
             Master_SSL_Cipher:
                Master_SSL_Key: /etc/slave/mysql. 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: 30
                Master_SSL_Crl: /etc/slave/cacert.pem
            Master_SSL_Crlpath:
                    Using_Gtid:  No
                   Gtid_IO_Pos:
1 row  in  set  (0.00 sec)
  • 在DBMASTER2连接DBMASTER1

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
MariaDB [(none)]> change master  to  master_host= '192.168.1.112' ,master_user= 'repluser' ,master_password= 'replpass' ,master_log_file= 'mysql-bin.000021' ,master_log_pos=358;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting  for  master  to  send event
                   Master_Host: 192.168.1.112
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000021
           Read_Master_Log_Pos: 358
                Relay_Log_File: essun-relay-bin.000026
                 Relay_Log_Pos: 535
         Relay_Master_Log_File: mysql-bin.000021
              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: 358
               Relay_Log_Space: 832
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: Yes
            Master_SSL_CA_File: /etc/slave/cacert.pem
            Master_SSL_CA_Path:
               Master_SSL_Cert: /etc/slave/mysql.crt
             Master_SSL_Cipher:
                Master_SSL_Key: /etc/slave/mysql. 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_SSL_Crl: /etc/slave/cacert.pem
            Master_SSL_Crlpath:
                    Using_Gtid:  No
                   Gtid_IO_Pos:
1 row  in  set  (0.00 sec)

四、测试

1、数据库复制测试

  • 在DBMASTER1中建立一个数据库DBMASTER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MariaDB [(none)]>  create  database  DBMASTER;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases;
+ --------------------+
Database            |
+ --------------------+
| DBMASTER           |
| a                  |
| b                  |
| ceshidb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
| xxyy               |
| yydb               |
+ --------------------+
11  rows  in  set  (0.00 sec)
  • 在DBMASTER2,也建立一个DBMASTER1,并查看一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
MariaDB [(none)]>  create  database  DBMASTER1
     -> ;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+ --------------------+
Database            |
+ --------------------+
| DBMASTER           |
| DBMASTER1          |
| a                  |
| b                  |
| ceshidb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
| xxyy               |
+ --------------------+
11  rows  in  set  (0.00 sec)
  • DBMASTER1建立的数据库己经复制到了DBMASTER2上,在DBMASTER2上建立的数据库己在DBMASTER1中.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MariaDB [(none)]> show databases;
+ --------------------+
Database            |
+ --------------------+
| DBMASTER           |
| DBMASTER1          |
| a                  |
| b                  |
| ceshidb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
| xxyy               |
| yydb               |
+ --------------------+
12  rows  in  set  (0.00 sec)

2、测试事件脚本(DBMASTER1)

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@essun keepalived] # service mari status
MySQL running (3501)                                       [  OK  ]
[root@essun keepalived] # service keepalived status
keepalived (pid  3583) is running...
[root@essun keepalived] # ps aux |grep keepalived
root      3583  0.0  0.3 110148  1092 ?        Ss   04:21   0:00  /usr/sbin/keepalived  -D
root      3585  0.0  0.8 112376  2580 ?        S    04:21   0:00  /usr/sbin/keepalived  -D
root      3586  0.0  0.6 112252  2008 ?        S    04:21   0:01  /usr/sbin/keepalived  -D
root      3724  0.0  0.2 103252   828 pts /0     S+   05:10   0:00  grep  keepalived
[root@essun keepalived] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:98:b0:ac brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.112 /24  brd 255.255.255.255 scope global eth0
     inet 192.168.1.200 /32  scope global eth0
     inet6 fe80::20c:29ff:fe98:b0ac /64  scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun keepalived] # ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:3306 wrr persistent 50
   -> 192.168.1.112:3306           Local   1      0          0
[root@essun keepalived] # service mari stop
Shutting down MySQL..                                      [  OK  ]
[root@essun keepalived] # ps aux |grep keepalived
root      3763  0.0  0.2 103252   820 pts /0     S+   05:11   0:00  grep  keepalived
[root@essun keepalived] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:98:b0:ac brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.112 /24  brd 255.255.255.255 scope global eth0
     inet 192.168.1.200 /32  scope global eth0
     inet6 fe80::20c:29ff:fe98:b0ac /64  scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun keepalived] # ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:3306 wrr persistent 50

只要一停止maradb,vip就转移了

  • 查看一下DBMASTER2上的ip信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@essun ~] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:d2:39:cb brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.113 /24  brd 255.255.255.255 scope global eth0
     inet 192.168.1.200 /32  scope global eth0
     inet6 fe80::20c:29ff:fed2:39cb /64  scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun ~] # ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:3306 wrr persistent 50
   -> 192.168.1.113:3306           Local   1      0          0
  • 登陆数据库测试下,将vip切换到DBMASRTER1上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@essun keepalived] # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:98:b0:ac brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.112 /24  brd 255.255.255.255 scope global eth0
     inet 192.168.1.200 /32  scope global eth0
     inet6 fe80::20c:29ff:fe98:b0ac /64  scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun keepalived] # ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:3306 wrr persistent 50
   -> 192.168.1.112:3306           Local   1      0          0    
[root@essun keepalived] #
  • 使用vip登录测试

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
root@essun ~]# mysql -uroot -pmari -h192.168.1.200
Welcome  to  the MariaDB monitor.  Commands  end  with  or  \g.
Your MariaDB  connection  id  is  39
Server version: 10.0.10-MariaDB-log Source distribution
Copyright (c) 2000, 2014, Oracle, SkySQL Ab  and  others.
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear the  current  input statement.
MariaDB [(none)]> show databases;
+ --------------------+
Database            |
+ --------------------+
| DBMASTER           |
| DBMASTER1          |
| a                  |
| b                  |
| ceshidb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
| xxyy               |
| yydb               |
+ --------------------+
12  rows  in  set  (0.00 sec)
MariaDB [(none)]>

这是DBMASTER1上的数据库,此时不退出登录状态,将DBMASRER1上的mariadb关闭,看一下能否切换到。

  • DBMASRTER2上的数据库上

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@essun keepalived]# service mari stop
Shutting down MySQL.                                       [  OK  ]
[root@essun keepalived]#
##################DBMASTER2####################
[root@essun ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
     inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:0c:29:d2:39:cb brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.113/24 brd 255.255.255.255 scope  global  eth0
     inet 192.168.1.200/32 scope  global  eth0
     inet6 fe80::20c:29ff:fed2:39cb/64 scope link tentative dadfailed
        valid_lft forever preferred_lft forever
[root@essun ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 ( size =4096)
Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port            Forward  Weight ActiveConn InActConn
TCP  192.168.1.200:3306 wrr persistent 50
   -> 192.168.1.113:3306            Local    1      0          0
#######################由此发现地址己经切换过到DBMASTER2上了######
#再show databases;
unknown [(none)]> show databases;
No  connection . Trying  to  reconnect...
Connection  id:    11
Current  database : *** NONE ***
+ --------------------+
Database            |
+ --------------------+
| DBMASTER           |
| DBMASTER1          |
| a                  |
| b                  |
| ceshidb            |
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
| xxyy               |
+ --------------------+
11  rows  in  set  (0.00 sec)
MariaDB [(none)]>
#这己经是DBMASTER2的数据库了









本文转自 jinlinger 51CTO博客,原文链接:http://blog.51cto.com/essun/1405880,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
关系型数据库 MySQL Linux
小白带你学习linux的mariadb高可用MAH
小白带你学习linux的mariadb高可用MAH
128 0
|
关系型数据库 MySQL Linux
|
关系型数据库 MySQL 测试技术
基于Keepalived高可用集群的MariaDB读写分离机制实现
一 MariaDB读写分离机制 在实现读写分离机制之前先理解一下三种主从复制方式:1.异步复制:MariaDB默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题,主节点如果挂掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。
1278 0
|
NoSQL 关系型数据库 MySQL
阿里云RDS关系型数据库大全_MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等
阿里云RDS关系型数据库如MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等,NoSQL数据库如Redis、Tair、Lindorm和MongoDB
363 0