1. 先了解一下你是否應該用MySQL集群。
減少數據中心結點壓力和大數據量處理,采用把MySQL分布,一個或多個application對應一個MySQL數據庫。把幾個MySQL數據庫公用的數據做出共享數據,例如購物車,用戶對象等等,存在數據結點裡面。其他不共享的數據還維持在各自分布的MySQL數據庫本身中。
2. 集群MySQL中名稱概念.(如上圖)
1)Sql結點(SQL node--上圖對應為MySQLd):分布式數據庫。包括自身數據和查詢中心結點數據.
2)數據結點(Data node -- ndbd):集群共享數據(內存中).
3)管理服務器(Management Server – ndb_mgmd):集群管理SQL node,Data node.
3.配置
MySQL-max版本,當然現在MySQL集群系統windonws平台上面不被支持.
安裝MySQL就不多說了,網上一大堆,簡明扼要。
當然,你也可以讓一個機器同時為3者。
A,B my.inf加上:
[MySQLD] ndbcluster # run NDB engine ndb-connectstring=192.168.1.251 # location of MGM node # Options for ndbd process: [MySQL_CLUSTER] ndb-connectstring=192.168.1.251 # location of MGM node A: /var/lib/MySQL-cluster/config.ini [NDBD DEFAULT] NoOfReplicas=1 # 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=192.168.1.251 # 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=192.168.1.251 # Hostname or IP address datadir=/usr/local/MySQL/data # Directory for this data node's datafiles # SQL node options: [MySQLD] hostname=192.168.1.254 #[MySQLD] #這個相當於192.168.1.251
4. 啟動測試
在管理服務器上面(這裡是192.168.1.251):
shell>ndb_mgmd -f /var/lib/MySQL-cluster/config.ini
在數據結點服務器上面(依然是192.168.1.251and more):
shell>ndbd--initial
(第一次時加--initial參數)
SQL結點服務器上面(192.168.1.254):
shell>MySQLd &
在251上面察看
./ndb_mgm
-- NDB Cluster -- Management Client -- ndb_mgm> show Connected to Management Server at: 192.168.1.251:1186 Cluster Configuration --------------------- [ndbd(NDB)] 1 node(s) id=2 @192.168.1.251 (Version:5.0.22, Nodegroup: 0, Master) [ndb_mgmd(MGM)] 1 node(s) id=1 @192.168.1.251 (Version:5.0.22) [MySQLd(API)] 1 node(s) id=3 @192.168.1.254 (Version:5.0.22) ok
關閉集群:
shell>ndb_mgm -e shutdown
5.基本的集群說明
1)在MySQL集群中.當table引擎為NDBCLUSTER時才做集群,其他非NDBCLUSTER表和一般MySQL數據庫表一樣,不會共享數據. NDBCLUSTER表數據存儲在Data node服務器內存中,Data Node可以為1台或多台服務器,它們之間存放共享數據。Data Node服務器可以分組數據copy。
例如:2,3,4,5為四台Data Node服務器ID. 2,3為組0。 4,5為組1。 2,3維持數據相同,4,5維持數據相同。 組0和組1維持數據不同。
2)sql node服務器中,非NDBCLUSTER數據存在本身數據庫中,table引擎為NDBCLUSTER時,數據存儲在Data Node中。當查詢NDBCLUSTER表時,它會從Data node集群中提起數據.
3)Manager server
管理SQl node和Data node狀態。
附:MySQL集群配置詳細過程錄制
1、准備三台linux服務器(三台機器進行如下配置)
--hostname配置
192.168.9.241 sqltest01 (mysqld及存儲節點)
192.168.9.242 sqltest02 (mysqld及存儲節點)
192.168.9.243 sqltest03
其中,sqltest01、sqltest02分別是mysql節點及存儲節點,sqltest03為管理節點
--同時,把防火牆進行關閉或者把相關的端口打開,如3306,管理節點的1186等
[root@sqltest01 u01]# service iptables status
iptables: Firewall is not running.
如果開啟的,請使用service iptables stop
--創建相應的用戶及目錄
[root@sqltest01 u01]# groupadd mysql [root@sqltest01 u01]# useradd -r -g mysql mysql [root@sqltest01 u01]# mkdir -p /usr/local/mysql [root@sqltest01 u01]# chown -R mysql.mysql
2、mysql cluster
下載網址:dev.mysql.com,然後選擇cluster,然後在網頁中出現的選擇平台中,選擇linux generic!在這裡選擇所需要tar包,我這裡用的是mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64.tar.gz
下載完成後,使用ftp傳送到服務器上面,然後分別在三台機器上解壓
[root@sqltest01 u01]# tar -zxvf mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64.tar.gz
mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/have_plugin_auth.inc mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/kill_query.inc mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/unsafe_binlog.inc mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/have_multi_ndb.inc mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/ipv6_clients.inc mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/setup_fake_relay_log.inc mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64/mysql-test/include/wait_for_slave_sql_error_and_skip.inc ......................................................................................................
解壓後,裡面包括了數據庫文件以及集群軟件
3、配置管理節點(sqltest03)
--將剛才解壓的軟件拷貝到指定位置/usr/local/mysql
[root@sqltest03 mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64]# pwd /u01/mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64 [root@sqltest03 mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64]# mv * /usr/local/mysql/
--創建集群目錄
[root@sqltest03 u01]# cd /usr/local/mysql [root@sqltest03 mysql]# mkdir mysql-cluster [root@sqltest03 mysql]# pwd /usr/local/mysql [root@sqltest03 mysql]# cp bin/ndb_mgm* /usr/local/bin/ [root@sqltest03 mysql]# cd /var/lib [root@sqltest03 mysql]# mkdir mysql-cluster [root@sqltest03 mysql]# cd mysql-cluster [root@sqltest03 mysql]# vi config.ini
配置內容如下:
[root@sqltest03 mysql-cluster]# cat config.ini [ndbd default] NoOfReplicas=1 DataMemory=2048M IndexMemory=512M [tcp default] [ndb_mgmd] hostname=192.168.9.243 datadir=/var/lib/mysql-cluster NodeId=1 [ndbd] hostname=192.168.9.241 datadir=/u01/mysql/data NodeId=2 [ndbd] hostname=192.168.9.242 datadir=/u01/mysql/data NodeId=3 [mysqld] hostname=192.168.9.241 NodeId=4 [mysqld] hostname=192.168.9.242 NodeId=5
配置說明:
[ndbd default]
這部分是公共部分,對於每一個數據節點都有效,只需要配置一份
NoOfReplicas=1
數據鏡像幾份(各數據節點之間相互備份)
[tcp default]
針對每個數據節點及管理節點之間使用哪個端口進行通訊,在舊版本的NDB集群軟件配置時,這個地方通常配置portnumber=2202但新版的NDB軟件這裡不需要配置,並且MySQL官方也強烈建議不要配置
[ndb_mgmd]
管理節點的配置部分(通常只有一個)。注意NodeId=1指明管理節點的節點ID為1,如果不指定,在啟動集群時,會報錯
hostname=192.168.9.243
指明管理節點的IP地址
datadir=/var/lib/mysql-cluster
指明集群管理日志存放的位置
[ndbd]
數據節點配置部分,有幾個數據節點就配置幾個[ndbd]
hostname=192.168.1.111
指明數據節點的IP地址
datadir=/u01/app/mysql/data
指明數據節點上的數據庫文件存放的位置
NodeId=2
指明該數據節點在整個集群中的nodeid號(很重要)
[mysqld]
SQL節點配置部分,有幾個SQL節點,就配置幾個[mysqld]
到這裡,就可以啟動集群了
[root@sqltest03 bin]# pwd /usr/local/bin [root@sqltest03 bin]# ./ndb_mgmd -f /var/lib/mysql-cluster/config.ini MySQL Cluster Management Server mysql-5.6.21 ndb-7.3.7
進入執行查看
[root@sqltest03 bin]# ndb_mgm
-- NDB Cluster -- Management Client -- 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.9.241) id=3 (not connected, accepting connect from 192.168.9.242) [ndb_mgmd(MGM)] 1 node(s) id=1 @192.168.9.243 (mysql-5.6.21 ndb-7.3.7) [mysqld(API)] 2 node(s) id=4 (not connected, accepting connect from 192.168.9.241) id=5 (not connected, accepting connect from 192.168.9.242)
可以看到有兩個節點,節點沒有連接上
4、配置mysqld節點及存儲節點(sqltest01,sqltest02)
--建立相應目錄
[root@sqltest01 mysql]# mkdir -p /usr/local/mysql --用於存放剛才解壓的文件,如mysql的bin目錄等 [root@sqltest01 mysql]# mkdir -p /u01/mysql/data --用於存儲數據文件(innodb) [root@sqltest01 mysql]# chown -R mysql.mysql /u01
--將先前解壓的文件拷貝
[root@sqltest01 mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64]# pwd /u01/mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64 [root@sqltest01 mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64]# mv * /usr/local/mysql/ [root@sqltest01 mysql]# chown -R mysql.mysql /usr/local/mysql/
--拷貝mysql.server
[root@sqltest01 support-files]# pwd /usr/local/mysql/support-files [root@sqltest01 support-files]# ls -lrt total 32 -rw-r--r--. 1 mysql mysql 773 Oct 9 21:46 magic -rwxr-xr-x. 1 mysql mysql 10880 Oct 9 22:42 mysql.server -rwxr-xr-x. 1 mysql mysql 894 Oct 9 22:42 mysql-log-rotate -rwxr-xr-x. 1 mysql mysql 1061 Oct 9 22:42 mysqld_multi.server -rw-r--r--. 1 mysql mysql 1126 Oct 9 22:42 my-default.cnf -rwxr-xr-x. 1 mysql mysql 1153 Oct 9 22:42 binary-configure [root@sqltest01 support-files]# cp mysql.server /etc/rc.d/init.d/mysqld
--編輯環境變量
[root@sqltest01 tmp]# vi /etc/profile
添加如下:
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
[root@sqltest01 tmp]# source /etc/profile
--使修改生效
--配置my.cnf
[root@sqltest01 support-files]# cp my-default.cnf /etc/my.cnf
並對my.cnf進行配置,具體配置如下
[mysqld] ndbcluster basedir=/usr/local/mysql datadir=/u01/mysql/data port=3306 [mysql_cluster] ndb-connectstring=192.168.9.243
--初始化節點數據庫
[root@sqltest01 mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/u01/mysql/data
執行完這條命令以後,數據庫的數據文件(包括mysql,test , performance_schema等數據庫),就安裝到相應的位置了,可以使用了
在兩個節點都執行上面的步驟即可
5、在兩個點啟動
[root@sqltest01 mysql]# ndbd --initial
2014-12-24 17:55:57 [ndbd] INFO -- Angel connected to '192.168.9.243:1186' 2014-12-24 17:55:57 [ndbd] INFO -- Angel allocated nodeid: 2
第一次啟動時,需要加--initial來初始化數據節點,第二次啟動時,就不需要這個參數了
在管理節點查看,可以看到第一個節點已經連接
ndb_mgm> show
Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7, starting, Nodegroup: 0) --表明已經連接上了 id=3 (not connected, accepting connect from 192.168.9.242) [ndb_mgmd(MGM)] 1 node(s) id=1 @192.168.9.243 (mysql-5.6.21 ndb-7.3.7) [mysqld(API)] 2 node(s) id=4 (not connected, accepting connect from 192.168.9.241) id=5 (not connected, accepting connect from 192.168.9.242)
啟動mysqld
[root@sqltest01 mysql]# cd /usr/local/mysql/bin [root@sqltest01 bin]# ./mysqld_safe --user=mysql
141224 17:59:50 mysqld_safe Logging to '/u01/mysql/data/sqltest01.err'. 141224 17:59:51 mysqld_safe Starting mysqld daemon with databases from /u01/mysql/data
啟動數據庫時,第一次初始化使用的root,而這次使用mysql,需要對/u01/mysql/data權限進行配置,否則報不可讀寫
再次在管理節點查看
ndb_mgm> show
Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7, Nodegroup: 0, *) id=3 (not connected, accepting connect from 192.168.9.242) [ndb_mgmd(MGM)] 1 node(s) id=1 @192.168.9.243 (mysql-5.6.21 ndb-7.3.7) [mysqld(API)] 2 node(s) id=4 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7)
--表明已經連接上了
最後把第二節點也啟動,再次從管理節點檢查
ndb_mgm> show
Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7, Nodegroup: 0, *) id=3 @192.168.9.242 (mysql-5.6.21 ndb-7.3.7, Nodegroup: 1) [ndb_mgmd(MGM)] 1 node(s) id=1 @192.168.9.243 (mysql-5.6.21 ndb-7.3.7) [mysqld(API)] 2 node(s) id=4 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7) id=5 @192.168.9.242 (mysql-5.6.21 ndb-7.3.7)
6、在兩個節點測試
[root@sqltest01 ~]# mysql -uroot -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.21-ndb-7.3.7-cluster-gpl MySQL Cluster Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database mydb1; Query OK, 1 row affected (0.07 sec) mysql> use mydb1; Database changed mysql> create table mytb1(id int,birthdate datetime,pername char(10)) engine=ndbcluster; Query OK, 0 rows affected (0.19 sec)
mysql> insert into mytb1(id,birthdate,pername) values(1,'2013-01-23 09:45:10','pengzitong');
Query OK, 1 row affected (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> insert into mytb1(id,birthdate,pername) values(2,'2007-07-09 09:45:10','pengzixin'); Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
在第二節點檢查
[root@sqltest02 ~]# mysql -uroot -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.21-ndb-7.3.7-cluster-gpl MySQL Cluster Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mydb1 mysql> select * from mytb1; +------+---------------------+------------+ | id | birthdate | pername | +------+---------------------+------------+ | 1 | 2013-01-23 09:45:10 | pengzitong | | 2 | 2007-07-09 09:45:10 | pengzixin | +------+---------------------+------------+
7、集群停止
要想關閉 Cluster,可在MGM節點所在的機器上,在Shell中簡單地輸入下述命令:
[root@sqltest03 bin]# /usr/local/mysql/ndb_mgm -e shutdown
運行以下命令關閉SQL節點的mysqld服務:
[root@sqltest01 bin]# /usr/local/mysql/bin/mysqladmin -uroot shutdown