How Baidu architects build MySQL distributed cluster

1. Prepare the cluster to build the environment

Use 6 virtual machines to build a MySQL distributed cluster . The corresponding relationship between the corresponding experimental environment and the corresponding MySQL nodes is shown in the following figure:

Management node (MGM): The role of this type of node is to manage other nodes in MySQLCluster, such as providing configuration data, and stopping nodes, running backups, etc. Since such nodes are responsible for managing the configuration of other nodes, they should be started before 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 replicas and is a multiple of the fragment. For example, for two replicas, each replica has two fragments, then there are 4 data nodes, there is no need to set too many replicas, the data will be stored in memory as much as possible in NDB. The data node is started with 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. Usually, the SQL node is started with the command "mysqld-ndbcluster";

2. Prepare the installation package

Download the mysql installation package on the official website: mysql-cluster-gpl-7.4.11-Linux-glibc2.5-x86_64.tar.gz, and decompress it.

3. Cluster construction process

1] Move the files extracted from the above installation package to /usr/local/mysql;

2] Run the mysql-install-db.sh script in the script directory, and the running command is ./mysql-install-db.sh --user=root--basedir =/usr/local/mysql --datadir=/usr/ local/mysql; note that the name of the user as root needs to be the same as that in the configuration file my.cnf;

Execute the above installation command on the management node, data node, and SQL node to complete the installation of mysql;

4. Cluster configuration and startup

1] The overall configuration of the cluster needs to be completed on the management node: implement the following configuration information in /var/lib/mysql-cluster/config.ini:

2] In the data node, the relevant configuration information for the data node needs to be completed in my.cnf, as follows:

It is necessary to specify the root directory, data directory, socket connection configuration, user configuration, and ip address configuration of the corresponding management node of the configured data node; move the configured configuration file to /etc/my.cnf, complete;

3] Complete the configuration information for the SQL node on the SQL node. Similarly, complete the corresponding configuration information in my.cnf, and move the configuration file to /etc/my.cnf. The corresponding configuration information is set as follows Show:

After completing the above configuration, you can start each node in the cluster.

5. Cluster startup

When starting the mysql cluster, pay attention to start the management node first, and then start several other nodes in turn. The corresponding startup steps are as follows:

1] On the management node, switch to the /usr/local/mysql/bin directory, and execute the ndb_mgmd -f /var/lib/mysql-cluster/config.ini command to complete the startup of the management node;

2] On each data node, switch to the /usr/local/mysql/bin directory, and execute ndbd --initial (when starting for the first time, otherwise execute ndbd) to complete the startup of the data node;

3] On each SQL node, also switch to the /usr/local/mysql/bin directory, and execute mysqld_safe --user=root to complete the startup;

 

4] Run the ndb_mgm command on the management node, enter the database management client, enter the show command, and view the status of each node connected to it;

5] Enter the security state of the system on the SQL node, and complete the password modification of the root user, run the following command to modify the password:

A use mysql, switch to mysql database;

B UPDATE user SET Password = PASSWORD('123456')WHERE user = 'root'; to modify the root password;

C flush privilege, complete the modification;

6] Modify so that any host can connect to mysql, make the following modifications, and enter the safe mode in step 5 of the same installation, and complete the corresponding modifications, as follows:

grant all on‘*.*’to ‘root@'%' identified by '123456';

In this way, any host can log in to mysql through the root user;

6. Cluster test

Execute the create database on an SQL node on the cluster, create a table, and complete the corresponding data insertion, 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); //Complete data insertion

Log in to another SQL node, and execute the SQL query operation to see if there is already data in the database, as follows:

select * from ctest;

If there is data, it means that the data was inserted successfully;

7. Close the cluster

1] First shut down the management node and data node, you need to execute the command on the management node, as follows: ./ndb_mgm -e shutdown;

2] Then close the SQL node, execute the command /usr/local/mysql/support-fies/mysql.server stop on the SQL node (where /usr/local/mysql/ is the installation directory of mysql). Thus close the SQL node;

Here I recommend an architecture learning exchange group to everyone. Communication and learning group number: 744642380, which will share some video recordings recorded by senior architects: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, principles of microservice architecture, JVM performance optimization, distributed architecture Wait for these to become the necessary knowledge system for architects. You can also receive free learning resources, which are currently benefiting

Guess you like

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