Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql+keepalived dual master hot standby is highly available

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Introduction to the theory:

What we usually call dual-machine hot standby means that both machines are running, but not both machines are providing services at the same time. When one of the service providers fails, the other will automatically take over and provide service immediately, and the switching time is very short. MySQL dual-master replication, that is, Master-Slave for each other (only one Master provides write operation), can achieve hot backup of database server, but a Master cannot be dynamically switched after downtime. Using Keepalived, we can realize the unified interface between two hosts and the mechanism of automatic checking and failure switching through virtual IP, so as to realize the high availability scheme of MySQL database.

Environment description:

Version version 5.6.25-log

Main 1 Library ip: 10.219.24.25

Master 2 Library ip:10.219.24.22

Vip: 10.219.24.30

Os version: centos 6.7

Hot standby software installed: xtrabackup

Firewall is off

Add:

Principle of master-slave replication: http://blog.csdn.NET/zhang123456456/article/details/72972701

MySQL 5.6installation: http://blog.csdn.net/zhang123456456/article/details/53608554

Xtrabackup installation: http://blog.csdn.Net/zhang123456456/article/details/72836184

The whole process: first build one master and one slave, and then build it vice versa, that is, double master replication, and finally configure a highly available environment for Mysql+Keepalived failover

First, build one master and one slave first, then build in the opposite direction, and configure double master replication

1. Adjust the parameters of the main library

-- stop the main library mysql

[root@mysql02 ~] # service mysql stop

[root@mysql02 ~] # netstat-nltp | grep mysql | grep 3606

-- the main library creates a relay log directory

[root@mysql02 full] # mkdir-p / data/mysql/relaylog/

[root@mysql02 full] # chown-R mysql:mysql / data/mysql/relaylog

-- adjust my.cnf parameters

[root@mysql02 ~] # cat / etc/my.cnf

[client]

Password = Oracle

Port = 3306

Socket = / data/mysql/mysql.sock

[mysqld]

Server-id=25

Port = 3306

Socket = / data/mysql/mysql.sock

Character_set_server = utf8

Character_set_client = utf8

Collation-server=utf8_general_ci

Lower_case_table_names = 1

Max_connections = 1000

Datadir = / data/mysql

Log_bin = / data/mysql/binarylog/binlog

Log_bin_index = / data/mysql/binarylog/binlog

Relay-log = / data/mysql/relaylog/relay

Relay-log-index = / data/mysql/relaylog/relay

Relay_log_purge = on

Binlog_format = mixed

Innodb_data_file_path = ibdata1:12M:autoextend

Auto_increment_increment = 10

Auto_increment_offset = 1

[mysql]

Default-character-set = utf8

Description:

A. Parameters that must be configured in the main library

Server-id (master and slave server-id must be different), log_bin, binlog_format, relay-log, relay-log-index, relay_log_purge

Auto-increment-offset 、 auto-increment-increment

B, log-slave-updates means whether these changes need to be included in your own binarylog after the relay log is executed. It needs to be turned on when your B server needs to be the master server for another server. That is, two masters back up each other, or multi-master cycle backup. We need it here, so open it.

C, auto-increment-offset and auto-increment-increment are used to back up each other in a dual-master (multi-master loop). Because each database server may insert data in the same table, if the table has an automatically growing primary key, primary key conflicts will occur on multiple servers. The solution to this problem is to make the self-increasing primary keys of each database discontiguous. As shown in the figure above, I assume that 10 servers may be needed for backup in the future, so auto-increment-increment is set to 10. 0. And auto-increment-offset=1 represents the serial number of the server. Starting from 1, do not exceed auto-increment-increment. After doing this, the first id I insert on this server is 1, and the id on the second line is 11, not 2. (similarly, the first id inserted on the second server is 2, and the second line is 12, which will be described later) so that there is no primary key conflict. We will demonstrate the effect of this id later.

-- start the main library

[root@mysql02] # mysqld_safe-- defaults-file=/etc/my.cnf &

2. Adjust the parameters from the library

-- stop mysql from library

[root@mysql01 ~] # service mysql stop

[root@mysql01 ~] # netstat-nltp | grep mysql | grep 3606

-- adjust my.cnf parameters

[root@mysql01 ~] # cat / etc/my.cnf

[client]

Password = oracle

Port = 3306

Socket = / data/mysql/mysql.sock

[mysqld]

Server-id=22

Port = 3306

Socket = / data/mysql/mysql.sock

Character_set_server = utf8

Character_set_client = utf8

Collation-server=utf8_general_ci

Lower_case_table_names = 1

Max_connections = 1000

Datadir = / data/mysql

Log_bin = / data/mysql/binarylog/binlog

Log_bin_index = / data/mysql/binarylog/binlog

Relay-log = / data/mysql/relaylog/relay

Relay-log-index = / data/mysql/relaylog/relay

Relay_log_purge = on

Auto_increment_increment = 10

Auto_increment_offset = 2

[mysql]

Default-character-set = utf8

Description: parameters that must be configured from the library

Server-id 、 log_bin 、 relay-log 、 relay_log_purge 、 auto-increment-offset 、 auto-increment-increment 、

3. Backup of the main database

-- main library backup directory

[root@mysql02 full] # pwd

/ xtrabackup/full

-- innobackupex backup of the main library

[root@mysql02] # innobackupex-- user=root-- password=oracle-- port=3606 / xtrabackup/full/

170610 17:50:23 Backup created in directory'/ xtrabackup/full/2017-06-10 billion 17-50-19 Universe

MySQL binlog position: filename 'binlog.000010', position' 120'

....

170610 17:50:23 completed OK!

-- check backup binlog number and cut-off position

[root@mysql02 2017-06-10 17-50-19] # cat xtrabackup_binlog_info

Binlog.000010 120

4. Create the same backup directory as the master library from the slave library.

[root@mysql01] # mkdir-p / xtrabackup/full

[root@mysql01] # chown-R mysql:mysql / xtrabackup/full/

5. The master library will back up the scp to the slave library

[root@mysql02 full] # pwd

/ xtrabackup/full

[root@mysql02 full] # scp-r 2017-06-10 10.219.24.22:/xtrabackup/full 17-50-19

6. View the backups from scp from the library

[root@mysql01] # cd / xtrabackup/full/2017-06-10 17-50-19 /

[root@mysql01 2017-06-10 17-50-19] # ll

Total 12320

-rw-r-. 1 root root 419 Jun 10 18:01 backup-my.cnf

-rw-r-. 1 root root 12582912 Jun 10 18:01 ibdata1

Drwxr-x---. 2 root root 4096 Jun 10 18:01 mysql

Drwxr-x---. 2 root root 4096 Jun 10 18:01 performance_schema

Drwxr-x---. 2 root root 4096 Jun 10 18:01 test

-rw-r-. 1 root root 18 Jun 10 18:01 xtrabackup_binlog_info

-rw-r-. 1 root root 113 Jun 10 18:01 xtrabackup_checkpoints

-rw-r-. 1 root root 482 Jun 10 18:01 xtrabackup_info

-rw-r-. 1 root root 2560 Jun 10 18:01 xtrabackup_logfile

7. Create synchronous users in the main library

Mysql > GRANT replication slave ON *. * TO 'slave25'@'%' IDENTIFIED BY' oracle'

Query OK, 0 rows affected (0.05 sec)

8. Recover the main database data from the library

Rename the original datadir folder to a new location from the library and create the original folder

[root@mysql01 ~] # mv / data/mysql / data/mysqlbak

[root@mysql01] # mkdir-p / data/mysql

-- innobackupex apply-log

[root@mysql01 ~] # innobackupex-- apply-log-- user=oracle\

-- password=oracle-- port=3606 / xtrabackup/full/2017-06-1010 17-50-19 /

-- innobackupex copy recovered files to the original data location

[root@mysql01 mysql] # innobackupex-defaults-file=/etc/my.cnf-user=root\

-- copy-back / xtrabackup/full/2017-06-10, 17-50-19 /

170610 18:25:11 completed OK!

-- create binlog directory and relaylog directory and empower them

[root@mysql01] # mkdir-p / data/mysql/binarylog

[root@mysql01] # mkdir-p / data/mysql/relaylog/

[root@mysql01 mysql] # chown-R mysql:mysql / data/mysql

9. Slave library configuration and detection

-- launch from the library

[root@mysql01 mysql] # mysqld_safe-- defaults-file=/etc/my.cnf &

-- specify the basic information synchronized with the master library from the slave library

Mysql >

Change master to

Master_host='10.219.24.25'

Master_port=3306

Master_user='slave25'

Master_password='oracle'

Master_log_file='binlog.000010'

Master_log_pos=120

Query OK, 0 rows affected, 2 warnings (0.04 sec)

Parameter explanation:

MASTER_HOST: sets the ip address of the primary server to connect to

MASTER_USER: sets the user name of the primary server to connect to

MASTER_PASSWORD: sets the password of the primary server to connect to

MASTER_LOG_FILE: sets the log name of the bin log of the primary server to connect to

MASTER_LOG_POS: sets the location of the bin log of the primary server to connect to

-- start slave status (start listening for msater changes)

Mysql > start slave

Query OK, 0 rows affected, 1 warning (0.00 sec)

-- check the status of slave.

Mysql > show slave status\ G

* * 1. Row *

Slave_IO_State: Waiting for master to send event

Master_Host: 10.219.24.25 # main library IP

Master_User: slave25 # users replicated by the main library

Master_Port: 3306 # main library mysqld

Connect_Retry: 60

Master_Log_File: binlog.000010 # io_thread reads the main library master_log_file

Read_Master_Log_Pos: 717 # io_thread reads the main library master_log_pos

Relay_Log_File: relay.000002

Relay_Log_Pos: 877

Relay_Master_Log_File: binlog.000010 # sql_thread executes the master_log_file of the main library

Slave_IO_Running: the key of Yes # is whether io_thread is running

Slave_SQL_Running: the key of Yes # is whether sql_thread is running

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: 717 # sql_thread executes the master_log_pos of the main library

Relay_Log_Space: 1040

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 # delay of slave library

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: 25

Master_UUID: 29d68531-4cf9-11e7-8e1f-000c297c4100

Master_Info_File: / data/mysql/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)

ERROR:

No query specified

10. Master-slave synchronization check

-- main library

Mysql > create database repl

Query OK, 1 row affected (0.00 sec)

Mysql > use repl

Database changed

Mysql > create table repl (id int)

Query OK, 0 rows affected (0.02 sec)

Mysql > insert into repl values (1)

Query OK, 1 row affected (0.00 sec)

-from the library

Mysql > show databases

+-+

| | Database |

+-+

| | information_schema |

| | binarylog |

| | mysql |

| | performance_schema |

| | relaylog |

| | repl |

| | test |

+-+

7 rows in set (0.00 sec)

Mysql > use repl

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 > select * from repl

+-+

| | id |

+-+

| | 1 |

+-+

1 row in set (0.00 sec) > one master and one slave synchronization succeeded!

# # #

# so far, the replication from A to B has been configured, and the replication from B to An is configured below. #

# # #

Declaration > in the following operations, the new master database is the original slave database (10.219.24.22), and the new slave database is the original master library (10.219.24.25).

11. Create synchronous users in the new main library

Mysql > GRANT replication slave ON *. * TO 'slave22'@'%' IDENTIFIED BY' oracle'

Query OK, 0 rows affected (0.00 sec)

12. Check the binlog file number and position point in the new main library.

Mysql > show master status

+-+

| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |

+-+

| | binlog.000004 | 313 |

+-+

1 row in set (0.00 sec)

13. The new slave library specifies the basic information synchronized with the master library.

Mysql >

Change master to

Master_host='10.219.24.22'

Master_port=3306

Master_user='slave22'

Master_password='oracle'

Master_log_file='binlog.000004'

Master_log_pos=313

Query OK, 0 rows affected, 2 warnings (0.04 sec)

14. Open the slave replication function in the new slave library.

Mysql > start slave

Query OK, 0 rows affected (0.00 sec)

15. New slave library detects synchronous replication status

Mysql > show slave status\ G

* * 1. Row *

Slave_IO_State: Waiting for master to send event

Master_Host: 10.219.24.22

Master_User: slave22

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: binlog.000004

Read_Master_Log_Pos: 313

Relay_Log_File: relay.000002

Relay_Log_Pos: 280

Relay_Master_Log_File: binlog.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: 313

Relay_Log_Space: 443

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: 22

Master_UUID: 70023652-4dc7-11e7-9360-000c2944297a

Master_Info_File: / data/mysql/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)

ERROR:

No query specified

-- Test data synchronization status of new slave library

Mysql > create database mm_repl

Query OK, 1 row affected (0.00 sec)

Mysql > use mm_repl

Database changed

Mysql > create table mm_repl (id int auto_increment,name varchar (10), primary key (id))

Query OK, 0 rows affected (0.01 sec)

Mysql > insert into mm_repl (name) values ("andy"), ("taoYe")

Query OK, 1 row affected (0.00 sec)

Mysql > select * from mm_repl

+-+ +

| | id | name |

+-+ +

| | 1 | andy |

| | 11 | taoYe |

+-+ +

2 rows in set (0.00 sec)

-- Test data synchronization status of the new main library

Mysql > select * from mm_repl

+-+ +

| | id | name |

+-+ +

| | 1 | andy |

| | 11 | taoYe |

+-+ +

2 rows in set (0.00 sec)

Mysql > insert into mm_repl (name) values ("andy"), ("taoYe")

Query OK, 2 rows affected (0.00 sec)

Records: 2 Duplicates: 0 Warnings: 0

Mysql > select * from mm_repl

+-+ +

| | id | name |

+-+ +

| | 1 | andy |

| | 11 | taoYe |

| | 12 | andy |

| | 22 | taoYe |

+-+ +

4 rows in set (0.00 sec)

-- New slave library check synchronous replication

Mysql > select * from mm_repl

+-+ +

| | id | name |

+-+ +

| | 1 | andy |

| | 11 | taoYe |

| | 12 | andy |

| | 22 | taoYe |

+-+ +

4 rows in set (0.00 sec) > master master synchronization test succeeded

Configure a highly available environment for Mysql+Keepalived failover

Install keepalived (on both master1 and master2 machines)

II. 1.1 install LVS

Premise: the local Yum source configuration process has been configured in advance. Please see > http://blog.csdn.NET/zhang123456456/article/details/56690945.

1. Check the system kernel

Before installation, first check whether the system kernel supports LVS's IPVS module; since Linux 2.6, the system kernel has completely built-in LVS modules.

[root@mysql02 ~] # modprobe-l | grep ipvs

Kernel/net/netfilter/ipvs/ip_vs.ko

Kernel/net/netfilter/ipvs/ip_vs_rr.ko

Kernel/net/netfilter/ipvs/ip_vs_wrr.ko

Kernel/net/netfilter/ipvs/ip_vs_lc.ko

....

Kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko

Seeing the output information above indicates that the system kernel supports the IPVS module by default.

2. Install the dependency package

Since it is a source package installation, the following dependency packages need to be typed when compiling.

# yum-y install kernel-headers glibc-headers glibc-devel libgomp gcc libstdc++-devel gcc-c++

3. Yum install ipvsadm-1.26-4

[root@mysql02 ~] # yum-y install kernel-devel ipvsadm

=

Package Arch Version Repository Size

=

Installing:

Ipvsadm x8634 1.26-4.el6 c6-media 42k

Transaction Summary

=

Install 1 Package (s)

Total download size: 42 k

Installed size: 78 k

Downloading Packages:

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Installing: ipvsadm-1.26-4.el6.x86_64 1 Compact 1

Verifying: ipvsadm-1.26-4.el6.x86_64 1 Compact 1

Installed:

Ipvsadm.x86_64 0vl 1.26-4.el6

Complete!

4. Be a ln

[root@mysql02 ~] # ln-sv / usr/src/kernels/2.6.32-573.el6.x86_64/ / usr/src/linux can see the help information, which means that IPVS is installed successfully.

II. 1.2 install keepalived-1.2.24

1. Download address on the official website:

[root@mysql02 ~] # wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz

2. Extract the installation package:

[root@mysql02 ~] # ll keepalived-1.2.24.tar.gz

-rw-r--r--. 1 root root 601873 Jun 16 23:49 keepalived-1.2.24.tar.gz

[root@mysql02 ~] # tar-zxvf keepalived-1.2.24.tar.gz

3. Enter the installation directory:

[root@mysql02 ~] # cd keepalived-1.2.24

4. Install keepalived:

[root@mysql03 keepalived-1.2.24] # / configure-- prefix=/usr/local/keepalived-- with-kernel-dir=/usr/src/kernels/2.6.32-573.el6.x86_64/

Note:-- with-kernel-dri: specifies to use the header file in the kernel source code, that is, the include directory. This parameter is important and is only used when using LVS.

[root@mysql03 keepalived-1.2.24] # make & & make install

5. Copy scripts and files:

5.1 copy the execution file

[root@mysql02 keepalived-1.2.24] # cp / usr/local/keepalived/sbin/keepalived / usr/sbin/

5.2 copy the init.d file to etc and add the boot entry

[root@mysql02 keepalived-1.2.24] # cp / usr/local/keepalived/etc/rc.d/init.d/keepalived / etc/init.d/

5.3 copy the keepalived file to the etc and add the Nic configuration

[root@mysql02 keepalived-1.2.24] # cp / usr/local/keepalived/etc/sysconfig/keepalived / etc/sysconfig/

5.4 create a keepalived folder

[root@mysql02 keepalived-1.2.24] # mkdir / etc/keepalived

Copy the keepalived configuration file to etc

[root@mysql02 keepalived-1.2.24] # cp / usr/local/keepalived/etc/keepalived/keepalived.conf / etc/keepalived/

5.6 copy startup script

[root@mysql02 keepalived-1.2.24] # cp / usr/local/keepalived/sbin/keepalived / etc/keepalived/

6. Add boot entry

[root@mysql02 keepalived-1.2.24] # chkconfig-- add keepalived # must be added to ensure the existence of / etc/init.d/keepalived

7. Start

[root@mysql02 keepalived-1.2.24] # chmod 755 / etc/init.d/keepalived

[root@mysql03 keepalived-1.2.24] # service keepalived start

Starting keepalived: [OK]

8. Close

[root@mysql02 keepalived-1.2.24] # service keepalived stop

Stopping keepalived: [OK] > OK, success!

_

9. Bind virtual IP (VIP), only on master1 machines. # # Note: whether this step is done at will or not will not affect the normal use as an understanding.

Description:

Bind the virtual IP (VIP), which can only be bound on the master1 machine. After keepalived is configured as follows, when the master1 machine fails, the VIP resources will be automatically taken over by the master2 machine! (in fact, this step does not have to use ifconfig binding so directly outside. VIP can be directly configured in the configuration file of Keepalived. Using the command ip addr, you can see that in the highly available environment where vip; only has LVS+Keepalived layer 4 load balancer, vip should be set separately outside (that is, ifconfig eth0:0. To create a vip), you can view the vip through ifconfig. )

[root@mysql02 keepalived-1.2.24] # ifconfig

Eth0 Link encap:Ethernet HWaddr 00:0C:29:7C:41:00

Inet addr:10.219.24.25 Bcast:10.255.255.255 Mask:255.0.0.0

Inet6 addr: fe80::20c:29ff:fe7c:4100/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:12156 errors:0 dropped:0 overruns:0 frame:0

TX packets:2700 errors:0 dropped:0 overruns:0 carrier:0

Collisions:0 txqueuelen:1000

RX bytes:1389340 (1.3 MiB) TX bytes:901117 (879.9 KiB)

[root@mysql02 keepalived-1.2.24] # ifconfig eth0:0 10.219.24.30 broadcast 10.255.255.255 netmask 255.0.0.0 up

[root@mysql02 keepalived-1.2.24] # route add-host 10.219.24.30 dev eth0:0

[root@mysql02 keepalived-1.2.24] # ifconfig

Eth0 Link encap:Ethernet HWaddr 00:0C:29:7C:41:00

Inet addr:10.219.24.25 Bcast:10.255.255.255 Mask:255.0.0.0

Inet6 addr: fe80::20c:29ff:fe7c:4100/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:12397 errors:0 dropped:0 overruns:0 frame:0

TX packets:2748 errors:0 dropped:0 overruns:0 carrier:0

Collisions:0 txqueuelen:1000

RX bytes:1405326 (1.3 MiB) TX bytes:906805 (885.5 KiB)

Eth0:0 Link encap:Ethernet HWaddr 00:0C:29:7C:41:00

Inet addr:10.219.24.30 Bcast:10.255.255.255 Mask:255.0.0.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Add:

Delete virtual ip command: ip addr del 10.219.24.30 dev eth0 and route del-host 10.219.24.30 dev eth0:0

_

9. Keepalived.conf configuration on master1 machine. (the load balancing feature of lvs is not used in the following configuration, so there is no need to configure the virtual server virtual server)

[root@mysql02 keepalived-1.2.24] # cp / etc/keepalived/keepalived.conf / etc/keepalived/keepalived.conf.bak

-- clear default content

[root@mysql02 keepalived-1.2.24] # > / etc/keepalived/keepalived.conf

-- in the following configuration

[root@mysql02 keepalived-1.2.24] # cat / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Global_defs {

Router_id MYSQL-MASTER-HA

}

Vrrp_script chk_mysql_port {

Script "/ data/chk_mysql.sh" # mysql Health check script

Interval 2 # script execution interval, detected every 2s

}

Vrrp_instance VI_2 {

State BACKUP

Interface eth0 # specify the network card interface of the virtual ip

Virtual_router_id 54 # Router identity, MASTER and BACKUP must be consistent

Priority 100

Advert_int 1

Nopreempt

Authentication {

Auth_type PASS

Auth_pass 1111

}

Virtual_ipaddress {

10.219.24.30 # vip

}

Track_script {

Chk_mysql_port

}

}

10. Keepalived configuration on master2 machine.

The keepalived.conf file on the master2 machine only modifies the priority to 90, nopreempt is not set, and real_server sets the local IP.

[root@mysql01 keepalived-1.2.24] # cp / etc/keepalived/keepalived.conf / etc/keepalived/keepalived.conf.bak

[root@mysql01 keepalived-1.2.24] # > / etc/keepalived/keepalived.conf

[root@mysql01 keepalived-1.2.24] # vim / etc/keepalived/keepalived.conf

[root@mysql01 keepalived-1.2.24] # cat / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Global_defs {

Router_id MYSQL-MASTER-HA

}

Vrrp_script chk_mysql_port {

Script "/ data/chk_mysql.sh"

Interval 2

}

Vrrp_instance VI_2 {

State BACKUP

Interface eth0

Virtual_router_id 54

Priority 90

Advert_int 1

Nopreempt

Authentication {

Auth_type PASS

Auth_pass 1111

}

Virtual_ipaddress {

10.219.24.30

}

Track_script {

Chk_mysql_port

}

}

11. Write a switching script. (both master1 and master2 operate)

KeepAlived does a heartbeat test, and if Master's MySQL service fails (port 3306 does), it will choose to commit suicide. Slave's KeepAlived detects this through a heartbeat test and takes over VIP's request.

-- check whether nc can be used properly.

[root@mysql02 keepalived-1.2.24] # nc

-bash: nc: command not found

-- NC package is not installed, yum is installed

[root@mysql02 keepalived-1.2.24] # yum-y install nc

[root@mysql02 keepalived-1.2.24] # vim / data/chk_mysql.sh

[root@mysql02 ~] # cat / data/chk_mysql.sh

#! / bin/bash

# check mysql server status

# mysql Port

PORTS= "3306"

Function check_ports {

For port in $PORTS;do

Nc-z 127.0.0.1$ port | grep-Q succeeded

["${PIPESTATUS [1]}"-eq 0] & & mark=$ {mark} 1

Done

# if the mark value is empty, the port is impassable.

# if mark is equal to 1, a port is open.

Echo $mark

}

Ret1=$ (check_ports)

# if the mysql port is not available, an attempt will be made to restart mysql once

If ["$ret1"! = 1]; then

Service mysql stop

Mysqld_safe-- defaults-file=/etc/my.cnf &

Q! # useless command, just to jump out of the above command

Sleep 1

Ret2=$ (check_ports)

# if the port is still blocked, which means that the mysql service is abnormal, then stop keepalived and cause the VIP to switch.

["$ret2"! = 1] & & / etc/init.d/keepalived stop

Fi

12. Script empowerment

[root@mysql02 keepalived-1.2.24] # chmod 755 / data/chk_mysql.sh

13. Master1 and master2 start the keepalived process

-- master1

[root@mysql02 keepalived-1.2.24] # service keepalived start

-- master2

[root@mysql01 keepalived-1.2.24] # service keepalived start

14. Testing

14.1 check the vip situation and find that vip is only on master1, tcpdump grabs packets, and only master1 10.219.24.25 multicast packets are found, which is also in line with VRRP working mechanism.

-- master1

[root@mysql02 keepalived-1.2.24] # ip al

1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:7c:41:00 brd ff:ff:ff:ff:ff:ff

Inet 10.219.24.25/8 brd 10.255.255.255 scope global eth0

Inet 10.219.24.30/32 scope global eth0

Inet6 fe80::20c:29ff:fe7c:4100/64 scope link

Valid_lft forever preferred_lft forever

[root@mysql02 keepalived-1.2.24] # tcpdump-n 'host 224.0.0.18'

Tcpdump: verbose output suppressed, use-v or-vv for full protocol decode

Listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

00RV 58 IP 35.883861 IP 10.219.24.25 > 224.0.0.18: VRRPv2, Advertisement, vrid 54, prio 100, authtype simple, intvl 1s, length 20

00RV 58 IP 36.885543 IP 10.219.24.25 > 224.0.0.18: VRRPv2, Advertisement, vrid 54, prio 100, authtype simple, intvl 1s, length 20

-- master2

[root@mysql01 keepalived-1.2.24] # ip al

1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:44:29:7a brd ff:ff:ff:ff:ff:ff

Inet 10.219.24.22/8 brd 10.255.255.255 scope global eth0

Inet6 fe80::20c:29ff:fe44:297a/64 scope link

Valid_lft forever preferred_lft forever

14.2 keepalived was closed in master1 and vip was found floating on master2.

-- master1

[root@mysql02 keepalived-1.2.24] # service keepalived stop

Stopping keepalived: [OK]

-- master2

[root@mysql01 keepalived-1.2.24] # ip al

1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:44:29:7a brd ff:ff:ff:ff:ff:ff

Inet 10.219.24.22/8 brd 10.255.255.255 scope global eth0

Inet 10.219.24.30/32 scope global eth0

Inet6 fe80::20c:29ff:fe44:297a/64 scope link

Valid_lft forever preferred_lft forever

14.3 when the previously closed keepalived is turned on, it is found that the vip has not floated back, which coincides with the non-preemptive active / standby mode.

[root@mysql02 keepalived-1.2.24] # service keepalived start

Starting keepalived: [OK]

[root@mysql02 keepalived-1.2.24] # ip al

1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:7c:41:00 brd ff:ff:ff:ff:ff:ff

Inet 10.219.24.25/8 brd 10.255.255.255 scope global eth0

Inet6 fe80::20c:29ff:fe7c:4100/64 scope link

Valid_lft forever preferred_lft forever

14.4 close the mysql on the master1, and find that the keepalived process on the master1 is also kill, and the vip floats back to the master2 successfully.

-- master1

[root@mysql02 keepalived-1.2.24] # ip al

1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:7c:41:00 brd ff:ff:ff:ff:ff:ff

Inet 10.219.24.25/8 brd 10.255.255.255 scope global eth0

Inet6 fe80::20c:29ff:fe7c:4100/64 scope link

Valid_lft forever preferred_lft forever

-- master2

[root@mysql01 keepalived-1.2.24] # ip al

1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:44:29:7a brd ff:ff:ff:ff:ff:ff

Inet 10.219.24.22/8 brd 10.255.255.255 scope global eth0

Inet 10.219.24.30/32 scope global eth0

Inet6 fe80::20c:29ff:fe44:297a/64 scope link

Valid_lft forever preferred_lft forever

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report