Configuring MySQL replication for high availability
Jul 06, 2025 am 02:47 AMThe configuration steps of MySQL master-slave replication include: 1. Prepare the master-slave server environment, ensure consistent versions and network communication, configure different server-ids, and enable binary logs; 2. Create a dedicated replication account in the master library and authorize it; 3. Use mysqldump to initialize the slave library data and import it; 4. Execute the CHANGE MASTER command to start the replication thread and check the status; 5. Optionally set up automatic failover, achieve high availability with external tools, or manually switch the master library and adjust the replication relationship to ensure data consistency.
MySQL master-slave replication is a basic means to implement a highly available architecture. If configured properly, you can quickly switch to the slave library when the master library fails to ensure service continuity. The key is to ensure the stability of the replication link, ensure data consistency, and cooperate with appropriate monitoring and switching mechanisms.

1. Prepare the master-slave server environment
Before you start, you need at least two servers: one as the master, and the other or multiple as the slave. It is recommended that the MySQL versions of these two servers are consistent and the operating systems are as similar as possible to reduce compatibility issues.

- Ensure network interoperability, the master library allows slave library to access its 3306 port
-
server-id
of the master and slave server is different, which is one of the basic requirements for replication. - Turn on the binary log function of the main library because replication depends on it to record data changes
For example, add in the main library configuration file (such as /etc/my.cnf
):
server-id=1 log-bin=mysql-bin
The slave library sets a different server-id
, such as 2, and can also enable read-only mode to prevent misoperation of writing:

server-id=2 read_only=1
After modification, restart the MySQL service to enable the configuration.
2. Create a dedicated copy account
For security reasons, do not use root or other users with excessive permissions to copy. An account should be created specifically for replication and granted REPLICATION SLAVE
permissions.
Execute the following command on the main library:
CREATE USER 'repl'@'from library IP' IDENTIFIED BY 'Secure Password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'From Library IP'; FLUSH PRIVILEGES;
Be careful to replace從庫IP
and password to ensure that the user can only connect from the specified IP.
3. Initialize the slave database data
To keep the master-slave data consistent, the data from the master library must be fully imported into the slave library. You can use mysqldump
to export the main library data:
mysqldump -u root -p --all-databases --master-data > dump.sql
Then transfer this dump file to the slave library and import:
mysql -u root -p < dump.sql
After the import is completed, the data status of the slave library is consistent with the export of the master library.
4. Start the replication process
Execute the following SQL command on the slave library, connect to the main library and start the copy thread:
CHANGE MASTER TO MASTER_HOST='Main Library IP', MASTER_USER='repl', MASTER_PASSWORD='Password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 4; START SLAVE;
The values ??of MASTER_LOG_FILE
and MASTER_LOG_POS
come from the CHANGE MASTER statement in the dump file generated when you execute mysqldump
.
You can use the following command to check whether the copy status is normal:
SHOW SLAVE STATUS\G
Focus on whether Slave_IO_Running
and Slave_SQL_Running
are both Yes.
5. Set up automatic failover (optional)
MySQL's own replication mechanism does not support automatic failover itself, and needs to be implemented with external tools or middleware, such as MHA (Master High Availability), Orchestrator, or using high availability solutions provided by cloud vendors.
If you just switch manually, you can after the main library goes down:
- Check the copy status of the slave library and confirm that you have caught up with the main library
- Point the application to the new master library (original slave library)
- You can restore the old master library as the slave library of the new master library
This step needs to be handled with caution, otherwise it is easy to cause data inconsistency.
Basically these steps. Although the configuration process is not complicated, every detail is critical, especially in terms of network permissions, log location and security of replication accounts. A slight negligence may lead to replication failure or security risks.
The above is the detailed content of Configuring MySQL replication for high availability. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Introduction to how to implement load balancing and high availability in FastAPI: With the development of Internet applications, the requirements for system load balancing and high availability are getting higher and higher. FastAPI is a high-performance Python-based web framework that provides a simple and powerful way to build, deploy and scale web applications. This article will introduce how to implement load balancing and high availability in FastAPI and provide corresponding code examples. Using Nginx to achieve load balancingNginx is a popular

Building a high-availability load balancing system: Best practices for NginxProxyManager Introduction: In the development of Internet applications, the load balancing system is one of the essential components. It can achieve high concurrency and high availability services by distributing requests to multiple servers. NginxProxyManager is a commonly used load balancing software. This article will introduce how to use NginxProxyManager to build a high-availability load balancing system and provide

Introduction to Webman Configuration Guide for Implementing High Availability of Websites: In today's digital era, websites have become one of the important business channels for enterprises. In order to ensure the business continuity and user experience of enterprises and ensure that the website is always available, high availability has become a core requirement. Webman is a powerful web server management tool that provides a series of configuration options and functions that can help us achieve a high-availability website architecture. This article will introduce some Webman configuration guides and code examples to help you achieve the high performance of your website.

High Availability and Disaster Recovery Solution of Nginx Load Balancing Solution With the rapid development of the Internet, the high availability of Web services has become a key requirement. In order to achieve high availability and disaster tolerance, Nginx has always been one of the most commonly used and reliable load balancers. In this article, we will introduce Nginx’s high availability and disaster recovery solutions and provide specific code examples. High availability of Nginx is mainly achieved through the use of multiple servers. As a load balancer, Nginx can distribute traffic to multiple backend servers to

With the advent of the Internet era, message queue systems have become more and more important. It enables asynchronous operations between different applications, reduces coupling, and improves scalability, thereby improving the performance and user experience of the entire system. In the message queuing system, RabbitMQ is a powerful open source message queuing software. It supports a variety of message protocols and is widely used in financial transactions, e-commerce, online games and other fields. In practical applications, it is often necessary to integrate RabbitMQ with other systems. This article will introduce how to use sw

How to use Workerman to build a high-availability load balancing system requires specific code examples. In the field of modern technology, with the rapid development of the Internet, more and more websites and applications need to handle a large number of concurrent requests. In order to achieve high availability and high performance, the load balancing system has become one of the essential components. This article will introduce how to use the PHP open source framework Workerman to build a high-availability load balancing system and provide specific code examples. 1. Introduction to Workerman Worke

With the development of web applications, more and more attention is turning to how to improve application performance. The role of caching is to offset high traffic and busy loads and improve the performance and scalability of web applications. In a distributed environment, how to implement high-availability caching has become an important technology. This article will introduce how to use some tools and frameworks provided by go-zero to implement high-availability distributed cache, and briefly discuss the advantages and limitations of go-zero in practical applications. 1. What is go-

MySQL is a very popular relational database management system with good performance and stability. It is a database software widely used by many enterprises and organizations. In MySQL, data replication is a very important feature, which allows data to be synchronized between multiple database servers to ensure data security and reliability. Mirroring techniques for setting up MySQL data replication is the topic of this article. The basic concept of MySQL data replication. In MySQL, data replication refers to copying data in a MySQL instance.
