MHA能够在10~30秒内实现自动故障检测和故障转移,适用于对高可用性,数据完整性要求较高的场合。要做到无缝切换,还需要依赖于VI
MHA能够在10~30秒内实现自动故障检测和故障转移,适用于对高可用性,数据完整性要求较高的场合。要做到无缝切换,还需要依赖于VIP漂移。VIP漂移比较常用的方式为使用keepalived或者使用脚本直接实现。脚本方式无须安装及复杂配置,,相对简单。本文描述了基于脚本实现VIP切换。
对于keepalived的相关配置可以参考:CentOS 5.9下安装配置Keepalived
1、当前主机环境及MHA配置
[root@vdbsrv1 ~]# more /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.1.6 vdbsrv1 #master
192.168.1.7 vdbsrv2 #slave1
192.168.1.8 vdbsrv3 #slave2
192.168.1.12 vdbsrv4 #manager
###os环境
[root@vdbsrv4 ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
###mysql环境
[root@vdbsrv4 ~]# mysql -e "show variables like 'version'"
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.22-log |
+---------------+------------+
[root@vdbsrv4 ~]# masterha_manager --version
masterha_manager version 0.56.
###MHA配置信息
[root@vdbsrv4 ~]$ more /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
user=mha
password=xxx
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=1
shutdown_script=""
master_ip_online_change_script=""
report_script=""
master_ip_failover_script=/tmp/master_ip_failover
[server1]
hostname=vdbsrv1
master_binlog_dir=/data/mysqldata
[server2]
hostname=vdbsrv2
master_binlog_dir=/data/mysqldata
[server3]
hostname=vdbsrv3
master_binlog_dir=/data/mysqldata/
#candidate_master=1
2、测试VIP切换
###测试VIP(192.168.1.13)是否被启用
[root@vdbsrv4 ~]# ping 192.168.1.13
PING 192.168.1.13 (192.168.1.13) 56(84) bytes of data.
From 192.168.1.12 icmp_seq=10 Destination Host Unreachable
###为主机vdbsrv1添加VIP
[root@vdbsrv4 ~]# ssh vdbsrv1 "/sbin/ifconfig eth0:0 192.168.1.13 netmask 255.255.255.0 up"
###校验VIP是否成功启用
[root@vdbsrv4 ~]# ping 192.168.1.13
PING 192.168.1.13 (192.168.1.13) 56(84) bytes of data.
64 bytes from 192.168.1.13: icmp_seq=1 ttl=64 time=1.82 ms
###开启MHA
[root@vdbsrv4 ~]# masterha_manager --conf=/etc/masterha/app1.cnf &
###模拟主库宕机
[root@vdbsrv4 ~]# ssh vdbsrv1 "killall -r mysqld"
###查看管理节点日志,可以看到VIP已经漂移
[root@vdbsrv4 ~]# grep VIP /var/log/masterha/app1/manager.log
Disabling the VIP on old master: vdbsrv1
Enabling the VIP - 192.168.1.13/24 on the new master - vdbsrv2
###验证VIP是否位于节点vdbsrv2
[root@vdbsrv4 ~]# ssh vdbsrv2 "ifconfig |grep 1.13 -B1"
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:5F:B2:EB
inet addr:192.168.1.13 Bcast:192.168.1.255 Mask:255.255.255.0
######查看管理节点MHA切换日志
[root@vdbsrv4 ~]# tail /var/log/masterha/app1/manager.log
Invalidated master IP address on vdbsrv1(192.168.1.6:3306)
The latest slave vdbsrv2(192.168.1.7:3306) has all relay logs for recovery.
Selected vdbsrv2(192.168.1.7:3306) as a new master.
vdbsrv2(192.168.1.7:3306): OK: Applying all logs succeeded.
vdbsrv2(192.168.1.7:3306): OK: Activated master IP address.
vdbsrv3(192.168.1.8:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
vdbsrv3(192.168.1.8:3306): OK: Applying all logs succeeded. Slave started, replicating from vdbsrv2(192.168.1.7:3306)
vdbsrv2(192.168.1.7:3306): Resetting slave info succeeded.
Master failover to vdbsrv2(192.168.1.7:3306) completed successfully.
3、VIP切换perl脚本
[root@vdbsrv4 app1]# more /tmp/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '192.168.1.13/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor