1. Prepare the cluster setting environment
Six virtual machines are used to build a MySQL distributed cluster. The corresponding relationship between the corresponding experimental environment and the corresponding MySQL node is shown in the following figure:
Management Node (MGM) : This type of node is used to manage other nodes in MySQLCluster, such as providing configuration data, stopping nodes, running backups, etc. Since such nodes are responsible for managing the configuration of other nodes, you should start them before starting other nodes. The MGM node is started with the command “ndb_mgmd”.
Data node (NDB) : This type of node is used to store the data of the Cluster. The number of data nodes is related to the number of copies and is a multiple of the fragment. For example, for two replicas, each of which has two fragments, there are four data nodes. There is no need to set up too many replicas. In NDB, data is kept in memory as much as possible. The data node is started using the command “NDB”.
SQL node: This is the node used to access Cluster data. For MySQL Cluster, the client node is a traditional MySQL server using the NDB Cluster storage engine. Typically, SQL nodes are started using the command “mysqld-ndbcluster”;
2. Prepare the installation package
Download the mysql installation package mysql-cluster-gPL-7.4.11-linux-glibc2.5-x86_64.tar. gz from the official website and decompress it.
3. Cluster building process
1] Install mysql in /usr/local/mysql.
Mysql > install db.sh mysql > install db.sh mysql > install db.sh mysql > install db.sh Run the./mysql-install-db.sh –user=root–basedir =/usr/local/mysql –datadir=/usr/local/mysql; Note The name of user root must be the same as that in the configuration file my.cnf.
Run the preceding installation commands on the management node, data node, and SQL node to install mysql.
4. Cluster configuration and startup
1] Configure the cluster on the management node: Configure the following information in /var/lib/mysql-cluster/config.ini:
2] In the data node, you need to complete the configuration information of the data node in my.cnf as follows:
You need to specify the root directory, data directory, socket connection configuration, user configuration, and corresponding management node IP address configuration of the data node. Move the configuration file to /etc/my.cnf.
CNF: /etc/my.cnf: /etc/my.cnf: /etc/my.cnf: /etc/my.cnf: /etc/my.cnf
After the above configuration is complete, you can start each node in the cluster.
5. The cluster starts
When starting the mysql cluster, start the management node first, and then start the other nodes as follows:
1] On the management node, go to the /usr/local/mysql.bin directory and run the ndb_mgmd -f /var/lib/mysql-cluster/config.ini command to start the management node.
2] Initial = /usr/local/mysql/bin;
3] Run the mysqLD_safe –user=root command to switch to the /usr/local/mysql-bin directory on each SQL node.
4] Run the ndb_mgm command on the management node to go to the database management client. Run the show command to check the status of each node connected to the database.
5] Enter the system security state on the SQL node and complete the password change for root user. Run the following command to change the password:
Mysql > select * from mysql;
B UPDATE user SET Password = PASSWORD(‘123456’)WHERE user = ‘root’; To achieve the root password change;
C Flush privilege.
Mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql >
Grant all on ‘*.*’ to ‘root@’%’ identified by ‘123456’;
This allows any host to log in to mysql as user root.
6. Cluster testing
Create a database on an SQL node on the cluster, create a table, and complete the corresponding data insert as follows:
A create database ctest; // Create database
B create table test (
Id int primarykey; // Create a table
);
C insert into test (id)values(1); // Data is inserted
Log in to another SQL node and perform a SQL query to see if there is already data in the database as follows:
select * from ctest;
If there is data, data is successfully inserted.
7. Shut down the cluster
1] shutdown the management node and data node. Run the following command on the management node:./ndb_mgm -e shutdown;
2] Then close the SQL node and run the /usr/local/mysql.support-fies /mysql.server stop command on the SQL node (where /usr/local/mysql.is the mysql installation directory). To close the SQL node;
Here I recommend an architecture learning exchange group. Exchange learning group number: 744642380, which will share some senior architects recorded video: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, microservice architecture principle, JVM performance optimization, distributed architecture and so on to become the architect of the necessary knowledge system. Can also receive free learning resources, currently benefit from good