MySQL创建表空间

1.原因

当mysql默认的存储路径所在磁盘空间不足时,来回移动数据很是麻烦,与其后期修补,不如早起预测。其中创建MySQL表空间就是一个很好的方法。

2.语句
  • 示例1
mysql> create tablespace big_data_in_mysql add datafile 'first.ibd';
Query OK, 0 rows affected (0.57 sec)

上面这个语句就会创建一个名为big_data_in_mysql的表空间。同时这个表空间所对应的数据存放在first.ibd这个文件中。因为这里没有指定存储目录,所以使用的是默认存储路径。这时,可以到默认数据存储文件夹下查看是否创建成功
在这里插入图片描述

  • 示例2
    指定table_space的固定路径
mysql> create tablespace test_tablespace add datafile 'F:\\test_mysql_tablespace\\first.ibd';
Query OK, 0 rows affected (0.17 sec)

执行上述的SQL之后,会自动在F盘下创建一个test_mysql_tablespace 文件,执行结果如下:
在这里插入图片描述

3.查看表空间
mysql> select * from information_schema.INNODB_SYS_TABLESPACES ;
+-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+
| SPACE | NAME                            | FLAG | FILE_FORMAT | ROW_FORMAT | PAGE_SIZE | ZIP_PAGE_SIZE | SPACE_TYPE | FS_BLOCK_SIZE | FILE_SIZE | ALLOCATED_SIZE |
+-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+
|     2 | mysql/plugin                    |   33 | Barracuda   | Dynamic    |     16384 |             0 | Single     |         65536 |     98304 |          98304 |
|     3 | mysql/servers                   |   33 | Barracuda   | Dynamic    |     16384 |             0 | Single     |         65536 |     98304 |          98304 |
|     4 | mysql/help_topic                |   33 | Barracuda   | Dynamic    |     16384 |             0 | Single     |         65536 |   9437184 |        9437184 |
····
|    93 | bas/t_bas_shop_item             |   33 | Barracuda   | Dynamic    |     16384 |             0 | Single     |         65536 |    163840 |         163840 |
|   119 | insidemysql/student#p#p2        |   33 | Barracuda   | Dynamic    |     16384 |             0 | Single     |         65536 |     98304 |          98304 |
|   120 | insidemysql/t                   |   33 | Barracuda   | Dynamic    |     16384 |             0 | Single     |         65536 |     98304 |          98304 |
|   122 | test_tablespace                 | 2048 | Any         | Any        |     16384 |             0 | General    |         65536 |     65536 |          65536 |
|   125 | big_data_in_mysql               | 2048 | Any         | Any        |     16384 |             0 | General    |         65536 |     65536 |          65536 |
+-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+
53 rows in set (0.03 sec)

可以看到最后两行就是我刚才创建的表空间。

4. 删除表空间
DROP TABLESPACE tablespace_name
[ENGINE [=] engine_name]
Logo

更多推荐