Heim  >  Artikel  >  Datenbank  >  MySQL Cluster创建磁盘表_MySQL

MySQL Cluster创建磁盘表_MySQL

WBOY
WBOYOriginal
2016-06-01 13:39:11926Durchsuche

bitsCN.com

 

一、概念

MySQL Cluster采用一系列的Disk Data objects来实现磁盘表。

    Tablespaces:作用是作为其他Disk Data objects的容器。

    Undo log files:存储事务进行回滚需要的信息,一个或者多个undo log files组成一个log files group,最后,该log file group关联到一个tablespaces。

    Data files:作用是存储表中的数据,data file直接关联到tablespaces。

    在每一个数据节点上undo log files和data files都是实际的文件,默认的,存放在ndb_node_id_fs文件夹下,该路径是在MySQL Cluster的config.ini中用DataDir指定的,node_id是data node的node ID。可以用绝对路径或者相对路径指定undo log或者data file的路径。tablespaces和log file group则不是实际的文件。

    注意:尽管不是所有的Disk Data Object都作为文件存储,但是他们共享同一的命名空间,这意味着每个Disk Data Object必须唯一命名。

二、创建步骤

MySQL Cluster创建一个磁盘表需要包含以下几步:

    2.1、创建一个log file group,将一个或者多个undo log files关联到它上面(undo log file也叫做 undofile)。注意,undo log file只是在创建磁盘表的时候才需要,创建ndb的内存表的时候不需要。

    2.2、创建一个tablespaces,关联一个log file group和一个或者多个data files到上面。

    2.3、使用该tablespaces创建一个磁盘表存储数据。

下面做一个例子:

    2.4、创建log file group:

    我们创建一个名为lg_cloudstor的log file group,包含cloudstor_undo_1.log和cloudstor_undo_2.log两个undo log file。初始大小分别为200M和100M(默认的undo log file的初始大小为128M),你还可以指定log file group的重写缓冲大小(默认为8M),这里我们设置为20M。创建log file group的时候必须和一个undo log file一起创建。如下:

CREATE LOGFILE GROUP lg_cloudstorCREATE LOGFILE GROUP lg_cloudstor    ADD UNDOFILE 'cloudstor_undo_1.log'    INITIAL_SIZE 200M    UNDO_BUFFER_SIZE 20M    ENGINE NDBCLUSTER;


 

    该语句可能会出错,出现ERROR 1064 (42000)语法错误的信息,原因是字符集的问题,先执行:set character_set_client=latin1; 在执行:

1     添加新的undo log file:

ALTER LOGFILE GROUP lg_cloudstorALTER LOGFILE GROUP lg_cloudstor    ADD UNDOFILE 'cloudstor_undo_2.log'    INITIAL_SIZE 100M    ENGINE NDBCLUSTER;


 

    2.5、创建tablespace

    下来我们创建一个tablespace,创建tablespace必须指明一个log file group用于存储undo log。还必须指定一个data file,tablespace创建完成后,你可以稍后添加更多的data file到tablespace。下来我们创建一个使用log file group为lg_cloudstor名为 ts_cloudstore的表空间,这个表空间包含cloudstore_data_1.dbf和cloudstore_data_2.dbf两个data file,初始大小为100M(默认为128M),自动扩展为10M。

CREATE TABLESPACE ts_cloudstoreCREATE TABLESPACE ts_cloudstore    ADD DATAFILE 'cloudstore_data_1.dbf'    USE LOGFILE GROUP lg_cloudstor    INITIAL_SIZE 100M    AUTOEXTEND_SIZE 10M    ENGINE NDBCLUSTER;

 

    添加新的datafile

ALTER TABLESPACE ts_cloudstoreALTER TABLESPACE ts_cloudstore    ADD DATAFILE 'cloudstore_data_2.dbf'    INITIAL_SIZE 100M    AUTOEXTEND_SIZE 10M    ENGINE NDBCLUSTER;

下来我们可以看下新建的这些文件在物理磁盘上的数据文件:

2

    2.6、创建磁盘表

    下来创建一个将没有索引的列存储在表空间ts_cloudstore的磁盘表。

create table Disktablecreate table Disktable(	id int  auto_increment primary key,	c1 varchar(50) not null,	c2 varchar(30) default null,	c3 date default null,	index(c1))TABLESPACE ts_cloudstore STORAGE DISKENGINE NDBCLUSTER;

这样,列c2,c3的数据将会存储在磁盘上,id和c1的数据仍会存储在内存中,因为只有没有索引的列才能存储在磁盘上。创建完成后,就可以进行正常的数据操作了。

    log file group,tablespaces,Disk Data tables需要按照一定的顺序执行,删除这些的时候也是这样,删除规则为:有任何表空间使用log file group时,log file group不能删除;表空间含有任何data files的时候,不能删除表空间;Data files含有任何表残余的时候,不能从表空间删除数据文件。

    2.7、删除数据表

drop table Disktable;drop table Disktable;

3    2.8、删除数据文件

     当没有表在关联到表空间的时候,我们一个一个删除data files。

drop data filesalter tablespace ts_cloudstore 	drop datafile 'cloudstore_data_1.dbf'	engine ndbcluster;alter tablespace ts_cloudstore 	drop datafile 'cloudstore_data_2.dbf'	engine ndbcluster;

4

    2.9、删除表空间

drop tablespacedrop tablespace ts_cloudstore 	engine ndbcluster;

    2.10、删除日志组

drop logfile groupdrop logfile group lg_cloudstor	engine ndbcluster;

    下来查看物理文件是否已经删除:

    5       

    可以在INFORMATION_SCHEMA数据库中的FILES表中查看磁盘表的信息。

    2.11、几点注意的

    在磁盘数据表中,TEXT和BLOB列的前256bytes存储在内存中,剩余内容存储在磁盘上;磁盘表中的每一行需要在内存中存储8bytes的数据用于指向磁盘上的数据,使用--initial选项启动集群时,不会删除磁盘上的数据文件,必须手动删除。

bitsCN.com
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn