MySQLCluster (MySQL cluster) Author: ye Jinrong (Email :), source: imysql. cnMySQLCluster is a highly practical and redundant MySQL version suitable for distributed computing environments. It uses the NDBCluster storage engine and allows multiple MySQL servers to run in one Cluster. Binary values in MyQL5.0 and later versions
MySQL Cluster (MySQL Cluster) Author: ye Jinrong (Email:), source: http://imysql.cn MySQL Cluster is MySQL suitable for distributed computing environment of high practical, high redundancy version. It uses the NDB Cluster Storage engine and allows multiple MySQL servers to run in one Cluster. Binary values in MySQL 5.0 and later versions
MySQL Cluster (MySQL Cluster) initial test
Author: ye Jinrong (Email :), source: http://imysql.cn
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 available in MySQL 5.0 and later binary versions, 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 MySQL Cluster, the client node is a traditional MySQL server that uses the NDB Cluster 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 preparation
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. Because MySQL Cluster uses TCP/IP connection and data transmission between nodes is not encrypted, 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 nobodyroot# /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.gzroot# mv mysql-max-5.0.24-linux-i686 /usr/local/mysql/root# cd /usr/local/mysql/root# ./configure --prefix=/usr/local/mysqlroot# ./scripts/mysql_install_dbroot# 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/datauser = nobodyport = 3306socket = /tmp/mysql.sockndbclusterndb-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]ndbclusterndb-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.gzroot# mkdir /usr/local/mysql/root# mkdir /usr/local/mysql/data/root# cd mysql-max-5.0.24-linux-i686/bin/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 the management node server [NDB_MGMD] hostname = db1 # datadir =/usr/local/mysql /data/# Set the storage node server (NDB node) [NDBD] hostname = db4datadir =/usr/local/mysql/data/# Second NDB node [NDBD] hostname = db5datadir =/usr/local/mysql/data/# Set 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> SHOWConnected to Management Server at: localhost:1186Cluster 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 alter table option to use the NDB Cluster 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.
References: MySQL 5.1 Chinese manual