设为首页 收藏本站
查看: 602|回复: 0

[经验分享] Mysql5.6—分区表及独享表空间

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-20 10:52:28 | 显示全部楼层 |阅读模式
  1、先停止Mysql
  [root@localhost ~]# /etc/init.d/mysqld stop
  Shutting down MySQL.[确定]

   2、修改 /etc/my.cnf配置文件
  [root@localhost ~]# vi /etc/my.cnf
  [mysqld]
innodb_file_per_table = 1
  3、启动Mysql
  [root@localhost ~]# /etc/init.d/mysqld start
  Starting MySQL..[确定]
  4、连接到Mysql数据库
  [root@localhost ~]# mysql -uroot -p
  Enter password:
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 1
  Server version: 5.5.22-log Source distribution
  Copyright (c) 2000, 2011, 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> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | dbmonitor          |
      | mysql              |
      | performance_schema |
      | test               |
      +--------------------+
     5 rows in set (0.01 sec)

  mysql> show plugins;
    +-----------------------+--------+--------------------+---------+---------+
    | Name                  | Status | Type               | Library | License |
    +-----------------------+--------+--------------------+---------+---------+
    | binlog                | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | mysql_native_password | ACTIVE | AUTHENTICATION     | NULL    | GPL     |
    | mysql_old_password    | ACTIVE | AUTHENTICATION     | NULL    | GPL     |
    | MEMORY                | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | MRG_MYISAM            | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | MyISAM                | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | CSV                   | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | InnoDB                | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | INNODB_TRX            | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | INNODB_LOCKS          | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | INNODB_LOCK_WAITS     | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | INNODB_CMP            | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | INNODB_CMP_RESET      | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | INNODB_CMPMEM         | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | INNODB_CMPMEM_RESET   | ACTIVE | INFORMATION SCHEMA | NULL    | GPL     |
    | PERFORMANCE_SCHEMA    | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    | partition             | ACTIVE | STORAGE ENGINE     | NULL    | GPL     |
    +-----------------------+--------+--------------------+---------+---------+

  mysql> use dbmonitor;
  Database changed
  5、创建分区表(history_text):
mysql> create table history_text
  (
    id bigint(20) unsigned default null,
    itemid bigint(20) unsigned default null,
    clock int(11) default 0,
    value text default null,
    ns int(11) default 0,
    index itemid_idx (itemid),
    primary key(id,clock)
  ) engine=Innodb
  partition by range (clock)
  (
    partition p1_201502 values less than (unix_timestamp('2015-03-01')),
    partition p1_201503 values less than (unix_timestamp('2015-04-01')),
    partition p1_201504 values less than (unix_timestamp('2015-05-01')),
    partition p1_201505 values less than (unix_timestamp('2015-06-01')),
    partition p1_201506 values less than (unix_timestamp('2015-07-01')),
    partition p1_201507 values less than (unix_timestamp('2015-08-01')),
    partition p1_201508 values less than (unix_timestamp('2015-09-01')),
    partition p1_201509 values less than (unix_timestamp('2015-10-01')),
    partition p1_201510 values less than (unix_timestamp('2015-11-01')),
    partition p1_201511 values less than (unix_timestamp('2015-12-01')),
    partition p1_201512 values less than (unix_timestamp('2016-01-01')),
    partition p1_201601 values less than (unix_timestamp('2016-02-01')),
    partition p1_201602 values less than (unix_timestamp('2016-03-01')),
    partition p1_201603 values less than (unix_timestamp('2016-04-01')),
    partition p1_201604 values less than (unix_timestamp('2016-05-01')),
    partition p1_201605 values less than (unix_timestamp('2016-06-01')),
    partition p1_201606 values less than (unix_timestamp('2016-07-01')),
    partition p1_201607 values less than (unix_timestamp('2016-08-01')),
    partition p1_201608 values less than (unix_timestamp('2016-09-01')),
    partition p1_201609 values less than (unix_timestamp('2016-10-01')),
    partition p1_201610 values less than (unix_timestamp('2016-11-01')),
    partition p1_201611 values less than (unix_timestamp('2016-12-01')),
    partition p1_other_all values less than maxvalue
  );


    mysql>alter table history_text add primary key(id,clock);
  6、检查数据数据库配置参数是否生效。    [root@localhost data]# ll
    总计 10900348
    drwx------ 2 mysql mysql       4096 03-20 09:40 dbmonitor
    -rw-rw---- 1 mysql mysql 7266631680 03-20 10:29 ibdata1
    -rw-rw---- 1 mysql mysql    5242880 03-20 10:30 ib_logfile0
    -rw-rw---- 1 mysql mysql    5242880 03-20 10:30 ib_logfile1
    drwxr-xr-x 2 mysql root        4096 03-18 00:18 mysql
    -rw-rw---- 1 mysql mysql      27338 03-18 00:18 mysql-bin.000001
    [root@localhost ~]# cd /usr/local/mysql/data/dbmonitor/
    [root@localhost dbmonitor]# ll
    总计 5112
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201503.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:34 history_text#P#p1_201504.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201505.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 10:06 history_text#P#p1_201506.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201507.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201508.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201509.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201510.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201511.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201512.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201601.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201602.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201603.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201604.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201605.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201606.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201607.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201608.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201609.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201610.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201611.ibd
    -rw-rw---- 1 mysql mysql 131072 03-18 09:35 history_text#P#p1_other_all.ibd

  7、查看分区表。
    mysql> select table_name, partition_name from information_schema.partitions
    -> where table_schema='dbmonitor' and table_name='history_text';
    +--------------+----------------+
    | table_name   | partition_name |
    +--------------+----------------+
    | history_text | p1_201503      |
    | history_text | p1_201504      |
    | history_text | p1_201505      |
    | history_text | p1_201506      |
    | history_text | p1_201507      |
    | history_text | p1_201508      |
    | history_text | p1_201509      |
    | history_text | p1_201510      |
    | history_text | p1_201511      |
    | history_text | p1_201512      |
    | history_text | p1_201601      |
    | history_text | p1_201602      |
    | history_text | p1_201603      |
    | history_text | p1_201604      |
    | history_text | p1_201605      |
    | history_text | p1_201606      |
    | history_text | p1_201607      |
    | history_text | p1_201608      |
    | history_text | p1_201609      |
    | history_text | p1_201610      |
    | history_text | p1_201611      |
    | history_text | p1_other_all   |
    +--------------+----------------+
    22 rows in set (0.01 sec)

    8、插入数据测试分区表。
    mysql> delimiter //
    mysql> create procedure insert_history()
        begin
           declare var int;
        set var=0;
        while var<100000 do
        insert into history_text values(var,var,1430496000,var,var);
        set var=var+1;
        end while;
        end;
        //


    mysql> delimiter ;

    mysql> call insert_test();
    Query OK, 1 row affected (2 min 46.97 sec)
  9、查看数据文件大小的变化。
[root@localhost data]# cd dbmonitor/
[root@localhost dbmonitor]# ll
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201503.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:34 history_text#P#p1_201504.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201505.ibd
-rw-rw---- 1 mysql mysql 19922944 03-20 10:54 history_text#P#p1_201506.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201507.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201508.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201509.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201510.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201511.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201512.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201601.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201602.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201603.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201604.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201605.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201606.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201607.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201608.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201609.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201610.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:21 history_text#P#p1_201611.ibd
-rw-rw---- 1 mysql mysql   131072 03-18 09:35 history_text#P#p1_other_all.ibd

[root@localhost data]# ll
总计 10919648
drwx------ 2 mysql mysql       4096 03-20 09:40 dbmonitor
-rw-rw---- 1 mysql mysql 7266631680 03-20 10:55 ibdata1
-rw-rw---- 1 mysql mysql    5242880 03-20 10:55 ib_logfile0
-rw-rw---- 1 mysql mysql    5242880 03-20 10:55 ib_logfile1
drwxr-xr-x 2 mysql root        4096 03-18 00:18 mysql
-rw-rw---- 1 mysql mysql      27338 03-18 00:18 mysql-bin.000001
-rw-rw---- 1 mysql mysql    1031892 03-18 00:18 mysql-bin.000002
-rw-rw---- 1 mysql mysql        126 03-18 00:23 mysql-bin.000003
-rw-rw---- 1 mysql mysql        126 03-18 00:25 mysql-bin.000004
-rw-rw---- 1 mysql mysql  703646366 03-18 00:58 mysql-bin.000005
-rw-rw---- 1 mysql mysql 1074397884 03-18 02:48 mysql-bin.000006
-rw-rw---- 1 mysql mysql 1073854162 03-18 04:00 mysql-bin.000007
-rw-rw---- 1 mysql mysql  973199491 03-18 04:43 mysql-bin.000008
-rw-rw---- 1 mysql mysql        126 03-18 09:19 mysql-bin.000009
-rw-rw---- 1 mysql mysql   41826406 03-18 11:01 mysql-bin.000010
-rw-rw---- 1 mysql mysql        126 03-18 11:06 mysql-bin.000011
-rw-rw---- 1 mysql mysql        126 03-18 14:03 mysql-bin.000012
-rw-rw---- 1 mysql mysql       2175 03-18 16:48 mysql-bin.000013
-rw-rw---- 1 mysql mysql        107 03-18 23:06 mysql-bin.000014
-rw-rw---- 1 mysql mysql        107 03-19 08:49 mysql-bin.000015
-rw-rw---- 1 mysql mysql    5720758 03-20 10:29 mysql-bin.000016
-rw-rw---- 1 mysql mysql   19741312 03-20 10:55 mysql-bin.000017
-rw-rw---- 1 mysql mysql        323 03-20 10:30 mysql-bin.index
drwx------ 2 mysql mysql       4096 03-18 00:18 performance_schema
drwxr-xr-x 2 mysql root        4096 03-18 15:14 test
注释:发现只有单独的表空间数据文件在增大,mysql的元数据文件不变化。
10、删除分区表中的数据,减少数据文件的大小,清理空间。
mysql> alter table history_text truncate partition p1_201506;
Query OK, 0 rows affected (0.09 sec)
[root@localhost dbmonitor]# ll
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201503.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:34 history_text#P#p1_201504.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201505.ibd
-rw-rw---- 1 mysql mysql 131072 03-20 11:01 history_text#P#p1_201506.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201507.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201508.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201509.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201510.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201511.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201512.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201601.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201602.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201603.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201604.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201605.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201606.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201607.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201608.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201609.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201610.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:21 history_text#P#p1_201611.ibd
-rw-rw---- 1 mysql mysql 131072 03-18 09:35 history_text#P#p1_other_all.ibd


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88631-1-1.html 上篇帖子: mysql 编译错误 下篇帖子: MySQL的备份与恢复 空间
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表