1、下載MySQL-cluster 7.3.7
http://dev.mysql.com/downloads/cluster/
2、環境清理及安裝
1)清理CentOS6.5自帶的mysql服務,其中第一個指令我不會執行也可以,其他系統如果不可以,還是建議執行
# yum -y remove mysql # rpm -qa | grep mysql* # rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
2)環境準備
建立資料夾(分如下3個類別來建立對應的資料夾)
存储节点:# mkdir /var/lib/mysql/data 管理节点:# mkdir /var/lib/mysql-cluster SQL节点:可不用 文件夹授权 进程DIR:# mkdir /var/run/mysqld
使用如下的命令来变更权限保证可写入: # chmod -R 1777 /var/lib/mysql # chmod -R 1777 /var/run/mysqld # chmod -R 1777 /var/lib/mysql-cluster
3)安裝mysql-cluster
先解壓縮MySQL-Cluster-gpl-7.4.2-1.el6.x86_64.rpm-bundle.tar
# tar xvf MySQL-Cluster-gpl-7.4.2-1.el6.x86_64.rpm-bundle.tar
然後執行如下指令安裝
# rpm -ivh MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64.rpm # rpm -ivh MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64.rpm
1)執行以下指令:
--------------------------------------------------------------------------------------------------------------------- A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! You will find that password in '/root/.mysql_secret'. You must change that password on your first connect, no other statement but 'SET PASSWORD' will be accepted. See the manual for the semantics of the 'password expired' flag. Also, the account for the anonymous user has been removed. In addition, you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test database. This is strongly recommended for production servers. -----------------------------------------------------------
# cd /var/lib/mysql-cluster # vi config.ini
修改my.cnf文件,新增如下內容:
[computer] Id=mgr-server-01 HostName=192.168.100.218 [mgm default] datadir=/var/lib/mysql-cluster [mgm] HostName=192.168.100.218 NodeId=60 ExecuteOnComputer=mgr-server-01 PortNumber=1186 ArbitrationRank=2 [ndbd default] NoOfReplicas=2 DataMemory=500M IndexMemory=500M [ndbd] HostName=192.168.100.217 DataDir=/var/lib/mysql NodeId=1 [ndbd] HostName=192.168.100.218 DataDir=/var/lib/mysql NodeId=2 [mysqld] HostName=192.168.100.217 NodeId=81 [mysqld] HostName=192.168.100.218 NodeId=82
1)啟動mysql-cluster
執行初次啟動前請先確認將兩台機器的防火牆關閉(service iptables stop 或設定防火牆連接埠可通,兩個連接埠即通訊埠1186、資料埠3306 )
初次啟動mgt console指令:ndb_mgmd -f /var/lib/mysql-cluster/config.ini
啟動均衡節點指令:
啟動資料節點指令: mysqld_safe --defaults-file=/etc/my.cnf --explicit_defaults_for_timestamp &
注意在啟動過程中需要監控整個控制台輸出,發現有錯誤資料。
------------------------------------------------- -------------------------------------------------- ------
如果一起正常,使用以下指令將Management console開啟: ndb_mgm
執行# show
檢查是否各個節點都已經完全啟動,如下,每個節點都已經連接,如果有節點沒連接,會看
[client] socket=/var/lib/mysql/mysql.sock [mysqld] max_connections=100 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock ndbcluster ndb-connectstring=192.168.100.218 [mysqld_safe] log-error=/var/log/mysqld.log #pid-file=/var/run/mysqld/mysqld.pid pid-file=/var/lib/mysql/mysqld.pid [mysql_cluster] ndb-connectstring=192.168.100.218
ndb_mgm> show Connected to Management Server at: 192.168.100.218:1186 Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=1 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0) id=2 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0, *) [ndb_mgmd(MGM)] 1 node(s) id=60 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2) [mysqld(API)] 2 node(s) id=81 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2) id=82 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2)
當mysqld 啟動完畢正常後(可以使用pgrep mysqld來獲取程序IDID) ,我們可以使用以下指令修改:
ndb_mgm> show Cluster Configuration -------------------- [ndbd(NDB)] 2 node(s) id=1 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0) id=2 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0, *) [ndb_mgmd(MGM)] 1 node(s) id=60 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2) [mysqld(API)] 2 node(s) id=81 (not connected, accepting connect from 192.168.100.217) id=82 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2)
mysql -u root -p;
SET PASSWORD = PASSWORD('新密码');
111111的hash值是「*FD571203974BA9AFE270FE62151AE967ECA5E0AA」
SET PASSWORD = PASSWORD('*FD571203974BA9AFE270FE62151AE967ECA5E0AA');如下命令,強制使用明文進行登陸
select password('111111');
5、當上面步奏都操作完成以後,測試cluster是否正常工作
1 )簡單功能測試
在218上進入mysql後執行如下:
use mysql; delete from user; grant all on *.* to root@'%' identified by "111111" with grant option; flush privileges; quit;
2)測試災備情況下資料是否可以同步(一台資料庫停掉了,再啟動的時候,是否會將遺失的資料自動補充)
停掉217上的mysql服務,在218上執行
create database clustertest; use clustertest; CREATE TABLE testtable(Count INT) ENGINE=NDBCLUSTER;
insert into testtable values (1);
關閉資料節點:# mysqld stop (SQL節點可以用mysqladmin shutdown或別的方式關閉。 )
在管理節點上執行:# ndb_mgm -e shutdown
將安全的關閉管理節點和資料節點。
關閉後使用如下的進程檢測命令查看一下是否退出來了:
select * from testtable;
7、再次啟動Cluster方案:
啟動整個cluster的次序,注意,有些語句已經減去了初始化的內容:
ndb_mgmd -f /var/lib/mysql-cluster/config.ini ndbd mysqld_safe --defaults-file=/etc/my.cnf --explicit_defaults_for_timestamp &
备注:
1、mysql安装后,默认会自动启动的,每次启动cluster的时候,都需要先把mysql的服务关闭,太麻烦,于是可以执行如下命令(我出手有点狠):
chkconfig --level 123456 mysql off
2、防火墙问题,偷个懒,我懒得去一一开放端口,干脆把防火墙直接关了
chkconfig --level 123456 mysql off
以上就是MySQL之——MySQL Cluster集群搭建详解(基于RPM安装包)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!