1、服務(wù)器準備
1) MySQL節(jié)點1 10.41.1.85
2) MySQL節(jié)點2 10.41.1.84
3) ndb節(jié)點1 10.41.1.83
4) ndb節(jié)點2 10.41.1.82
5) 管理節(jié)點 10.41.1.81
2、測試環(huán)境
5臺服務(wù)器均一樣,不是必須的,所以服務(wù)器均已關(guān)閉iptables和selinux,生產(chǎn)環(huán)境請根據(jù)實際情況自行決定
systemctl stop firewalld #關(guān)閉防火墻
systemctl disable firewalld #永久關(guān)閉防火墻
setenforce 0 #臨時關(guān)閉selinux
sed -i 's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config #永久關(guān)閉selinux
getenforce #查看當前狀態(tài)
3、軟件準備
mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
4、管理節(jié)點安裝配置
建立目錄:
mkdir /usr/local/mysql/bin -p
mkdir /data/mysql-cluster -p
準備文件
cp ./mysql-cluster-gpl-7.2.4-linux2.6-x86_64/bin/ndb_mgm /usr/local/mysql/bin/
cp ./mysql-cluster-gpl-7.2.4-linux2.6-x86_64/bin/ndb_mgmd /usr/local/mysql/bin/
環(huán)境變量
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
配置
cd /data/mysql-cluster/
touch config.ini
cat /data/mysql-cluster/config.ini
[NDBD DEFAULT]
NoOfReplicas=1
DataMemory=64M
IndexMemory=16M
#管理節(jié)點
[NDB_MGMD]
nodeid=1
hostname=10.41.1.81
datadir=/data/mysql-cluster
#第一個 ndbd 節(jié)點:
[NDBD]
nodeid=2
hostname=10.41.1.83
datadir=/data/mysql-cluster/data
#第二個 ndbd 節(jié)點:
[NDBD]
nodeid=3
hostname=10.41.1.82
datadir=/data/mysql-cluster/data
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)瓊中黎族,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
[MySQLD]
nodeid=4
hostname=10.41.1.85
[MySQLD]
nodeid=5
hostname=10.41.1.84
[MySQLD]
6、NDB節(jié)點安裝(數(shù)據(jù)節(jié)點)
數(shù)據(jù)節(jié)點1: 10.41.1.83
數(shù)據(jù)節(jié)點2: 10.41.1.82
建立用戶
useradd mysql
解壓mysql cluster
tar zxf mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
mv mysql-cluster-gpl-7.2.4-linux2.6-x86_64/ /usr/local/mysql/
授權(quán)
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .
建立數(shù)據(jù)目錄,并授權(quán)(用來在沒使用NDB時存放的數(shù)據(jù))
mkdir /data/mysql -p
chown -R mysql.mysql /data/mysql/
初始化
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql
授權(quán)root
chown -R root .
配置
\cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
環(huán)境變量
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
建立目錄(存放NDB節(jié)點的數(shù)據(jù))
mkdir /data/mysql-cluster/data -p
修改/etc/my.cnf文件
cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
basedir= /usr/local/mysql
ndbcluster
ndb-connectstring=10.41.1.81
[MYSQL_CLUSTER]
ndb-connectstring=10.41.1.81
說明:
如果希望盡可能的各環(huán)境保持一致,建議在NDB節(jié)點也和SQL節(jié)點一樣安裝整個帶有 NDB Cluster 存儲引擎的MySQL Server。(NDB節(jié)點可以不用初始化數(shù)據(jù),自己已經(jīng)測試,但是我依然會初始化)安裝細節(jié)和上面的SQL節(jié)點完全一樣。
7、安裝 MySQL 節(jié)點
sql節(jié)點1: 10.41.1.85
sql節(jié)點2: 10.41.1.84
建立用戶
useradd mysql
解壓mysql cluster
tar zxf mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
mv mysql-cluster-gpl-7.2.4-linux2.6-x86_64/ /usr/local/mysql/
授權(quán)
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .
建立數(shù)據(jù)目錄,并授權(quán)(用來在沒使用NDB時存放的數(shù)據(jù))
mkdir /data/mysql -p
chown -R mysql.mysql /data/mysql/
初始化
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql
授權(quán)root
chown -R root .
配置
\cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
環(huán)境變量
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
建立目錄(存放NDB節(jié)點的數(shù)據(jù))
mkdir /data/mysql-cluster/data -p
修改/etc/my.cnf文件
cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
basedir= /usr/local/mysql
ndbcluster
ndb-connectstring=10.41.1.81
[MYSQL_CLUSTER]
ndb-connectstring=10.41.1.81
[ndbd(NDB)] 2 node(s)
id=2 @10.41.1.83 (mysql-5.5.19 ndb-7.2.4, Nodegroup: 0, Master)
id=3 @10.41.1.82 (mysql-5.5.19 ndb-7.2.4, Nodegroup: 1)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @10.41.1.81 (mysql-5.5.19 ndb-7.2.4)
[mysqld(API)] 3 node(s)
id=4 @10.41.1.85 (mysql-5.5.19 ndb-7.2.4)
id=5 @10.41.1.84 (mysql-5.5.19 ndb-7.2.4)
id=6 (not connected, accepting connect from any host)
10、測試1-NDB存儲引擎測試
(1)在任意一個SQL節(jié)點(我這里選擇10.41.1.85)的test_cluster庫中創(chuàng)建測試表test_table01(不指定引擎)和test_table02(NDB存儲引擎),設(shè)置存儲引擎為NDB,并插入兩條測試數(shù)據(jù):
mysql> create table test_table01( id int, name varchar(20) ) engine=ndb;
mysql> create table test_table02( id int, name varchar(20) ) ;
mysql> show tables;
+------------------------+
Tables_in_test_cluster
+------------------------+
test_table01
test_table02
+------------------------+
2 rows in set (0.01 sec)
另一sql節(jié)點:
mysql> show tables;
+------------------------+
Tables_in_test_cluster
+------------------------+
test_table02
+------------------------+
1 row in set (0.02 sec)
說明只有ndb引擎才同步。
(2)在test_table02中插入兩條測試數(shù)據(jù)
mysql> insert into t1 select 1,'yayun';
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t1 select 1,'atlas';
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
兩個sql節(jié)點都能查到數(shù)據(jù)。
mysql> select * from test_table02;
+------+-----------+
id | name
+------+-----------+
1 | cluster01
2 | cluster02
+------+-----------+
2 rows in set (0.00 sec)
顯然,兩個SQL節(jié)點查詢的數(shù)據(jù)是一致。
(3)在SQL節(jié)點10.41.1.85上把測試表test_table02引擎改為MyISAM,再次插入測試數(shù)據(jù):
mysql> alter table test_table02 engine=myisam;
Query OK, 2 rows affected (0.90 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into test_table02 value(3,'cluster03');#10.41.1.84
ERROR 1412 (HY000): Table definition has changed, please retry transaction
插入數(shù)據(jù)直接報錯。
11、單點故障測試
管理節(jié)點,不需要特殊的配置,只需要將管理工具和配置文件放置多臺服務(wù)器上即可。
SQL節(jié)點
10.41.1.84上的MySQL服務(wù)停止
管理節(jié)點:
[mysqld(API)] 3 node(s)
id=4 @10.41.1.85 (mysql-5.5.19 ndb-7.2.4)
id=5 (not connected, accepting connect from 10.41.1.84)
id=6 (not connected, accepting connect from any host)
10.41.1.85:
mysql> insert into test_table02 value(4,'cluster04');
Query OK, 1 row affected (0.00 sec)
mysql> select from test_table02;
+------+-----------+
id | name
+------+-----------+
2 | cluster02
3 | cluster03
4 | cluster04
1 | cluster01
+------+-----------+
4 rows in set (0.00 sec)
10.41.1.85服務(wù)開啟:
mysql> select from test_table02;
+------+-----------+
id | name
+------+-----------+
3 | cluster03
4 | cluster04
1 | cluster01
2 | cluster02
+------+-----------+
4 rows in set (0.01 sec)
數(shù)據(jù)又同步過來了
NDB(數(shù)據(jù)節(jié)點)
數(shù)據(jù)節(jié)點10.41.1.82上的NDB進程停止
[root@node3 mysql]# ps -ef | grep ndbd
root 15969 1 0 14:37 ? 00:00:04 ndbd --initial
root 15970 15969 1 14:37 ? 00:00:47 ndbd --initial
root 16029 15801 0 15:36 pts/3 00:00:00 grep --color=auto ndbd
killall ndbd
任意sql節(jié)點查詢:
mysql> select from test_table02;
ERROR 1296 (HY000): Got error 157 'Unknown error code' from NDBCLUSTER
無法查詢
10.41.1.81:
grep 'NoOfReplicas' /data/mysql-cluster/config.ini
NoOfReplicas=1 #每個數(shù)據(jù)節(jié)點的鏡像數(shù)量
將配置文件中的NoOfReplicas改為2,按照前面的步驟重新啟動集群,無法啟動,NoOfReplicas參數(shù)無法臨時更改,我們開始就需要設(shè)置好,不要到后面才想到更改,那時就悲劇了。如果重新ndbd --initial,將會丟失所有數(shù)據(jù)。
NoOfReplicas=2:
管理節(jié)點需要--initial參數(shù)啟動
ndb_mgmd --initial -f /data/mysql-cluster/config.ini
數(shù)據(jù)節(jié)點(NDB)啟動還要用ndbd --initial,數(shù)據(jù)丟失
mysql> select from test_table02;
ERROR 1146 (42S02): Table 'test_cluster.test_table02' doesn't exist
mysql> create table test(
-> id int,
-> name varchar(20)
-> ) engine=ndb;
Query OK, 0 rows affected (0.85 sec)
mysql> show tables;
+------------------------+
Tables_in_test_cluster
+------------------------+
test
+------------------------+
1 row in set (0.01 sec)
mysql> insert into test value(1,'one');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test value(2,'two');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+------+
id | name
+------+------+
1 | one
2 | two
+------+------+
2 rows in set (0.00 sec)
再次停掉數(shù)據(jù)節(jié)點10.41.1.82:
兩節(jié)點都可以正常數(shù)據(jù)查詢
12、MySQL Cluster集群的關(guān)閉
關(guān)閉順序:SQL節(jié)點->數(shù)據(jù)節(jié)點->管理節(jié)點(在MySQL Cluster環(huán)境中,NDB節(jié)點和管理節(jié)點的關(guān)閉都可以在管理節(jié)點的管理程序中完成,也可以分節(jié)點關(guān)閉,但是SQL節(jié)點卻沒辦法。所以,在關(guān)閉整個MySQL Cluster環(huán)境或者關(guān)閉某個SQL節(jié)點的時候,首先必須到SQL節(jié)點主機上來關(guān)閉SQL節(jié)點程序。關(guān)閉方法和MySQL Server的關(guān)閉一樣。)
(1)SQL節(jié)點關(guān)閉
/etc/init.d/mysqld stop
(2)(NDB)數(shù)據(jù)節(jié)點關(guān)閉
ndbd stop
(3)管理節(jié)點關(guān)閉
ndb_mgm> shutdown或者命令行:ndb_mgm -e shutdown
總結(jié):
參數(shù)NoOfReplicas無法臨時更改。
selinux,iptables等相關(guān)問題。
注:參考大量網(wǎng)絡(luò)數(shù)據(jù)。