Detailed explanation of MySQL cluster construction

MySQL Cluster is a highly practical, scalable, high-performance, and highly redundant version of MySQL suitable for distributed computing environments. The original intention of its R&D and design is to meet the most stringent application requirements in many industries. These applications often require the database to run The reliability should reach 99.999%. MySQL Cluster allows the deployment of "in-memory" database clusters in a shared-nothing system. Through a shared-nothing architecture, the system can use inexpensive hardware and has no special requirements for hardware or software. Also, since each component has its own memory and disk, there is no single point of failure.

In fact, MySQL Cluster integrates an in-memory cluster storage engine called NDB with a standard MySQL server. It consists of a set of computers, each running one or more processes, which may include a MySQL server, a data node, a management server, and a proprietary data access program.

MySQL Cluster can configure the NDB storage engine with various failover and load balancing options, but it is easiest to do this on the storage engine at the Cluster level. The following is the structure diagram of MySQL cluster,

 

 

In terms of structure, MySQL consists of three types of nodes (computers or processes), namely:

Management node: used to provide configuration, management, arbitration and other functions to other nodes in the entire cluster. In theory, it can be provided by a server.

Data node: The core of MySQL Cluster, which stores data, logs, and provides various management services for data. When there are more than 2 nodes, the high availability of the cluster can be guaranteed. When the number of DB nodes increases, the processing speed of the cluster will slow down.

SQL node (API): used to access MySQL Cluster data and provide external application services. Adding API nodes will improve the concurrent access speed and overall throughput of the entire cluster. The nodes can be deployed on Web application servers, dedicated servers, or on the same server as DB.

NDB engine

MySQL Cluster uses a dedicated memory-based storage engine, the NDB engine, which has the advantage of being fast, without the bottleneck of disk I/O, but because it is memory-based, the size of the database is limited by the total system memory , If the MySQL server running NDB must have enough memory, such as 4G, 8G, or even 16G. The NDB engine is distributed. It can be configured on multiple servers to achieve data reliability and scalability. In theory, by configuring two NDB storage nodes, the redundancy of the entire database cluster can be achieved and the single point of failure problem can be solved. .

defect

  • Based on memory, the size of the database is limited by the size of the total cluster memory
  • Based on memory, data may be lost after power failure, which needs to be verified by testing.
  • Multiple nodes implement operations such as communication, data synchronization, and query through the network, so the integrity is affected by the network speed, so the speed is relatively slow

2.2 Advantages

  • Multiple nodes can be distributed in different geographical locations, so it is also a solution to implement a distributed database.
  • The scalability is very good, and the expansion of the database cluster can be realized by adding nodes.
  • The redundancy is very good, and there are complete database data on multiple nodes, so the failure of any node will not cause service interruption.

The cost of implementing high availability is relatively low. Unlike traditional high availability solutions, which require shared storage devices and dedicated software, NDB can be implemented as long as there is enough memory.

This article will build a minimal MySQL Cluster system. All commands in the configuration method are run as root account. This MySQL Cluster includes one management node, two data nodes, and two SQL nodes. These five nodes will be installed on five virtual machines respectively. The names and IPs of the virtual machines are as follows:

1. Public configuration

Please configure the configuration items here on each of the three virtual machines.

1. Install the virtual machine

The virtual machine operating system installs the x86_64 version of CentOS 6.4, uses the NAT network, and also installs vmware-tools. The specific installation method is not described in detail here.

2. Copy mysql cluster

Download the following versions of MySQL-Cluster:

http://cdn.mysql.com/Downloads/MySQL-Cluster-7.3/mysql-cluster-gpl-7.3.4-linux-glibc2.5-x86_64.tar.gz

Copy the downloaded compressed package to the /root/Downloads directory of the virtual machine, and then run the following command in the shell:

  1. cd /root/Downloads 
  2.  
  3. tar -xvzf mysql-cluster-gpl-7.3.4-linux-glibc2.5-x86_64.tar.gz 
  4.  
  5. mv mysql-cluster-gpl-7.3.4-linux-glibc2.5-x86_64 /usr/local/mysql 

3. Turn off the security policy

Close the iptables firewall (or open ports 1186 and 3306 of the firewall) and run the following commands in the shell:

  1. chkconfig --level 35 iptables off 

To turn off SELinux, run the following command in the shell:

  1. gedit /etc/selinux/config 

Change the SELINUX item in the config file to disabled, and the content of the modified config file is as follows:

  1. # This file controls the state of SELinux on the system. 
  2.  
  3. # SELINUX= can take one of these three values
  4.  
  5. # enforcing - SELinux security policy is enforced. 
  6.  
  7. # permissive - SELinux prints warnings insteadof enforcing.  
  8.  
  9. # disabled - No SELinux policy is loaded. 
  10.  
  11. SELINUX=disabled 
  12.  
  13. # SELINUXTYPE= can take one of these two values
  14.  
  15. # targeted - Targeted processes are protected, 
  16.  
  17. # mls - Multi Level Security protection. 
  18.  
  19. SELINUXTYPE=targeted 

Finally reboot the system

 

2. Configuration management node (192.168.124.141)

1. Configure the config.ini configuration file

Run the following commands in a shell:

  1. mkdir /var/lib/mysql-cluster 
  2.  
  3. cd /var/lib/mysql-cluster 
  4.  
  5. gedit config.ini 

The content of the configuration file config.ini is as follows:

  1. [ndbd default
  2.  
  3. NoOfReplicas=2 
  4.  
  5. DataMemory = 80M 
  6.  
  7. IndexMemory=18M 
  8.  
  9. [ndb_mgmd] 
  10.  
  11. NodeId=1 
  12.  
  13. hostname=192.168.124.141 
  14.  
  15. datadir=/var/lib/mysql-cluster 
  16.  
  17. [ndbd] 
  18.  
  19. NodeId=2 
  20.  
  21. hostname=192.168.124.142 
  22.  
  23. datadir=/usr/local/mysql/data 
  24.  
  25. [ndbd] 
  26.  
  27. NodeId=3 
  28.  
  29. hostname=192.168.124.143 
  30.  
  31. datadir=/usr/local/mysql/data 
  32.  
  33. [mysqld] 
  34.  
  35. NodeId=4 
  36.  
  37. hostname=192.168.124.144 
  38.  
  39. [mysqld] 
  40.  
  41. NodeId=5 
  42.  
  43. hostname=192.168.124.145 

2. Install the management node

To install the management node, the mysqld binary file is not required, only the MySQL Cluster server program (ndb_mgmd) and the listening client program (ndb_mgm) are required. Run the following commands in a shell:

  1. cp /usr/local/mysql/bin/ndb_mgm* /usr/local/bin 
  2.  
  3. cd /usr/local/bin 
  4.  
  5. chmod +x ndb_mgm* 

3. Configure data nodes (192.168.124.142, 192.168.124.143)

1. Add mysql group and user

Run the following commands in a shell:

  1. groupadd mysql 
  2.  
  3. useradd -g mysql mysql 

2. Configure the my.cnf configuration file

Run the following commands in a shell:

  1. gedit /etc/my.cnf 

The content of the configuration file my.cnf is as follows:

  1. [mysqld] 
  2.  
  3. basedir=/usr/local/mysql 
  4.  
  5. datadir=/usr/local/mysql/data 
  6.  
  7. socket=/usr/local/mysql/sock/mysql.sock 
  8.  
  9. user=mysql 
  10.  
  11. # Disabling symbolic-links is recommended to prevent assorted security risks 
  12.  
  13. symbolic-links=0 
  14.  
  15. [mysqld_safe] 
  16.  
  17. log-error=/var/log/mysqld.log 
  18.  
  19. pid-file=/var/run/mysqld/mysqld.pid 
  20.  
  21. [mysql_cluster] 
  22.  
  23. ndb-connectstring=192.168.124.141 

3. Create the system database

Run the following commands in a shell:

  1. cd /usr/local/mysql 
  2.  
  3. mkdir sock 
  4.  
  5. scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 

4. Set the data directory

Run the following commands in a shell:

  1. chown -R root . 
  2.  
  3. chown -R mysql.mysql /usr/local/mysql/data 
  4.  
  5. chown -R mysql.mysql /usr/local/mysql/sock 
  6.  
  7. chgrp -R mysql . 

5. Configure MySQL service

Run the following commands in a shell:

  1. cp support-files/mysql.server /etc/rc.d/init.d/ 
  2.  
  3. chmod +x /etc/rc.d/init.d/mysql.server 
  4.  
  5. chkconfig --add mysql.server 

 

Fourth, configure SQL nodes (192.168.124.144, 192.168.124.145)

1. Add mysql group and user

Run the following commands in a shell:

  1. groupadd mysql 
  2.  
  3. useradd -g mysql mysql 

2. Configure the my.cnf configuration file

Run the following commands in a shell:

gedit /etc/my.cnf

The content of the configuration file my.cnf is as follows:

  1. [client] 
  2.  
  3. socket=/usr/local/mysql/sock/mysql.sock 
  4.  
  5. [mysqld] 
  6.  
  7. ndbcluster 
  8.  
  9. datadir=/usr/local/mysql/data 
  10.  
  11. socket=/usr/local/mysql/sock/mysql.sock 
  12.  
  13. ndb-connectstring=192.168.124.141 
  14.  
  15. [mysql_cluster] 
  16.  
  17. ndb-connectstring=192.168.124.141 

3. Create the system database

Run the following commands in a shell:

  1. cd /usr/local/mysql 
  2.  
  3. mkdir sock 
  4.  
  5. scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 

4. Set the data directory

Run the following commands in a shell:

  1. chown -R root . 
  2.  
  3. chown -R mysql.mysql /usr/local/mysql/data 
  4.  
  5. chown -R mysql.mysql /usr/local/mysql/sock 
  6.  
  7. chgrp -R mysql . 

5. Configure MySQL service

Run the following commands in a shell:

  1. cp support-files/mysql.server /etc/rc.d/init.d/ 
  2.  
  3. chmod +x /etc/rc.d/init.d/mysql.server 
  4.  
  5. chkconfig --add mysql.server 

5. Start the Cluster environment

Note the startup order: first the management node, then the data node, and finally the SQL node.

1. Start the management node

Run the following commands in a shell:

  1. ndb_mgmd -f /var/lib/mysql-cluster/config.ini 

You can also use ndb_mgm to listen to the client, as follows:

ndb_mgm

2. Start the data node

When starting for the first time, you need to add the --initial parameter to initialize the NDB node. In the subsequent startup process, this parameter cannot be added, otherwise the ndbd program will clear all previously created data files and log files for recovery.

  1. /usr/local/mysql/bin/ndbd --initial 

If it is not the first boot, execute the following command.

  1. /usr/local/mysql/bin/ndbd 

3. Start the SQL node

If the MySQL service is not running, run the following command in the shell:

  1. /usr/local/mysql/bin/mysqld_safe --user=mysql & 

4. Start the test

Check the management node and start successfully

 

6. Cluster test

1. Test one

Now we create a related database on one of the SQL nodes, and then go to another SQL node to see if the data is synchronized.

Execute on SQL node 1 (192.168.124.144):

  1. shell> /usr/local/mysql/bin/mysql -u root -p 
  2.  
  3. mysql>show databases; 
  4.  
  5. mysql>createdatabase aa;  
  6.  
  7. mysql>use aa; 
  8.  
  9. mysql> CREATE TABLE  ctest2 (i  INT ) ENGINE=NDB; //The engine of the database table must be specified as NDB, otherwise the synchronization will fail  
  10.  
  11. mysql> INSERTINTO ctest2 () VALUES (1);  
  12.  
  13. mysql> SELECT * FROM ctest2; 

Then check whether the data is synchronized on SQL node 2

After testing, data created on a non-master can be synchronized to the master

Check whether the engine of the table is NDB, >show create table table name;

2. Test 2

Close one data node, write input on another node, open the closed node, and see if the data is synchronized.

First restart data node 1, then add data to node 2

The operation on SQL node 2 (192.168.124.145) is as follows:

  1. mysql> createdatabase bb;  
  2.  
  3. mysql> use bb; 
  4.  
  5. mysql> CREATETABLE ctest3 (i INT) ENGINE=NDB;  
  6.  
  7. mysql> use aa; 
  8.  
  9. mysql> INSERTINTO ctest2 () VALUES (3333);  
  10.  
  11. mysql> SELECT * FROM ctest2; 

After data node 1 is started, start the service of data node 1

  1. #/usr/local/mysql/bin/ndbd --initial#service mysqld start 

Then log in to view the data

  1. # /usr/local/mysql/bin/mysql -u root –p 

It can be seen that the data has been synchronized, indicating that the data can be synchronized in both directions.

Seven, close the cluster

1. Shut down the management node and data node, just execute in the management node (ClusterMgm--134):

  1. shell> /usr/local/mysql/bin/ndb_mgm -e shutdown 

show

  1. Connected to Management Server at: localhost:1186 
  2.  
  3. 2 NDB Cluster node(s) have shutdown. 
  4.  
  5. Disconnecting to allow management server to shutdown. 

2. Then close the Sql node (135, 136) and run it in two nodes respectively:

shell> /etc/init.d/mysql.server stop

Shutting down MySQL... SUCCESS!

Note: To start the cluster again, just follow the startup steps in Part 5, but do not add the "-initial" parameter when starting the data node this time.
 

【Editor's Choice】

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326286999&siteId=291194637