Home > Article > Operation and Maintenance > How to configure a highly available distributed database on Linux
How to configure a highly available distributed database on Linux
Introduction
In today's Internet era, the amount of data and access continues to increase, which requires higher availability and performance of the database. requirements. To meet these needs, distributed databases have become a widely adopted solution. This article will introduce how to configure a highly available distributed database on Linux and provide corresponding code examples.
Step 1: Install the MySQL Cluster package
Use the following command to install the MySQL Cluster package:
$ sudo apt-get install mysql-cluster-community-server
Step 2: Create the configuration file
Create the configuration file my.cnf in the installation directory with the following content:
[ndbd default] NoOfReplicas = 2 # 设置数据复制的副本数 DataDir = /var/lib/mysql-cluster # 设置数据存储目录 [ndbd] NodeId = 1 # 设置节点ID HostName = 192.168.0.1 # 设置节点主机名 DataDir = /usr/local/mysql/data # 设置数据存储目录 [ndbd] NodeId = 2 # 设置节点ID HostName = 192.168.0.2 # 设置节点主机名 DataDir = /usr/local/mysql/data # 设置数据存储目录 [ndb_mgmd] NodeId = 3 # 设置节点ID HostName = 192.168.0.3 # 设置节点主机名 DataDir = /var/lib/mysql-cluster # 设置数据存储目录 [mysqld] NodeId = 4 # 设置节点ID HostName = 192.168.0.4 # 设置节点主机名 [mysqld] NodeId = 5 # 设置节点ID HostName = 192.168.0.5 # 设置节点主机名
Step 3: Start the ndb cluster manager
Use the following command to start the ndb cluster manager:
$ sudo ndb_mgmd -c /etc/mysql-cluster/my.cnf
Step 4: Start the data node
Use the following command to start the data node:
$ sudo ndbd
Step 5: Start the MySQL server
Use the following command to start the MySQL server:
$ sudo systemctl start mysql
Create database:
$ mysql -u root -p
Create data table:
mysql> CREATE DATABASE mydatabase; mysql> USE mydatabase; mysql> CREATE TABLE mytable (id INT PRIMARY KEY, name VARCHAR(20));
Insert data:
mysql> INSERT INTO mytable VALUES (1, 'John'), (2, 'Mike'), (3, 'Lisa');
Query data:
mysql> SELECT * FROM mytable;
Update Data:
mysql> UPDATE mytable SET name = 'Tom' WHERE id = 1;
Delete data:
mysql> DELETE FROM mytable WHERE id = 2;
Configuring a highly available distributed database is a complex process, but through correct selection and configuration, we can meet the needs of large-scale data storage and high concurrent access. On the Linux platform, we can choose a distributed database that suits our needs and follow the corresponding steps to install, configure and manage it. At the same time, we also need to use monitoring tools to monitor the status and operation of the database cluster and perform fault recovery operations. I hope the code examples provided in this article will help you configure your distributed database.
The above is the detailed content of How to configure a highly available distributed database on Linux. For more information, please follow other related articles on the PHP Chinese website!