MYSQL集羣

介紹:
MySQL集羣是一種分佈式設計,目標是要達到沒有任何單點故障點。因此,任何組成部分都應該擁有自己的內存和磁盤。通過這種冗餘設計,MySQL聲稱數據的可用度可以達到99。999%。
實際上,MySQL集羣是把一個叫做NDB的內存集羣存儲引擎集成與標準的MySQL服務器集成。它包含一組計算機,每個都跑一個或者多個進程,這可能包括一個MySQL服務器,一個數據節點,一個管理服務器和一個專有的一個數據訪問程序。它們之間的關係如下圖所示:
安裝
準備:實驗環境:rhel5.3(5臺)
機器: 全部安裝Red Hat Linux AS 5且關閉防火牆
軟件: mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz
四臺機器分別分配如下IP及用途:
IP
Description
192.168.99.80
Management Node
192.168.99.87
SQL Node
192.168.99.88
SQL Node
192.168.99.89
Data Node
192.168.99.90
Data Node
安裝節點,四臺機器,需要重複四次:(管理節點不用安裝mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz,可以從其他4臺機器拷貝出ndbd_mgmd,ndbd_mgm兩個程序。)
[root@candyshop ~]#groupadd mysql
[root@candyshop ~]#useradd –g mysql mysql
[root@candyshop ~]#tar zxvf mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz
[root@candyshop ~]#chown mysql:mysql mysql-cluster-gpl-6.3.20-linux-i686-glibc23
[root@candyshop ~]#mv mysql-cluster-gpl-6.3.20-linux-i686-glibc23 /usr/local/mysql
上述命令先創建mysql組和mysql用戶,並且把mysql分配到mysql組,然後將安裝文件解壓,把它放置到/usr/local/mysql目錄。
配置
配置管理節點[192.168.99.80]:
創建以下文件/usr/local/mysql/cluster-conf/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.
# Management process options:
[ndb_mgmd]
id=1
hostname=192.168.99.80 # Hostname or IP address of management node
datadir=/var/lib/mysql-cluster # Directory for management node log files
目錄不存在手動創建。
# Options for data node "A":
[ndbd]
Id=2
Hostname=192.168.99.89 # Hostname or IP address
Datadir=/usr/local/mysql/ndbdata # Directory for this data node's data files
目錄不存在手動創建
# Options for data node "B":
[ndbd]
Id=3
Hostname=192.168.99.90 # Hostname or IP address
Datadir=/usr/local/mysql/ndbdata # Directory for this data node's data files
# SQL node options:
[mysqld]
Id=4
Hostname=192.168.99.87 # Hostname or IP address
# (additional mysqld connections can be
# specified for this node for various
# purposes such as running ndb_restore)
[mysqld]
Id=5
Hostname=192.168.99.88
在這個文件裏,我們分別給五個節點分配了ID,這有利於更好的管理和區分各個節點。當然,要是不指定,MySQL也會動態分配一個。上述幾個Datadir如果還不存在的需要手動創建。用mkdir –p命令即可。
配置數據節點[192.168.99.89, 192.168.99.90]:
創建/etc/my.cnf文件,內容如下
# Options for mysqld process:
[mysqld]
Datadir=/usr/local/mysql/ndbdata
ndbcluster # run NDB storage engine
ndb-connectstring=192.168.99.80 # location of management server
# Options for ndbd process:
[mysql_cluster]
ndb-connectstring=192.168.99.80 # location of management server
配置SQL節點[192.168.99.87,192.168.99.88]
創建/etc/my.cnf文件,內容如下
# Options for mysqld process:
[mysqld]
ndbcluster # run NDB storage engine
ndb-connectstring=192.168.99.80 # location of management server
# Options for ndbd process:
[mysql_cluster]
ndb-connectstring=192.168.99.80 # location of management server
[root@server88 mysql]# ./scripts/mysql_install_db --user=mysql
在sql節點上初始化數據庫。
Installing MySQL system tables...
OK
Filling help tables...
OK
到這裏,所有配置已經完成了。
啓動
啓動管節點[192.168.99.80]:
[root@candyshop mysql]# ./bin/ndb_mgmd -f cluster-conf/config.ini
[root@candyshop mysql]# ./bin/ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 192.168.99.80:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 192.168.99.89)
id=3 (not connected, accepting connect from 192.168.99.90)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.99.80 (mysql-5.1.30 ndb-6.3.20)
[mysqld(API)] 1 node(s)
id=4 (not connected, accepting connect from 192.168.99.87)
[mysqld(API)] 1 node(s)
id=5 (not connected, accepting connect from 192.168.99.88)
我們可以看到集羣的狀態都還沒有啓動,都是not connected狀態。
啓動數據節點[192.168.99.89, 192.168.99.90]:不用啓動mysql。
[root@server89 ~]#cd /usr/local/mysql
[root@server89 mysql]#./bin/ndbd
啓動SQL節點[192.168.99.87,192.168.99.88]:
[root@server88 mysql]# ./bin/mysqld_safe –user=mysql &
至此,所有節點已經啓動完成,讓我們來查看系統狀態:
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.99.89 (mysql-5.1.30 ndb-6.3.20, Nodegroup: 0, Master)
id=3 @192.168.99.90 (mysql-5.1.30 ndb-6.3.20, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.99.80 (mysql-5.1.30 ndb-6.3.20)
[mysqld(API)] 1 node(s)
id=4 @192.168.99.87 (mysql-5.1.30 ndb-6.3.20)
id=5 @192.168.99.88 (mysql-5.1.30 ndb-6.3.20)
在SQL節點[192.168.99.87,192.168.99.88]上創建數據庫且插入數據
1. mysql> create database world;
2. Query OK, 1 row affected (0.09 sec)
3.
4. mysql> use world;
5. Database changed
6.
7. mysql> CREATE TABLE `City` (
8. -> `ID` int(11) NOT NULL auto_increment,
9. -> `Name` char(35) NOT NULL default '',
10. -> `CountryCode` char(3) NOT NULL default '',
11. -> `District` char(20) NOT NULL default '',
12. -> `Population` int(11) NOT NULL default '0',
13. -> PRIMARY KEY (`ID`)
14. -> ) ENGINE=NDBCLUSTER DEFAULT CHARSET=utf8;
15.Query OK, 0 rows affected (0.93 sec)
16.
17.mysql> INSERT INTO `City` VALUES (1,'Kabul','AFG','Kabol',1780000);
18.Query OK, 1 row affected (0.00 sec)
19.
20.mysql> INSERT INTO `City` VALUES (2,'Qandahar','AFG','Qandahar',237500);
21.Query OK, 1 row affected (0.00 sec)
22.
23.mysql> INSERT INTO `City` VALUES (3,'Herat','AFG','Herat',186800);
24.Query OK, 1 row affected (0.00 sec)
25.
26.
27.
28.
29.mysql> select * from City;
30.+----+----------+-------------+----------+------------+
31.| ID | Name | CountryCode | District | Population |
32.+----+----------+-------------+----------+------------+
33.| 3 | Herat | AFG | Herat | 186800 |
34.| 1 | Kabul | AFG | Kabol | 1780000 |
35.| 2 | Qandahar | AFG | Qandahar | 237500 |
36.+----+----------+-------------+----------+------------+
37.3 rows in set (0.00 sec)
1. [root@candyshop mysql]# ./bin/mysql -u root
2. Welcome to the MySQL monitor. Commands end with ; or \g.
3. Your MySQL connection id is 2
4. Server version: 5.1.30-ndb-6.3.20-cluster-gpl MySQL Cluster Server (GPL)
5.
6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
7.
8. mysql> show databases;
9. +--------------------+
10.| Database |
11.+--------------------+
12.| information_schema |
13.| mysql |
14.| test |
15.| world |
16.+--------------------+
17.4 rows in set (0.00 sec)
18.
19.mysql> select * from world.City;
20.+----+----------+-------------+----------+------------+
21.| ID | Name | CountryCode | District | Population |
22.+----+----------+-------------+----------+------------+
23.| 1 | Kabul | AFG | Kabol | 1780000 |
24.| 2 | Qandahar | AFG | Qandahar | 237500 |
25.| 3 | Herat | AFG | Herat | 186800 |
26.+----+----------+-------------+----------+------------+
27.3 rows in set (0.07 sec)
發現之前在另外一個SQL節點[192.168.99.87]創建的數據庫也出現在節點5[192.168.99.88]上面了。
這樣整個安裝和測試都完結了。
在sql節點創建的數據會保存在數據節點上,所以一個mysql宕機了不影響數據的存儲。

原文地址:http://bbs.coderli.com/thread-246-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章