search
HomeDatabaseMysql TutorialMySQL dual master hot standby solution based on Keepalived

MySQL dual master hot standby solution based on Keepalived

Apr 08, 2025 am 10:00 AM
mysqltooldata lost

The MySQL dual master hot standby solution based on Keepalived ensures that the database service is not interrupted. 1. Keepalived monitors database instances and switches VIP to backup library when the main library fails; 2. MySQL replication function (such as GTID) ensures synchronization and consistency of data between the two main libraries; 3. Keepalived relies on the heartbeat package to detect the status of the main node through the VRRP protocol, and switches VIPs according to configuration parameters (priority, detection interval, etc.), and requires careful configuration to avoid split brain phenomenon; 4. Scripts need to be written to monitor MySQL status, and log problems are checked in combination; 5. High availability requires selecting appropriate hardware, regular backups, reasonable monitoring, and continuous maintenance and optimization.

MySQL dual master hot standby solution based on Keepalived

MySQL dual-main hot standby solution based on Keepalived: In-depth analysis and practice

Many people want to understand that MySQL is highly available, but they always feel confused. In fact, the core sentence is just one sentence: ensure that the database service is not interrupted. The dual-main hot standby solution based on Keepalived is a powerful means to achieve this goal, but it cannot be solved by simple configuration, and there are many pitfalls in it. This article will give you an in-depth understanding and let you truly master this solution. After reading it, you can not only build it, but also deal with various emergencies.

Let’s talk about the basics first: What is Keepalived? It is a virtual IP (VIP) management tool that can monitor your database instance. Once the main library is hung, it can quickly switch VIP to the backup library to ensure that the service is not interrupted. It sounds simple, but the devil is hidden in the details. MySQL dual master hot standby means that both database servers can write data, and data synchronization is the key. This usually requires the use of MySQL replication functions, such as GTID replication, to ensure data consistency. Without data consistency, your hot backup becomes a joke.

The core lies in understanding the working mechanism of Keepalived. It mainly relies on the VRRP protocol (virtual routing redundant protocol) to realize VIP switching. Keepalived will send heartbeat packets to other Keepalived instances periodically. If the master node heartbeat is detected to be lost, it will take over the VIP and turn itself into a new master. This sounds beautiful, but in actual operation, you need to finely configure the various parameters of Keepalived, such as priority, detection interval, reservation time, etc. The wrong parameter configuration may cause the VIP switching to fail or the brain split phenomenon (both nodes think they are the master node).

Let's take a look at a simple example and feel the configuration of Keepalived:

 <code class="language-bash">vrrp_script check_mysql {</code><pre class='brush:php;toolbar:false;'> script "/usr/local/bin/check_mysql.sh"
interval 2
weight 100

}

vrrp_instance VI_1 {

 state MASTER
virtual_router_id 51
priority 100
interface eth0
virtual_ipaddress 192.168.1.100
virtual_ipaddress 192.168.1.101
track_script check_mysql

}

This configuration defines a VRRP instance named VI_1 , listening to the eth0 interface, with virtual IPs of 192.168.1.100 and 192.168.1.101 . The check_mysql.sh script is responsible for checking whether the MySQL service is running normally. The point is track_script , which allows Keepalived to monitor the state of MySQL. If MySQL is hung up, Keepalived will automatically switch VIPs according to the configuration. The check_mysql.sh script needs to be written by itself, and it usually checks whether the MySQL process is running and whether the database connection is normal.

Of course, this is just the most basic usage. Advanced usage includes: using different monitoring scripts to monitor more comprehensive metrics; configuring multiple VIPs to achieve load balancing; using other Keepalived functions, such as health checks, etc.

Let’s talk about common pitfalls. First of all, network jitter will affect Keepalived's heartbeat detection, resulting in misjudgment. Secondly, improper MySQL replication configuration may lead to inconsistent data. Again, the configuration of Keepalived itself also needs to be carefully considered. An incorrect configuration of one parameter may cause the entire system to be paralyzed. To debug these problems, you need to carefully analyze the Keepalived logs and troubleshoot them in combination with MySQL logs.

Lastly, on performance optimization and best practices. Choosing the right hardware, such as high-performance servers and network devices, can improve system stability and performance. Regular backup of databases can reduce the risk of data loss. A reasonable monitoring strategy can promptly detect potential problems. The most important thing is to have an in-depth understanding of the entire system so that you can quickly locate and solve problems when encountering them. Remember, high availability is not achieved overnight and requires continuous maintenance and optimization. Don't forget that the readability and maintainability of the code are also important. Writing clear and easy-to-understand code can save you a lot of time and energy in the future.

The above is the detailed content of MySQL dual master hot standby solution based on Keepalived. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use