MySQL Cluster安裝

svoid發表於2015-03-16
MySQL Cluster的基本概念

NDB是一種“記憶體中”儲存引擎,它具有可用性高和資料一致性好的特點。。
在很多情況下,術語“節點”用於指計算機,但在討論MySQL Cluster時,它表示的是程式。在單臺計算機上可以有任意數目的節點。

有三類Cluster節點,在最低的MySQL Cluster配置中,至少有三個節點,這三類節點分別是:

  • 管理(MGM)節點
    這類節點的作用是管理MySQL Cluster內的其他節點,如提供配置資料、啟動並停止節點、執行備份等。由於這類節點負責管理其他節點的配置,應在啟動其他節點之前首先啟動這類節點。MGM節點是用命令ndb_mgmd啟動的。

  • 資料節點
    這類節點用於儲存Cluster的資料。資料節點的數目與副本的數目相關,是片段的倍數。例如,對於兩個副本,每個副本有兩個片段,那麼就有4個資料節點。沒有必要有一個以上的副本。資料節點是用命令ndbd啟動的。

  • SQL節點
    這是用來訪問Cluster資料的節點。對於MySQL Cluster,客戶端節點是使用NDB儲存引擎的傳統MySQL伺服器。典型情況下,SQL節點是使用命令mysqld –ndbcluster啟動的,或將ndbcluster新增到my.cnf後使用mysqld啟動。

環境介紹

IP地址 節點型別 描述
192.168.1.250 Mgmt Node Master管理節點
192.168.1.251 SQL Node SQL節點
192.168.1.252 SQL Node SQL節點
192.168.1.251 Data Node 資料節點
192.168.1.252 Data Node 資料節點

安裝MySQL Cluster

[192.168.1.250:SQL節點操作]
建立使用者
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> id mysql
uid=497(mysql) gid=500(mysql) 組=500(mysql)

shell> mkdir -p /opt/mysql
shell> mkdir -p /opt/mysql/etc

shell> tar zxvf mysql-cluster-gpl-7.4.4.tar.gz 
shell> cd mysql-cluster-gpl-7.4.4
shell> cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql \
 -DSYSCONFDIR=/opt/mysql/etc  \
 -DMYSQL_DATADIR=/opt/mysql/data \
 -DMYSQL_TCP_PORT=3306 \
 -DDEFAULT_CHARSET=utf8  \
 -DDEFAULT_COLLATION=utf8_general_ci  \
 -DWITH_NDB_JAVA=OFF  \
 -DWITH_NDBCLUSTER_STORAGE_ENGINE=ON

shell> make && make install
shell> chown -R mysql.mysql /opt/mysql/
shell> ./scripts/mysql_install_db --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/

修改root密碼並配置環境變數

 檢視ndb資料引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster         | NO      | Clustered, fault-tolerant tables                               | NULL         | NULL | NULL       |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| ndbinfo            | YES     | MySQL Cluster system information storage engine                | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)

關閉服務打包安裝檔案並解壓安裝到其他節點
其他節點依次執行
1. 建立使用者
2. 建立目錄
3. 設定環境變數

配置MySQL Cluster

配置Data Node和SQL Node
shell> vi /opt/mysql/ect/my.cnf
==================================================
[mysqld]
ndbcluster  #run NDB storage engine
[mysql_cluster]
ndb-connectstring=192.168.1.250 #location of management server
==================================================

配置管理節點
shell> vi /opt/mysql/ect/config.cnf
==================================================
[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=1    # Number of replicas
DataMemory=80M    # memory to allocate for data storage
IndexMemory=18M   # memory to allocate for index storage

[tcp default]
# TCP/IP options:
portnumber=2202   

[ndb_mgmd]
# Management process options:
Nodeid=1
hostname=192.168.1.250      # Hostname or IP address of MGM node
datadir=/opt/mysql/log      # Directory for MGM node log files

[ndbd]
# Options for data node "A":
hostname=192.168.1.251           # Hostname or IP address
datadir=/opt/mysql/mysql_ndb     # Directory for this data node's data file

[ndbd]
# Options for data node "B":
hostname=192.168.1.252           # Hostname or IP address
datadir=/opt/mysql/mysql_ndb     # Directory for this data node's data files

[mysqld]
# SQL node options:
hostname=192.168.1.251         

[mysqld]
hostname=192.168.1.252
==================================================

啟動MySQL Cluster

啟動管理節點
shell> ndb_mgmd -f /opt/mysql/etc/config.cnf 
MySQL Cluster Management Server mysql-5.6.23 ndb-7.4.4


使用ndb_mgm工具(MySQL Cluster Server的客戶端管理工具)管理ndb_mgmd

shell> 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 (not connected, accepting connect from 192.168.1.251)
id=3 (not connected, accepting connect from 192.168.1.252)

[ndb_mgmd(MGM)]    1 node(s)
id=1    @192.168.1.250  (mysql-5.6.23 ndb-7.4.4)

[mysqld(API)]    2 node(s)
id=4 (not connected, accepting connect from 192.168.1.251)
id=5 (not connected, accepting connect from 192.168.1.252)
啟動Data Node

在每個Data Node的主機,執行如下命令啟動ndbd程式:

shell> ndbd
2015-03-03 22:40:28 [ndbd] INFO     -- Angel connected to '192.168.1.250:1186'
2015-03-03 22:40:28 [ndbd] INFO     -- Angel allocated nodeid: 2

shell> ndbd --initial 
2015-03-04 06:12:34 [ndbd] INFO     -- Angel connected to '192.168.1.250:1186'
2015-03-04 06:12:34 [ndbd] INFO     -- Angel allocated nodeid: 3
啟動SQL Node

在每個SQL Node的主機,啟動mysql資料庫。

shell> /opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/etc/my.cnf &

驗證MySQL Cluster

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]    2 node(s)
id=2    @192.168.1.251  (mysql-5.6.23 ndb-7.4.4, Nodegroup: 0, *)
id=3    @192.168.1.252  (mysql-5.6.23 ndb-7.4.4, Nodegroup: 1)

[ndb_mgmd(MGM)]    1 node(s)
id=1    @192.168.1.250  (mysql-5.6.23 ndb-7.4.4)

[mysqld(API)]    2 node(s)
id=4    @192.168.1.251  (mysql-5.6.23 ndb-7.4.4)
id=5    @192.168.1.252  (mysql-5.6.23 ndb-7.4.4)

4號SQL Node節點執行如下操作:
shell> mysql -u root –p

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster         | YES     | Clustered, fault-tolerant tables                               | YES          | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| ndbinfo            | YES     | MySQL Cluster system information storage engine                | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)

mysql> create database ndb;
Query OK, 1 row affected (0.07 sec)
mysql> use ndb;
Database changed
mysql>  create table t1 (id int not null primary key ,name varchar(100)) engine=ndb;
Query OK, 0 rows affected (0.16 sec)

mysql> insert into t1 values(1,'svoid'),(2,'tom');
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

5號SQL Node節點進行資料查詢:
shell> mysql -u root -p

mysql> select * from ndb.t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | svoid |
|  2 | tom   |
+----+-------+
2 rows in set (0.04 sec)

測試正常,MySQL Cluster配置完成。

關閉

ndb_mgm> shutdown
Node 2: Cluster shutdown initiated
Node 3: Cluster shutdown initiated
Node 3: Node shutdown completed.
Node 2: Node shutdown completed.
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.

關閉管理節點與資料節點。

整理自網路

Svoid
2015-03-04

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29733787/viewspace-1462183/,如需轉載,請註明出處,否則將追究法律責任。

相關文章