MySQL is a common relational database management system commonly used for data storage and management of web applications. As data volume and concurrent access continue to increase, the performance of a single MySQL instance may not meet the needs of the application. Therefore, MySQL Cluster has become a common solution. This article will introduce how to build a MySQL cluster.
Step one: Prepare the environment
Before starting to build the MySQL cluster, we need to prepare some environments and tools:
1. At least 3 servers, one of which As the management node, the remaining servers serve as MySQL nodes.
2. Ensure network communication between servers.
3. Install MySQL server and MySQL cluster software.
In this article, we will use Ubuntu 18.04 LTS operating system and MySQL Cluster version 7.6.21 for demonstration.
Step 2: Create a management node
The management node is used to manage the entire MySQL cluster, including creating and deleting MySQL nodes, routing query requests, etc. On this node, we need to install two software packages: MySQL Cluster Manager and MySQL Server.
1. Install MySQL Cluster Manager
MySQL Cluster Manager is a command line tool for automatically creating and configuring MySQL clusters. Install through the following command:
sudo apt-get update
sudo apt-get install mysql-cluster-manager
2. Install MySQL Server
on the management node , we need to install MySQL Server so that MySQL Cluster Manager can manage MySQL Cluster. Install through the following command:
sudo apt-get install mysql-server
3. Create MySQL Cluster configuration
On the management node, create the MySQL Cluster configuration file and specify Node address and port number. In this example, we assume that the management node's IP address is 192.168.1.100 and the port number is 1186.
Create a file named mycluster.ini and specify the node address and port number:
[NDB_MGMD DEFAULT]
PortNumber=1186
[NDB_MGMD]
HostName=192.168.1.100
[MYSQLD DEFAULT]
[NDBD DEFAULT]
[NDBD]
HostName=192.168.1.101
[NDBD]
HostName=192.168.1.102
4. Start the management node
Start the management node through the following command:
ndb_mgmd -f mycluster.ini --configdir=/var/lib/mysql-cluster
At this time, MySQL Cluster Manager will automatically create and configure the MySQL cluster. You can check the status of the MySQL cluster using the following command:
ndb_mgm
show
Step 3: Create a MySQL node
MySQL node is the one that actually stores and manages data server. In this example, we will create two MySQL nodes.
1. Install MySQL Cluster software
Before installing the MySQL Cluster software, make sure you have the necessary dependencies and libraries installed on your server. You can install it using the following command:
sudo apt-get install libaio1 libmecab2
Then download the MySQL Cluster binary package:
wget https://dev.mysql.com/get /Downloads/MySQL-Cluster-7.6/mysql-cluster_7.6.21-1ubuntu18.04_amd64.deb-bundle.tar
Unzip the downloaded package:
tar -xvf mysql-cluster_7.6.21- 1ubuntu18.04_amd64.deb-bundle.tar
Enter the decompressed directory:
cd mysql-cluster_7.6.21-1ubuntu18.04_amd64.deb-bundle
Execute the following command Install MySQL Cluster:
sudo dpkg -i mysql-common_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-client_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-server_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-client-core_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-server- core_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-community-client_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-community-server_7.6.21-1ubuntu18. 04_amd64.deb
2. Configure MySQL node
On the MySQL node, we need to modify the MySQL configuration file my.cnf and specify the management node and port number corresponding to the node.
Open the my.cnf file:
sudo nano /etc/mysql/my.cnf
At the end of the file, add the following configuration:
[mysqld ]
ndbcluster
ndb-connectstring=192.168.1.100
3. Start the MySQL node
Start the MySQL node through the following command:
sudo systemctl start mysql
4. Join the MySQL cluster
After the MySQL node is started, the MySQL Cluster Manager will automatically monitor and manage the MySQL node. You can check the status of your MySQL cluster using the following command:
ndb_mgm
show
At this point, you can create and manage databases on the MySQL node.
Step 4: Test MySQL Cluster
To ensure that MySQL Cluster is working properly, create a database on one of the MySQL nodes and extend its tables and data to other nodes. You can create a testdb database using the following command:
mysql -uroot -p
create database testdb;
Then, create a table for the database:
use testdb;
create table user(name VARCHAR(20), age INT);
Finally, insert some data on a node:
insert into user values('John', 18);
Then, use another MySQL node to check if the table exists:
use testdb;
select * from user;
If you can retrieve and read the data normally, then MySQL cluster has been successfully set up.
Summary
MySQL Cluster can greatly improve the performance and reliability of applications. Through the combination of management nodes and multiple MySQL nodes, automatic distribution and replication of data can be achieved, providing high availability and fault tolerance. In the above steps, we introduced how to build a MySQL cluster and conducted some tests and verifications. Depending on the specific application scenario, more configuration and optimization may be required. However, these steps can serve as a starting guide to help you get started learning and using MySQL Cluster.
The above is the detailed content of Mysql cluster construction. For more information, please follow other related articles on the PHP Chinese website!