MySQL Cluster 是 MySQL 適合於分佈式計算環境的高實用、高冗餘版本。它採用了NDB Cluster 存儲引擎,容許在1個 Cluster 中運行多個MySQL服務器。MySQL Cluster 可以使用多種故障切換和負載平衡選項配置NDB存儲引擎,但在 Cluster 級別上的存儲引擎上作這個最簡單。下面咱們簡單介紹MySQL Cluster如何安裝與配置。
基本設定
管理(MGM)節點:192.168.0.111
MySQL服務器(SQL)節點:192.168.0.110
數據(NDBD)節點"A":192.168.0.112
數據(NDBD)節點"B":192.168.0.113node
mysql的集羣安裝能夠有三種方式,一是直接下載二進制使用,二是使用rpm安裝,三是源碼編譯。咱們這裏使用第一種安裝。
一、每一個節點作相同的操做mysql
cd /tmplinux
wget http://cdn.mysql.com/Downloads/MySQL-Cluster-7.2/mysql-cluster-gpl-7.2.8-linux2.6-i686.tar.gzsql
tar xzf mysql-cluster-gpl-7.2.8-linux2.6-i686.tar.gz數據庫
mv mysql-cluster-gpl-7.2.8-linux2.6-i686 /usr/local/mysqlcentos
注意:這裏下載的是32位的二進制包,若是你的系統是64位,須要下載64位的包。
二、存儲節點和SQL節點安裝安全
groupadd mysql服務器
useradd -g mysql mysql分佈式
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysqlide
chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/data
chgrp -R mysql /usr/local/mysql
cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
一、配置存儲節點和SQL節點
vi /etc/my.cnf
相似於:
# Options for mysqld process:
[MYSQLD]
ndbcluster # run NDB engine
ndb-connectstring=198.168.0.111 # location of MGM node
# Options for ndbd process:
[MYSQL_CLUSTER]
ndb-connectstring=198.168.0.111 # location of MGM node
二、配置管理節點
mkdir /var/lib/mysql-cluster
cd /var/lib/mysql-cluster
vi config.ini
config.ini文件應相似於:
# Options affecting ndbd processes on all data nodes:
[NDBD DEFAULT]
NoOfReplicas=2 # Number of replicas
DataMemory=80M # How much memory to allocate for data storage
IndexMemory=18M # How much memory to allocate for index storage
# For DataMemory and IndexMemory, we have used the
# default values. Since the "world" database takes up
# only about 500KB, this should be more than enough for
# this example Cluster setup.
# TCP/IP options:
[TCP DEFAULT]
portnumber=2202 # This the default; however, you can use any
# port that is free for all the hosts in cluster
# Note: It is recommended beginning with MySQL 5.0 that
# you do not specify the portnumber at all and simply allow
# the default value to be used instead
# Management process options:
[NDB_MGMD]
hostname=198.168.0.111 # Hostname or IP address of MGM node
datadir=/var/lib/mysql-cluster # Directory for MGM node logfiles
# Options for data node "A":
[NDBD]
# (one [NDBD] section per data node)
hostname=198.168.0.112 # Hostname or IP address
datadir=/usr/local/mysql/data # Directory for this data node's datafiles
# Options for data node "B":
[NDBD]
hostname=198.168.0.113 # Hostname or IP address
datadir=/usr/local/mysql/data # Directory for this data node's datafiles
# SQL node options:
[MYSQLD]
hostname=198.168.0.110 # Hostname or IP address
# (additional mysqld connections can be
# specified for this node for various
# purposes such as running ndb_restore)
一、啓動管理節點
/usr/local/mysql/bin/ndb_mgmd --configdir=/var/lib/mysql-cluster -f /var/lib/mysql-cluster/config.ini
二、啓動數據節點
首次啓動須要--initial參數初始化,下一次啓動就不須要了。
/usr/local/mysql/bin/ndbd --initial
三、啓動SQL節點
/usr/local/mysql/bin/mysqld_safe &
四、檢查狀態
若是一切正常,執行命令 /usr/local/mysql/bin/ndb_mgm -e show應該會輸出相似信息:
[root@localhost mysql-cluster]# /usr/local/mysql/bin/ndb_mgm -e show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.0.112 (mysql-5.5.27 ndb-7.2.8, Nodegroup: 0, Master)
id=3 @192.168.0.113 (mysql-5.5.27 ndb-7.2.8, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.111 (mysql-5.5.27 ndb-7.2.8)
[mysqld(API)] 1 node(s)
id=4 @192.168.0.110 (mysql-5.5.27 ndb-7.2.8)
在SQL節點上執行以下數據庫操做:
/usr/local/mysql/bin/mysql -uroot -p
mysql> create database clusterdb;use clusterdb;
mysql> create table simples (id int not null primary key) engine=ndb;
mysql> insert into simples values (1),(2),(3),(4);
mysql> select * from simples;
若是出現:
+----+
| id |
+----+
| 1 |
| 2 |
| 4 |
| 3 |
+----+
則表示工做正常。
一、關閉mysql集羣,可在管理節點在執行以下命令:
/usr/local/mysql/bin/ndb_mgm -e shutdown
二、重啓管理節點
/usr/local/mysql/bin/ndb_mgmd --configdir=/var/lib/mysql-cluster -f /var/lib/mysql-cluster/config.ini
三、重啓數據節點
/usr/local/mysql/bin/ndbd