Home  >  Article  >  Operation and Maintenance  >  How to configure a highly available database cluster on Linux

How to configure a highly available database cluster on Linux

王林
王林Original
2023-07-06 09:18:061100browse

How to configure a high-availability database cluster on Linux

1. Introduction
With the continuous growth of enterprise data, the high availability of databases has become more and more important. Highly available database clusters can provide continuous and reliable data access to ensure continuous business operation. This article will introduce how to configure a high-availability database cluster on the Linux operating system and provide corresponding code examples.

2. Preparation work
Before you start configuring a high-availability database cluster, you first need to do some preparation work.

  1. Install the operating system: Choose a stable and reliable Linux distribution, such as CentOS, Ubuntu, etc., and install it according to the official documentation.
  2. Install database software: Choose a mature and stable database software, such as MySQL, PostgreSQL, etc., and install it according to the official documentation.
  3. Configure the network: Make sure that each node in the cluster can communicate with each other. It is recommended to use static IP to avoid IP address changes.
  4. Create database user: Create a database user specifically for cluster data synchronization, and set appropriate permissions for it.

3. Configuring the database cluster
The following introduces a common database cluster architecture-the master-slave replication mode. One node is the master node, responsible for processing read and write requests, and the other nodes are backup Node for data backup and failover.

  1. Create the master node
    First, configure the master node.

Edit the database configuration file my.cnf and find the following section:

[mysqld]
server-id=1
log-bin=mysql-bin

Set server-id to a unique value to identify the master node.

Restart the database service:

service mysql restart
  1. Create a standby node
    Next, configure the standby node.

Edit the database configuration file my.cnf and find the following section:

[mysqld]
server-id=2
log-bin=mysql-bin

Set server-id to a unique value to identify the standby node.

Restart the database service:

service mysql restart
  1. Configure primary and secondary synchronization
    Execute the following command on the primary node:
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'备节点IP' IDENTIFIED BY '密码';
FLUSH PRIVILEGES;

Replace replication_user with the actual Database user name, replace the standby node IP with the actual IP address of the standby node, and set a password.

Execute the following command on the standby node:

CHANGE MASTER TO MASTER_HOST='主节点IP', MASTER_USER='replication_user', MASTER_PASSWORD='密码', MASTER_LOG_FILE='主节点的binlog文件名', MASTER_LOG_POS=主节点的binlog文件位置;
START SLAVE;

Replace the primary node IP with the actual IP address of the primary node, replace replication_user and password with the actual database user name and password, and replace the primary node IP with the actual IP address of the primary node. Replace the binlog file name and location with actual values.

  1. Failover
    When the primary node fails, you need to manually switch to the backup node.

Execute the following command on the standby node:

STOP SLAVE;
RESET MASTER;

Edit the database configuration file my.cnf on the standby node and comment out the following lines:

# server-id=2
# log-bin=mysql-bin

Then Restart the database service:

service mysql restart

Now the standby node will become the new primary node, and other standby nodes can be configured as new standby nodes according to the same steps.

4. Summary
Through the above steps, we successfully configured a high-availability database cluster based on the active-standby replication mode, ensuring continuous and reliable access to data. I hope this article can provide some help to readers in configuring a high-availability database cluster on Linux. If you have any questions, please refer to relevant official documents or consult professionals.

The above is the detailed content of How to configure a highly available database cluster on Linux. 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