MySQL 5.7新支援--------通用資料表空間實戰

來源:互聯網
上載者:User

標籤:mysql dba   general   tablespace   

1. 背景

  * 一個通用的資料表空間是一個共用的InnoDB資料表空間。

   * 與系統資料表空間類似,一般的資料表空間是共用的資料表空間,可以儲存多個表的資料

   * 一般的資料表空間比檔案表的資料表空間具有潛在的記憶體優勢。

   * MySQL 將資料表空間中繼資料儲存到一個資料表空間的生命週期中。在更少的一般資料表空間中,多個表對錶空間中繼資料的記憶體比在單獨的檔案資料表空間資料表空間中的相同數量的表要少。    

   * 一般的資料表空間資料檔案可能放在一個相對於MySQL資料目錄的目錄中,它為您提供了許多檔案資料表空間的資料檔案和儲存管理功能。與檔案表的資料表空間一樣,在MySQL資料目錄之外放置資料檔案的能力允許您單獨管理關鍵表的效能,為特定的表設定RAID或DRBD,或者將表綁定到特定的磁碟。

   * MySQL 5.7開始支援通用資料表空間管理功能。


2. MySQL 環境

Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.18 MySQL Community Server (GPL)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> show variables like ‘version‘;+---------------+--------+| Variable_name | Value  |+---------------+--------+| version       | 5.7.18 |+---------------+--------+1 row in set (0.01 sec)mysql> show variables like ‘datadir‘;+---------------+-------------------+| Variable_name | Value             |+---------------+-------------------+| datadir       | /data/mysql_data/ |+---------------+-------------------+1 row in set (0.04 sec)


3. 建立通用資料表空間

  * 建立資料表空間檔案存放目錄

[[email protected] mytest]# mkdir -v /mysql_general_datamkdir: created directory `/mysql_general_data‘


   * 查看mysqld 運行使用者

[[email protected] mytest]# ps aux | grep mysqld | grep -v greproot       1468  0.0  0.0 110400  1532 ?        S    16:00   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql_data --pid-file=/data/mysql_data/MySQL.pidmysql      1614  0.1  5.0 1309380 196656 ?      Sl   16:00   0:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql_data/error.log --pid-file=/data/mysql_data/MySQL.pid


   * 修改資料表空間檔案存放目錄所屬使用者與組為mysql運行使用者 [ 此步必須 ]

[[email protected] mytest]# chown -v mysql.mysql /mysql_general_data changed ownership of `/mysql_general_data‘ to mysql:mysql


   * 建立通用資料表空間

   ADD datafile: 指定通用資料表空間檔案存放路徑

     FILE_BLOCK_SIZE: 指定檔案塊大小,推薦與Innodb_page_size參數大小對應

     ENGINE: 指定儲存引擎

mysql> CREATE TABLESPACE ts1 ADD datafile ‘/mysql_general_data/ts1.ibd‘ FILE_BLOCK_SIZE=16384  ENGINE=InnoDB;Query OK, 0 rows affected (0.06 sec)mysql> show variables like ‘innodb_page_size‘;+------------------+-------+| Variable_name    | Value |+------------------+-------+| innodb_page_size | 16384 |+------------------+-------+1 row in set (0.02 sec)


  * 查看通用資料表空間檔案

mysql> system ls -l /mysql_general_data;total 64-rw-r----- 1 mysql mysql 65536 Jul  5 17:15 ts1.ibd


4. 測試通用資料表空間檔案

  * 使用通用資料表空間作為資料存放區建立表

mysql> CREATE TABLE test_general(    -> id BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT,    -> name VARCHAR(64) NOT NULL    -> )ENGINE=InnoDB TABLESPACE=ts1 DEFAULT CHARSET=utf8mb4;Query OK, 0 rows affected (0.04 sec)


  * 查看錶資訊

mysql> show create table test_general;+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table        | Create Table                                                                                                                                                                                      |+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| test_general | CREATE TABLE `test_general` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `name` varchar(64) NOT NULL,  PRIMARY KEY (`id`)) /*!50100 TABLESPACE `ts1` */ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.06 sec)


  * 查看錶檔案

mysql> select database();+------------+| database() |+------------+| mytest     |+------------+1 row in set (0.01 sec)mysql> system ls -l /data/mysql_data/mytest;total 16-rw-r----- 1 mysql mysql   67 Jul  5 16:30 db.opt-rw-r----- 1 mysql mysql 8586 Jul  5 17:19 test_general.frm


5. 刪除資料表空間檔案

   * 有表佔用時直接刪除

mysql> drop tablespace ts1;ERROR 1529 (HY000): Failed to drop TABLESPACE ts1


  * 先刪除佔用表,再刪除

mysql> drop table test_general;Query OK, 0 rows affected (0.04 sec)mysql> drop tablespace ts1;Query OK, 0 rows affected (0.03 sec)


6. 總結


以需求驅動技術,技術本身沒有優略之分,只有業務之分。

本文出自 “sea” 部落格,請務必保留此出處http://lisea.blog.51cto.com/5491873/1945446

MySQL 5.7新支援--------通用資料表空間實戰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.