With the rapid development of the Internet, the database, as an important part of back-end data storage, is crucial to the high availability and performance optimization of the website. Among them, master-slave replication is a commonly used database high availability solution, which can improve system stability and availability without affecting services.
This article mainly introduces how to use PHP to implement master-slave replication and failover.
1. The basic principle of master-slave replication
Master-slave replication refers to synchronizing all data on a master database to several slave databases. The master database can read and write data, while the slave database can only perform read operations, and the data in the slave database is completely consistent with the master database. Master-slave replication usually uses asynchronous replication. After the data on the master database changes, the newly added data log will be recorded, and then the MySQL slave thread will read the update log and apply it to the slave database.
In the master-slave replication architecture, we usually use the master library as the write server and the slave library as the read server. Because write operations generally consume more resources than read operations, write operations are placed on the main library to achieve separation of reading and writing, which reduces the pressure on the main library and improves the performance of the main library.
2. PHP implements master-slave replication
1. Configure master-slave replication
In MySQL, we can achieve master-slave replication by configuring the my.cnf file. The specific configuration steps are as follows:
1) Configure the main server
[mysqld]
log-bin=mysql-bin #Enable the binary log function and enable master-slave replication
server-id=1 #Configure a unique number to ensure that the number of each server is not repeated
2) Configure the slave server
[mysqld]
server-id=2 #Configure a unique number to ensure that the number of each server is not repeated
relay-log=mysql-relay-bin #Configure the relay log file, used to copy the data of the main library from the library
read_only=1 #Prohibit writing operations from the slave library
2. Connect the master-slave library through PHP
Connecting the master-slave library in PHP is actually not the same as connecting to a single database What a difference. We only need to specify which database to use when connecting to the database.
When performing a read operation, use the slave library to operate, and when performing a write operation, use the main library to operate. The specific code is as follows:
//Connect to the main library
$main_db = mysqli_connect("localhost", "root", "123456", "main_db");
/ /Connect to the slave library
$slave_db = mysqli_connect("localhost", "root", "654321", "slave_db");
//Read data from the slave library
$result = mysqli_query($slave_db, "SELECT * FROM user");
//Write data to the main database
mysqli_query($main_db, "INSERT INTO user(name, age) VALUES('tom', '25')");
3. Implementation of failover
When the primary database fails unexpectedly, it needs to switch to the secondary database to provide services. The failover process requires the following steps:
1. Switch from the slave library to the main library
When the main library cannot provide services, we need to switch from the slave library to the main library to make it Take over the services of the main library. The specific steps are as follows:
1) Run the command in the slave library: STOP SLAVE; to stop the replication process from the slave library.
2) Copy the data from the slave library to the main library.
3) Modify the server-id in the slave library to 1, and modify the log-bin and relay-log variables in the my.cnf file. In this way, the slave database becomes the master database and starts to provide services.
2. Modify the configuration in the application
When the main library fails over, you need to modify the configuration information in the application so that it can switch to the new main library to provide services.
3. Switching after failure recovery
When the main database resumes service, it is necessary to restore the main database to the main database and change the previous slave database to the slave database for backup. The specific steps are as follows:
1) Run the command: STOP SLAVE in the slave library to stop the replication process from the slave library.
2) Synchronize the latest data to the main database.
3) Run the command in the main library: CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_USER='root', MASTER_PASSWORD='xxxx', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 156, changes from slave library to master library.
In this way, after the fault is restored, the relationship between the master and slave libraries is established again and enters the normal working state.
4. Summary
Implementing database master-slave replication failover through PHP can improve the reliability and availability of the system, while also reducing application pressure and improving the overall performance of the system. In practice, attention needs to be paid to the failover process and application configuration to ensure that the entire switchover process proceeds smoothly and that data recovery and failure recovery of the entire system are completed in the shortest possible time.
The above is the detailed content of PHP method to implement database master-slave replication failover. For more information, please follow other related articles on the PHP Chinese website!