分佈式mysql數據庫mysqlcluster

IP規劃
mgm管理節點:192.168.88.199
data1:       192.168.88.204
data2:       192.168.88.205
sql1:        192.168.88.202
sql2:        192.168.88.203
全部節點執行
[root@localhost Desktop]# service iptables stop
[root@localhost Desktop]# yum remove  mysql-libs-5.1.71-1.el6.x86_64
[root@localhost Desktop]# rpm -qa|grep mysql
[root@localhost Desktop]#
同步mysql文件



添加mysql用戶和組,這是必需的。
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -g mysql mysql
解壓mysql cluster
[root@localhost soft]# tar -zxf mysql-cluster-gpl-7.4.10-linux-glibc2.5-x86_64.tar.gz
[root@localhost soft]# cp -rp mysql-cluster-gpl-7.4.10-linux-glibc2.5-x86_64/ /usr/local/mysql
[root@localhost soft]# cd /usr/local/mysql
設置權限
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data
[root@localhost mysql]# chgrp -R mysql .
初始表空間
[root@localhost mysql]# scripts/mysql_install_db --user=mysql
開機自啓動
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
啓動mysql服務,若是報錯請參考

service mysqld start
在管理節點
[root@localhost mysql]# cp bin/ndb_mgm* /usr/local/bin
[root@localhost mysql]# cp bin/ndb_mgm* /usr/local/bin
[root@localhost mysql]# cd /usr/local/bin
[root@localhost bin]# chmod +x ndb_mgm*

配置

數據節點和SQL節點

[client]

port    =3306

socket  =/tmp/mysql.sock

[mysqld]

basedir         =/usr/local/mysql/

datadir         =/usr/local/mysql/data

user            = mysql

log-error       =/var/lib/mysql/mysqld.err

ndbcluster   #運行NDB存儲引擎

#指定管理節點  以上兩行聲明其爲SQL節點

ndb-connectstring=192.168.88.199

[mysql_cluster]

#指定管理節點  以上兩行聲明其爲數據節點

ndb-connectstring=192.168.88.199


管理節點:
[root@localhost bin]# cd /var/lib
[root@localhost lib]# mkdir mysql-cluster
[root@localhost lib]# cd mysql-cluster
[root@localhost mysql-cluster]# vi  config.ini


[ndbd default]

NoOfReplicas=2

DataMemory=80M

IndexMemory=18M

[tcp default]

portnumber=2202

[ndb_mgmd]

hostname=192.168.88.199

datadir=/var/lib/mysql-cluster   

[ndbd]

hostname=192.168.88.204

datadir=/usr/local/mysql/data

[ndbd]

hostname=192.168.88.205

datadir=/usr/local/mysql/data

[mysqld]

hostname=192.168.88.202

[mysqld]

hostname=192.168.88.203

[NDBD DEFAULT]:表示每一個數據節點的默認配置在每一個節點的[NDBD]中不用再寫這些選項,只能有一個。
[NDB_MGMD]:表示管理節點的配置,只有一個。
[NDBD]:表示每一個數據節點的配置,能夠有多個。
[MYSQLD]:表示SQL節點的配置,能夠有多個,分別寫上不一樣SQL節點的IP地址,也能夠什麼都不寫,只保留一個空節點,表示任意一個IP地址均可以進行訪問,此節點的個數代表了能夠用來鏈接數據節點的SQL節點總數。
4、啓動
一、管理節點

mysql cluster 須要各個節點都 進行啓動後才能夠工做,節點的啓動順序爲管理節點->數據節點->SQL節點。首先啓動管理節點
[root@localhost mysql-cluster]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.6.28 ndb-7.4.10
[root@localhost mysql-cluster]# ndb_mgm
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.88.204)
id=3 (not connected, accepting connect from 192.168.88.205)

[ndb_mgmd(MGM)]    1 node(s)
id=1    @192.168.88.199  (mysql-5.6.28 ndb-7.4.10)

[mysqld(API)]    2 node(s)
id=4 (not connected, accepting connect from 192.168.88.202)
id=5 (not connected, accepting connect from 192.168.88.203)

命令行中的ndb_mgmd是mysql cluster的管理服務器,後面的-f表示後面的參數是啓動的參數配置文件。若是在啓動後過了幾天又添加了一個數據節點,這時修改了配置文件啓動時就必須加上--initial參數,否則添加的節點不會做用在mysql cluster中。

ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial

二、數據節點

安裝後第一次啓動數據節點時要加上--initial參數,其它時候不要加,除非是在備份、恢復或配置變化後重啓時。

cd mysql/bin/ndbd --initial

若是顯示如下信息說明啓動完成
NDBD1
[root@localhost mysql]# ./bin/ndbd --initial
2016-02-18 05:00:38 [ndbd] INFO     -- Angel connected to '192.168.88.199:1186'
2016-02-18 05:00:38 [ndbd] INFO     -- Angel allocated nodeid: 3

NDBD1
[root@localhost mysql]# ./bin/ndbd --initial
2016-02-18 05:00:38 [ndbd] INFO     -- Angel connected to '192.168.88.199:1186'
2016-02-18 05:00:38 [ndbd] INFO     -- Angel allocated nodeid: 2
在管理節點查看連接情況
db_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]    2 node(s)
id=2    @192.168.88.204  (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0, *)
id=3    @192.168.88.205  (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0)

[ndb_mgmd(MGM)]    1 node(s)
id=1    @192.168.88.199  (mysql-5.6.28 ndb-7.4.10)

[mysqld(API)]    2 node(s)
id=4 (not connected, accepting connect from 192.168.88.202)
id=5 (not connected, accepting connect from 192.168.88.203)
啓動mysql

[root@localhost mysql]# service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
發現找不了日誌
用mysqld——safe啓動
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1] 4228
[root@localhost mysql]# 160218 05:20:37 mysqld_safe Logging to '/var/lib/mysql/mysqld.err'.
touch: cannot touch `/var/lib/mysql/mysqld.err': No such file or directory
chmod: cannot access `/var/lib/mysql/mysqld.err': No such file or directory
touch: cannot touch `/var/lib/mysql/mysqld.err': No such file or directory
chown: cannot access `/var/lib/mysql/mysqld.err': No such file or directory
160218 05:20:37 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
建立目錄
mkdir /var/lib/mysql/
一樣方法啓動第二SQL個節點
[root@localhost data]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
查看狀態
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]    2 node(s)
id=2    @192.168.88.204  (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0, *)
id=3    @192.168.88.205  (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0)

[ndb_mgmd(MGM)]    1 node(s)
id=1    @192.168.88.199  (mysql-5.6.28 ndb-7.4.10)

[mysqld(API)]    2 node(s)
id=4    @192.168.88.202  (mysql-5.6.28 ndb-7.4.10)
id=5    @192.168.88.203  (mysql-5.6.28 ndb-7.4.10)

測試
在SQL1節點上
[root@localhost bin]# /usr/local/mysql/bin/mysql
mysql> create database tt;
mysql> use tt
Database changed
mysql> create table teacher(t_id int) engine=ndb;#建立一個存儲引擎爲ndb的存儲引擎
Query OK, 0 rows affected (0.28 sec)
ysql> insert into teacher values(133);#插入一條記錄
Query OK, 1 row affected (0.01 sec)
在SQL2節點上
[root@localhost data]#  /usr/local/mysql/bin/mysql
mysql> select * from tt.teacher;
+------+
| t_id |
+------+
|  133 |
+------+
1 row in set (0.07 sec)

二、模擬NDB節點Crash:
在節點2上終止掉NDB進程,而後再分別經過兩個SQL節點去訪問teacher表,查看是否能夠正常訪問,數據是否一致。以下:
查看進程
kill -9 3492殺掉一個ndb
db_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]    2 node(s)
id=2 (not connected, accepting connect from 192.168.88.204)###########被傻掉了
id=3    @192.168.88.205  (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0, *)

[ndb_mgmd(MGM)]    1 node(s)
id=1    @192.168.88.199  (mysql-5.6.28 ndb-7.4.10)

[mysqld(API)]    2 node(s)
id=4    @192.168.88.202  (mysql-5.6.28 ndb-7.4.10)
id=5    @192.168.88.203  (mysql-5.6.28 ndb-7.4.10)

ysql> select * from tt.teacher;############在sql2節點任然可以訪問,數據是寫在sql1上,
+------+
| t_id |
+------+
|  133 |
+------+
1 row in set (0.00 sec)

[root@localhost bin]# ps -ef |grep mysql
root      4405  2444  0 05:22 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
mysql     4567  4405  2 05:22 pts/0    00:00:29 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/var/lib/mysql/mysqld.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
root      4670  4598  0 05:33 pts/1    00:00:00 /usr/local/mysql/bin/mysql
root      4752  4742  0 05:46 pts/2    00:00:00 grep mysql
[root@localhost bin]# kill -9 4567
[root@localhost bin]# kill -9 4670
SQL2節點任然可以訪問,雖然insert在sql1上
mysql> select * from tt.teacher;
+------+
| t_id |
+------+
|  133 |
+------+
1 row in set (0.00 sec)
























node

相關文章
相關標籤/搜索