《MysqL教程MysqL的集群配置的基本命令使用及一次操作过程实录》要点:
本文介绍了MysqL教程MysqL的集群配置的基本命令使用及一次操作过程实录,希望对您有用。如果有疑问,可以联系我们。
减少数据中心结点压力和大数据量处理,采用把MysqL分布,一个或多个application对应一个MysqL数据库.把几个MysqL数据库公用的数据做出共享数据,例如购物车,用户对象等等,存在数据结点里面.其他不共享的数据还维持在各自分布的MysqL数据库本身中.
MysqL学习
1)sql结点(sql node--上图对应为MysqLd):分布式数据库.包括自身数据和查询中心结点数据.MysqL学习
2)数据结点(Data node -- ndbd):集群共享数据(内存中).MysqL学习
3)管理服务器(Management Server C ndb_mgmd):集群管理sql node,Data node.MysqL学习
3.配置MysqL学习
MysqL-max版本,当然现在MysqL集群系统windonws平台上面不被支持.MysqL学习
安装MysqL就不多说了,网上一大堆,简明扼要.MysqL学习
- A:192.168.1.251 C Data node和Management Server.
- B:192.168.1.254 C sql node.
[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. 启动测试MysqL学习
在管理服务器上面(这里是192.168.1.251):MysqL学习
在数据结点服务器上面(依然是192.168.1.251and more):MysqL学习
shell>ndbd--initial
(第一次时加--initial参数)
sql结点服务器上面(192.168.1.254):MysqL学习
shell>MysqLd &
在251上面察看MysqL学习
./ndb_mgm
MysqL学习
-- 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.基本的集群说明MysqL学习
1)在MysqL集群中.当table引擎为NDBCLUSTER时才做集群,其他非NDBCLUSTER表和一般MysqL数据库表一样,不会共享数据. NDBCLUSTER表数据存储在Data node服务器内存中,Data Node可以为1台或多台服务器,它们之间存放共享数据.Data Node服务器可以分组数据copy.MysqL学习
例如:2,3,4,5为四台Data Node服务器ID. 2,3为组0. 4,5为组1. 2,3维持数据相同,5维持数据相同. 组0和组1维持数据不同.MysqL学习
2)sql node服务器中,非NDBCLUSTER数据存在本身数据库中,table引擎为NDBCLUSTER时,数据存储在Data Node中.当查询NDBCLUSTER表时,它会从Data node集群中提起数据.MysqL学习
3)Manager serverMysqL学习
管理sql node和Data node状态.MysqL学习
附: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等
MysqL学习
[root@sqltest01 u01]# service iptables status
iptables: Firewall is not running.
如果开启的,请使用service iptables stop
--创建相应的用户及目录
MysqL学习
[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传送到服务器上面,然后分别在三台机器上解压
MysqL学习
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
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/
--创建集群目录
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]
到这里,就可以启动集群了
MysqL学习
[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
进入执行查看
MysqL学习
[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)
--建立相应目录
MysqL学习
[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/
[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
--编辑环境变量
MysqL学习
[root@sqltest01 tmp]# vi /etc/profile
export PATH
[root@sqltest01 tmp]# source /etc/profile
[root@sqltest01 support-files]# cp my-default.cnf /etc/my.cnf
并对my.cnf进行配置,具体配置如下
MysqL学习
[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、在两个点启动
MysqL学习
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来初始化数据节点,第二次启动时,就不需要这个参数了
在管理节点查看,可以看到第一个节点已经连接
MysqL学习
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)
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权限进行配置,否则报不可读写
再次在管理节点查看
MysqL学习
ndb_mgm> show
Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7,*) 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)
--表明已经连接上了
最后把第二节点也启动,再次从管理节点检查
MysqL学习
ndb_mgm> show
Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @192.168.9.241 (mysql-5.6.21 ndb-7.3.7,*) 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、在两个节点测试
MysqL学习
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,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)
在第二节点检查
MysqL学习
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,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中简单地输入下述命令:
MysqL学习
运行以下命令关闭sql节点的MysqLd服务:
MysqL学习
编程之家PHP培训学院每天发布《MysqL教程MysqL的集群配置的基本命令使用及一次操作过程实录》等实战技能,PHP、MysqL、LINUX、APP、JS,CSS全面培养人才。