MySQLCluster (MySQL cluster) initial test

Source: Internet
Author: User
Tags node server

MySQL Cluster is a highly practical and redundant MySQL version suitable for distributed computing environments. It uses the NDB Cluster Storage engine and allows multiple MySQL servers to run in one Cluster. This storage engine is provided in binary versions of MyQL5.0 and later, and in RPM compatible with the latest Linux version. (Note: To obtain the functions of MySQL Cluster, you must install mysql-server and mysql-max RPM ).

Currently, the operating systems that can run MySQL Cluster include Linux, Mac OS X, and Solaris (some users have reported that MySQL Cluster has been successfully run on FreeBSD, but MySQL AB does not officially support this feature ).

  I. MySQL Cluster Overview

MySQL Cluster is a technology that allows you to deploy the "in memory" Database Cluster in a non-shared system. Without a shared architecture, the system can use cheap hardware without special requirements for hardware and software. In addition, since each component has its own memory and disk, there is no single point of failure.

MySQL Cluster is composed of a group of computers, each of which runs a variety of processes, including MySQL servers, NDB Cluster data nodes, management servers, and (possibly) dedicated Data Access Program. For the relationship between these components in the Cluster, see:
  
All these nodes form a complete MySQL cluster system. The data is stored in the storage engine of the "NDB storage server", and the table (structure) is saved in the "MySQL server. Applications access these data tables through the "MySQL Server", and the cluster management server manages the "NDB storage server" through the management tool (ndb_mgmd ".


By introducing MySQL Cluster into the open source world, MySQL provides Cluster data management with high availability, high performance, and scalability for all people who need it.

  Ii. Basic concepts of MySQL Cluster

"NDB" is a "in-memory" storage engine with high availability and good data consistency.
MySQL Cluster can use a variety of failover and load balancing options to configure the NDB storage engine, but it is the easiest to do this on the storage engine at the Cluster level. The NDB storage engine of MySQL Cluster contains the complete dataset, which depends only on other data in the Cluster.

Currently, the Cluster of the MySQL Cluster can be configured independently of the MySQL server. In MySQL Cluster, each part of the Cluster is considered as one node.

Management (MGM) nodes: these nodes manage other nodes in the MySQL Cluster, such as providing configuration data, starting and stopping nodes, and running backups. Because these nodes are responsible for managing the configurations of other nodes, they should be started before other nodes are started. The MGM node is started with the command "ndb_mgmd.

Data nodes: these nodes are used to save Cluster data. The number of data nodes is related to the number of copies, which is a multiple of the segments. For example, if two copies have two segments, each of them has four data nodes. However, there is no need to set multiple copies. The data node is started with the "ndbd" command.

SQL node: This is the node used to access Cluster data. For MySQLCluster, the client node is a traditional MySQL server that uses the NDBCluster storage engine. Generally, an SQL node is started with the command "mysqld-ndbcluster", or "ndbcluster" is added to "my. cnf" and then started with "mysqld.

Note: in many cases, the term "Node" is used for computers, but when discussing MySQL Cluster, it represents a process. A single computer can have any number of nodes. Therefore, we use the term "Cluster host ".

The Management Server (MGM node) is responsible for managing Cluster configuration files and Cluster logs. Each node in the Cluster retrieves configuration data from the Management Server and requests to determine the location of the management server. When a new event occurs in the data node, the node transmits the information about the event to the management server, and then writes the information to the Cluster log.

In addition, there can be any number of Cluster client processes or applications. They are divided into two types:
Standard MySQL clients: For MySQL clusters, they are no different from standard (non-Cluster) MySQL. In other words, you can access the MySQL Cluster from existing MySQL applications written in PHP, Perl, C, C ++, Java, Python, and Ruby.

Management clients: these clients are connected to the Management Server and provide the ability to start and stop nodes, start and stop message tracing (only DEBUG Versions) displays the node version and status, starts and stops the backup, and other commands.

  3. Start the preparation phase
1. Prepare the server
Now we plan to establish a MySQL CLuster System with five nodes, so we need five machines for the following purposes:

Node (Purpose) IP address (host name)
Management node (MGM) 192.168.0.1 (db1)
SQL Node 1 (SQL1) 192.168.0.2 (db2)
SQL Node 2 (SQL2) 192.168.0.3 (db3)
Data Node 1 (NDBD1) 192.168.0.4 (db4)
Data Node 2 (NDBD2) 192.168.0.4 (db5)

2. Precautions and other

The operating system of each node is Linux. The host name is used in the following description, and IP addresses are no longer used. MySQLCluster uses TCP/IP connection and data transmission between nodes is not encrypted. Therefore, it is best to run this system only in a separate subnet, and considering the transmission rate, we strongly recommend that you do not use this system across the public network. The required MySQL software should be downloaded in http://dev.mysql.com/downloads beforehand.

In fact, the entire system can run successfully on a separate physical computer. Of course, you must set different directories and ports and use them only for testing.

  4. Start Installation

1. Assumptions
The nobody user is used on each node computer to run the Cluster. Therefore, execute the following command to add the relevant user (if the user already exists, it will be skipped and run with the root user ):

Root #/usr/sbin/groupadd nobody
Root #/usr/sbin/useradd nobody-g nobody

Assume that you have downloaded the binary installation package that mysql can use directly and put it under/tmp.
2. Install SQL nodes and storage nodes (NDB nodes) (that is, repeat the following steps on four machines)

Root # cd/tmp/
Root # tar zxf mysql-max-5.0.24-linux-i686.tar.gz
Root # mv mysql-max-5.0.24-linux-i686/usr/local/mysql/
Root # cd/usr/local/mysql/
Root #./configure -- prefix =/usr/local/mysql
Root #./scripts/mysql_install_db
Root # chown-R nobody: nobody/usr/local/mysql/

3. Configure an SQL Node

Root # vi/usr/local/mysql/my. cnf
Enter the following content:

[Mysqld]
Basedir =/usr/local/mysql/
Datadir =/usr/local/mysql/data
User = nobody
Port = 3306
Socket =/tmp/mysql. sock
Ndbcluster
Ndb-connectstring = db1
[MYSQL_CLUSTER]
Ndb-connectstring = db1

4. Configure the storage node (NDB node)
Root # vi/usr/local/mysql/my. cnf
Enter the following content:

[Mysqld]
Ndbcluster
Ndb-connectstring = db1
[MYSQL_CLUSTER]
Ndb-connectstring = db1

5. Install the management Node
Root # cd/tmp/
Root # tar zxf mysql-max-5.0.24-linux-i686.tar.gz
Root # mkdir/usr/local/mysql/
Root # mkdir/usr/local/mysql/data/
Root # mysql-max-5.0.24-linux-i686/bin/cd/
Root # cp ndb_mgm */usr/local/mysql/
Root # chown-R nobody: nobody/usr/local/mysql

6. Configuration Management Node

Root # vi/usr/local/mysql/config. ini
Enter the following content:

[Ndbd default]
NoOfReplicas = 1
[Tcp default]
Portnumber = 3306

Set Management node Server
[NDB_MGMD]
Hostname = db1
# Log storage directory on MGM
Datadir =/usr/local/mysql/data/
# Set storage node server (NDB node)
[NDBD]
Hostname = db4
Datadir =/usr/local/mysql/data/
# Second NDB Node
[NDBD]
Hostname = db5
Datadir =/usr/local/mysql/data/
# Setting an SQL node Server
[MYSQLD]
Hostname = db2
# Second SQL Node
[MYSQLD]
Hostname = db3

Note: The default port of the Cluster management node is 1186, and the default port of the data node is 2202. This restriction has been relaxed since MySQL 5.0.3, and the Cluster can automatically allocate ports to Data Nodes Based on idle ports. If your version is earlier than 5.0.22, pay attention to this details.

  V. Start MySQL Cluster

A reasonable sequence of startup is to first start the management node server, then start the storage node server, and finally start the SQL node Server:

On the Management node server, run the following command to start the MGM node process:
Root #/usr/local/mysql/ndb_mgmd-f/usr/local/mysql/config. ini

The "-f" or "-- config-file" parameter must be used to tell the location of the ndb_mgm configuration file, which is in the same directory as ndb_mgmd by default.

On each storage node server, if the ndbd process is started for the first time, you must first execute the following command:

Root #/usr/local/mysql/bin/ndbd -- initial
Note that the "-- initial" parameter should be used only when ndbd is started for the first time or when data is backed up/restored or ndbd is restarted after the configuration file changes. This parameter causes the node to delete any files created by an earlier ndbd instance for restoration, including log files for restoration.
If it is not the first time, run the following command:

Root #/usr/local/mysql/bin/ndbd
Finally, run the following command to start the SQL node Server:

Root #/usr/local/mysql/bin/mysqld_safe -- defaults-file =/usr/local/mysql/my. cnf &
If everything goes well, that is, there is no error message during the startup process, run the following command on the Management node Server:

Root #/usr/local/mysql/ndb_mgm
-- NDB Cluster -- Management Client --
Ndb_mgm> SHOW
Connected to Management Server at: localhost: 1186
Cluster Configuration
---------------------
[Ndbd (NDB)] 2 node (s)
Id = 2 @ 192.168.0.4 (Version: 5.0.22, Nodegroup: 0, Master)
Id = 3 @ 192.168.0.5 (Version: 5.0.22, Nodegroup: 0)
[Ndb_mgmd (MGM)] 1 node (s)
Id = 1 @ 192.168.0.1 (Version: 5.0.22)
[Mysqld (SQL)] 1 node (s)
Id = 2 (Version: 5.0.22)
Id = 3 (Version: 5.0.22)

The specific output content may be slightly different, depending on the MySQL version you are using.


Note: If you are using an earlier MySQL version, you may see the SQL node referenced as '[mysqld (API. This is an early usage and has been abandoned.

Now, you should be able to process databases, tables, and data in the MySQL Cluster.

  6. Create a database table

Compared with MySQL that does not use the Cluster, there is no big difference in the way data is operated in the MySQL Cluster. When performing such operations, remember two points:

Tables must be created with the ENGINE = NDB or ENGINE = NDBCLUSTER option, or changed with the ALTERTABLE option to use the NDBCluster storage ENGINE to copy them within the Cluster. If you use mysqldump output to import tables from an existing database, you can open the SQL script in the text editor and add this option to any table creation statement, alternatively, replace any existing ENGINE (or TYPE) Options with one of these options.

Remember that each NDB table must have a primary key. If you do not define a primary key when creating a table, the NDB Cluster Storage engine automatically generates an implicit primary key. (Note: This implicit key also occupies space, just like any other table index. The problem is not uncommon because there is not enough memory to hold these automatically created keys ).

The following is an example:
On db2, create a data table and insert data:

[Db2 ~] Root
# Mysql-uroot test
[Db2 ~] Mysql> create table city (
[Db2 ~] Mysql> id mediumint unsigned not null auto_increment primary key,
[Db2 ~] Mysql> name varchar (20) not null default''
[Db2 ~] Mysql>) engine = ndbcluster default charset utf8;
[Db2 ~] Mysql> insert into city values (1, 'city1 ');
[Db2 ~] Mysql> insert into city values (2, 'city2 ');

On db3, query data:

[Db3 ~] Root # mysql-uroot test
[Db2 ~] Mysql> select * from city;
+ ----------- +
| Id | name |
+ ----------- +
| 1 | city1 |
+ ----------- +
| 2 | city2 |
+ ----------- +

  7. disable security
To disable Cluster, you can simply enter the following command in Shell on the machine where the MGM node is located:

[Db1 ~] Root #/usr/local/mysql/ndb_mgm-e shutdown
Run the following command to disable the mysqld service of the SQL node:

[Db2 ~] Root #/usr/local/mysql/bin/mysqladmin-uroot shutdown

  VIII. Others

For more information about MySQL Cluster and backup, see the "MySQL Cluster (MySQL Cluster)" section in the MySQL manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.