mysql-5.7 收缩系统表空间详解
innodb 系统表空间是一个逻辑上的概念,它的物理表现就是innodb系统表空间文件;在讲扩展系统表空间时我们说到
可以用增加文件,增加autoextend标记 这两种方式来解决;但是问题到了收缩表空间时就变的不一般了,如果直接删掉
系统表空间文件就意味着数据的丢失,innodb系统表空间文件也没有提供收缩的标记可以打,也没有提供可以收缩表空间
的SQL语句。难道就没有能收缩系统表空间的办法了吗?办法是有的只是比较暴力,这个办法就是重建一个新实例。
一、官方文档中给出的收缩表空间的方法&步骤:
1、用mysqldump 备份出所有的innodb表,这里要特别注意mysql这个系统库中有部分表也是innodb的,所以这些表
也是要备份出来的。
2、关闭mysql数据库服务。
3、删除所有innodb表的*idb,*frm文件 ;删除系统表空间文件ibdata*.idb ,删除innodb的日志文件ib_log 文件。
4、修改配置文件中系统表空间的配置到想要的大小。
5、启动mysql服务
6、导入第一.1目中导出的数据
二、实战(对一个系统表空间为32M的实例进行表空间收缩,收缩到12M)
1、确定实例中有哪些表的引擎是innodb
select table_schema,table_name
-> from information_schema.tables
-> where engine='innodb' and table_schema != 'information_schema';
+--------------+---------------------------+
| table_schema | table_name |
+--------------+---------------------------+
| appdb | person |
| mysql | engine_cost |
| mysql | gtid_executed |
| mysql | help_category |
| mysql | help_keyword |
| mysql | help_relation |
| mysql | help_topic |
| mysql | innodb_index_stats |
| mysql | innodb_table_stats |
| mysql | plugin |
| mysql | server_cost |
| mysql | servers |
| mysql | slave_master_info |
| mysql | slave_relay_log_info |
| mysql | slave_worker_info |
| mysql | time_zone |
| mysql | time_zone_leap_second |
| mysql | time_zone_name |
| mysql | time_zone_transition |
| mysql | time_zone_transition_type |
| sys | sys_config |
+--------------+---------------------------+
这里是一个非常尴尬的地方 a、官方文档上明确指出了要dump出所有innodb表的数据,但是呢它自己的操作中只是查看了mysql这一个
系统库中的innodb表;事实上对于mysql5.7来说sys库和informations-schema都能查出它们有innodb表。
b、不管sys还是information_schema都它们的表都是虚的,也就是说这两个库不会有数据文件被保存在磁盘中;在下面操作中我只会
dump出mysql库和appdb 这个库中的innodb表;这样的话我操作的表的个数就相对少一些,也算是测试一下只dump mysql库和业务
库能不能完成收缩的任务
2、拼接出导出时dump的语句和导入时的source语句:
mysql> select concat('mysqldump --set-gtid-purged=OFF ',table_schema,' ',table_name,
-> ' > /tmp/',table_schema,'/',table_name,'.sql') as mysqldump_cmd
-> from information_schema.tables
-> where engine='innodb' and table_schema not in('information_schema','sys');
+------------------------------------------------------------------------------------------------------------+
| mysqldump_cmd |
+------------------------------------------------------------------------------------------------------------+
| mysqldump --set-gtid-purged=OFF appdb person > /tmp/appdb/person.sql |
| mysqldump --set-gtid-purged=OFF mysql engine_cost > /tmp/mysql/engine_cost.sql |
| mysqldump --set-gtid-purged=OFF mysql gtid_executed > /tmp/mysql/gtid_executed.sql |
| mysqldump --set-gtid-purged=OFF mysql help_category > /tmp/mysql/help_category.sql |
| mysqldump --set-gtid-purged=OFF mysql help_keyword > /tmp/mysql/help_keyword.sql |
| mysqldump --set-gtid-purged=OFF mysql help_relation > /tmp/mysql/help_relation.sql |
| mysqldump --set-gtid-purged=OFF mysql help_topic > /tmp/mysql/help_topic.sql |
| mysqldump --set-gtid-purged=OFF mysql innodb_index_stats > /tmp/mysql/innodb_index_stats.sql |
| mysqldump --set-gtid-purged=OFF mysql innodb_table_stats > /tmp/mysql/innodb_table_stats.sql |
| mysqldump --set-gtid-purged=OFF mysql plugin > /tmp/mysql/plugin.sql |
| mysqldump --set-gtid-purged=OFF mysql server_cost > /tmp/mysql/server_cost.sql |
| mysqldump --set-gtid-purged=OFF mysql servers > /tmp/mysql/servers.sql |
| mysqldump --set-gtid-purged=OFF mysql slave_master_info > /tmp/mysql/slave_master_info.sql |
| mysqldump --set-gtid-purged=OFF mysql slave_relay_log_info > /tmp/mysql/slave_relay_log_info.sql |
| mysqldump --set-gtid-purged=OFF mysql slave_worker_info > /tmp/mysql/slave_worker_info.sql |
| mysqldump --set-gtid-purged=OFF mysql time_zone > /tmp/mysql/time_zone.sql |
| mysqldump --set-gtid-purged=OFF mysql time_zone_leap_second > /tmp/mysql/time_zone_leap_second.sql |
| mysqldump --set-gtid-purged=OFF mysql time_zone_name > /tmp/mysql/time_zone_name.sql |
| mysqldump --set-gtid-purged=OFF mysql time_zone_transition > /tmp/mysql/time_zone_transition.sql |
| mysqldump --set-gtid-purged=OFF mysql time_zone_transition_type > /tmp/mysql/time_zone_transition_type.sql |
+------------------------------------------------------------------------------------------------------------+
mysql> select concat('source ','/tmp/',table_schema,'/',table_name,'.sql ;') as source_cmd
-> from information_schema.tables
-> where engine='innodb' and table_schema not in('information_schema','sys');
+---------------------------------------------------+
| source_cmd |
+---------------------------------------------------+
| source /tmp/appdb/person.sql ; |
| source /tmp/mysql/engine_cost.sql ; |
| source /tmp/mysql/gtid_executed.sql ; |
| source /tmp/mysql/help_category.sql ; |
| source /tmp/mysql/help_keyword.sql ; |
| source /tmp/mysql/help_relation.sql ; |
| source /tmp/mysql/help_topic.sql ; |
| source /tmp/mysql/innodb_index_stats.sql ; |
| source /tmp/mysql/innodb_table_stats.sql ; |
| source /tmp/mysql/plugin.sql ; |
| source /tmp/mysql/server_cost.sql ; |
| source /tmp/mysql/servers.sql ; |
| source /tmp/mysql/slave_master_info.sql ; |
| source /tmp/mysql/slave_relay_log_info.sql ; |
| source /tmp/mysql/slave_worker_info.sql ; |
| source /tmp/mysql/time_zone.sql ; |
| source /tmp/mysql/time_zone_leap_second.sql ; |
| source /tmp/mysql/time_zone_name.sql ; |
| source /tmp/mysql/time_zone_transition.sql ; |
| source /tmp/mysql/time_zone_transition_type.sql ; |
+---------------------------------------------------+
3、执行导出操作
#创建导出后数据要保存到的目录
mkdir /tmp/mysql/
mkdir /tmp/appdb/ #执行导出操作
mysqldump --set-gtid-purged=OFF appdb person > /tmp/appdb/person.sql
mysqldump --set-gtid-purged=OFF mysql engine_cost > /tmp/mysql/engine_cost.sql
mysqldump --set-gtid-purged=OFF mysql gtid_executed > /tmp/mysql/gtid_executed.sql
mysqldump --set-gtid-purged=OFF mysql help_category > /tmp/mysql/help_category.sql
mysqldump --set-gtid-purged=OFF mysql help_keyword > /tmp/mysql/help_keyword.sql
mysqldump --set-gtid-purged=OFF mysql help_relation > /tmp/mysql/help_relation.sql
mysqldump --set-gtid-purged=OFF mysql help_topic > /tmp/mysql/help_topic.sql
mysqldump --set-gtid-purged=OFF mysql innodb_index_stats > /tmp/mysql/innodb_index_stats.sql
mysqldump --set-gtid-purged=OFF mysql innodb_table_stats > /tmp/mysql/innodb_table_stats.sql
mysqldump --set-gtid-purged=OFF mysql plugin > /tmp/mysql/plugin.sql
mysqldump --set-gtid-purged=OFF mysql server_cost > /tmp/mysql/server_cost.sql
mysqldump --set-gtid-purged=OFF mysql servers > /tmp/mysql/servers.sql
mysqldump --set-gtid-purged=OFF mysql slave_master_info > /tmp/mysql/slave_master_info.sql
mysqldump --set-gtid-purged=OFF mysql slave_relay_log_info > /tmp/mysql/slave_relay_log_info.sql
mysqldump --set-gtid-purged=OFF mysql slave_worker_info > /tmp/mysql/slave_worker_info.sql
mysqldump --set-gtid-purged=OFF mysql time_zone > /tmp/mysql/time_zone.sql
mysqldump --set-gtid-purged=OFF mysql time_zone_leap_second > /tmp/mysql/time_zone_leap_second.sql
mysqldump --set-gtid-purged=OFF mysql time_zone_name > /tmp/mysql/time_zone_name.sql
mysqldump --set-gtid-purged=OFF mysql time_zone_transition > /tmp/mysql/time_zone_transition.sql
mysqldump --set-gtid-purged=OFF mysql time_zone_transition_type > /tmp/mysql/time_zone_transition_type.sql
[root@cstudio ]# ll /tmp/mysql
总用量
-rw-r--r--. root root 9月 : engine_cost.sql
-rw-r--r--. root root 9月 : gtid_executed.sql
-rw-r--r--. root root 9月 : help_category.sql
-rw-r--r--. root root 9月 : help_keyword.sql
-rw-r--r--. root root 9月 : help_relation.sql
-rw-r--r--. root root 9月 : help_topic.sql
-rw-r--r--. root root 9月 : innodb_index_stats.sql
-rw-r--r--. root root 9月 : innodb_table_stats.sql
-rw-r--r--. root root 9月 : plugin.sql
-rw-r--r--. root root 9月 : server_cost.sql
-rw-r--r--. root root 9月 : servers.sql
-rw-r--r--. root root 9月 : slave_master_info.sql
-rw-r--r--. root root 9月 : slave_relay_log_info.sql
-rw-r--r--. root root 9月 : slave_worker_info.sql
-rw-r--r--. root root 9月 : time_zone_leap_second.sql
-rw-r--r--. root root 9月 : time_zone_name.sql
-rw-r--r--. root root 9月 : time_zone.sql
-rw-r--r--. root root 9月 : time_zone_transition.sql
-rw-r--r--. root root 9月 : time_zone_transition_type.sql
[root@cstudio ]# ll /tmp/appdb/
总用量
-rw-r--r--. root root 9月 : person.sql
4、关闭mysql数据库并删除innodb相关文件:
#关闭mysql服务
systemctl stop mysql #删除innodb表相关的文件 #
rm -rf appdb/person.*
rm -rf mysql/engine_cost.*
rm -rf mysql/gtid_executed.*
rm -rf mysql/help_category.*
rm -rf mysql/help_keyword.*
rm -rf mysql/help_relation.*
rm -rf mysql/help_topic.*
rm -rf mysql/innodb_index_stats.*
rm -rf mysql/innodb_table_stats.*
rm -rf mysql/plugin.*
rm -rf mysql/server_cost.*
rm -rf mysql/servers.*
rm -rf mysql/slave_master_info.*
rm -rf mysql/slave_relay_log_info.*
rm -rf mysql/slave_worker_info.*
rm -rf mysql/time_zone.*
rm -rf mysql/time_zone_leap_second.*
rm -rf mysql/time_zone_name.*
rm -rf mysql/time_zone_transition.*
rm -rf mysql/time_zone_transition_type.* #2
rm -rf ibdata1
rm -rf ib_logfile0
rm -rf ib_logfile1
5、修改配置文件中系统表空间中的大小从32M到12M
[mysqld]
innodb_data_file_path =ibdata1:12M
6、启动mysql数据库
systemctl start mysql
7、导入数据
[jianglegege@cstudio mysql]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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 mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed mysql> source /tmp/mysql/engine_cost.sql ;
mysql> source /tmp/mysql/gtid_executed.sql ;
mysql> source /tmp/mysql/help_category.sql ;
mysql> source /tmp/mysql/help_keyword.sql ;
mysql> source /tmp/mysql/help_relation.sql ;
mysql> source /tmp/mysql/help_topic.sql ;
mysql> source /tmp/mysql/innodb_index_stats.sql ;
mysql> source /tmp/mysql/innodb_table_stats.sql ;
mysql> source /tmp/mysql/plugin.sql ;
mysql> source /tmp/mysql/server_cost.sql ;
mysql> source /tmp/mysql/servers.sql ;
mysql> source /tmp/mysql/slave_master_info.sql ;
mysql> source /tmp/mysql/slave_relay_log_info.sql ;
mysql> source /tmp/mysql/slave_worker_info.sql ;
mysql> source /tmp/mysql/time_zone.sql ;
mysql> source /tmp/mysql/time_zone_leap_second.sql ;
mysql> source /tmp/mysql/time_zone_name.sql ;
mysql> source /tmp/mysql/time_zone_transition.sql ;
mysql> source /tmp/mysql/time_zone_transition_type.sql ; mysql> use appdb;
Database changed
mysql> source /tmp/appdb/person.sql ;
8、检查一下是否都正常
a 查看表空间的大小是否收缩了
[jianglegege@cstudio mysql]$ ll -h /database/mysql/data/
................
-rw-r-----. mysql mysql 12M 9月 : ibdata1
................
b 查看业务表是否正常
[jianglegege@cstudio mysql]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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 appdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select * from person;
+----+--------+
| id | name |
+----+--------+
| | Jim |
| | welson |
+----+--------+
rows in set (0.00 sec)
从结果上看收缩表空间是成功了!
三、对收缩表空间的思考:
1、上面的dump过程是有问题的,因为上面只dump出了表结构和数据,对于routine,event,procedure,function,view 都数据库对象
并没有导出来。
2、官方给的收缩方案太长了,我觉得不好的地方再于要对系统库进行处理,我个人比较推荐做法是初始化一个新的实例,并把业务
数据数据都导到新的实例上去,再为它建上业务账号。
----
mysql-5.7 收缩系统表空间详解的更多相关文章
- mysql-5.7 扩展innodb系统表空间详解
一.innodb系统表空间的简介: innodb 系统表空间是由若干个文件组成的,表空间的大小就是对应文件的大小,表空间文件是由innodb_data_file_path 这人参数来定义的.下面我们来 ...
- 15.3、mysql之InnoDB和MyISAM表空间详解
15.3.1.InnoDB引擎表空间: 1.表空间分类: 共享表空间: 某一个数据库的所有的表数据,索引文件全部放在一个文件中,默认这个共享表空间的文件路径在 data目录下. 默认的文件名为:ibd ...
- Oracle表空间详解
Oracle表空间详解 1.表空间的分类 Oracle数据库把表空间分为两类:系统表空间和非系统表空间. 1.1系统表空间指的是数据库系统创建时需要的表空间,这些表空间在数据库创建时自动创建,是每个数 ...
- ORACLE结构体系篇之表空间详解.md
表空间详解一.系统表空间SYSTEM 表空间是Oracle 数据库最重要的一个表空间,存放了一些DDL 语言产生的信息以及PL/SQL 包.视图.函数.过程等,称之为数据字典,因此该表空间也具有其特殊 ...
- Oracle 表空间详解
目录 目录 表空间概述 表空间的分类 默认表空间 查看默认的永久表空间 查看默认的TEMP表空间 查看默认的表空间类型 逻辑结构到物理结构的映射 对表空间的操作 查看表空间使用情况 查看数据库拥有的表 ...
- MySQL数据库系列(四)- InnoDB下的共享表空间和独立表空间详解
一.概念 共享表空间: Innodb的所有数据保存在一个单独的表空间里面,而这个表空间可以由很多个文件组成,一个表可以跨多个文件存在,所以其大小限制不再是文件大小的限制,而是其自身的限制.从Innod ...
- 【Oracle XE系列之四】创建OracleXE表空间详解
创建OracleXE表空间示例 sqlplus /nolog connect sys as sysdba SQL> create tablespace OPFOCN datafile 'C:\ ...
- innodb系统表空间维护
环境说明: 有一个在运行中的mysql环境,由于之前的配置文件设置的过于简单(没有配置innodb_data_file_path变更):造成现在系统表空间已经满了 如果innodb_data_file ...
- MySQL 5.7新特性之在线收缩undo表空间
1. MySQL 5.5时代的undo log 在MySQL5.5以及之前,大家会发现随着数据库上线时间越来越长,ibdata1文件(即InnoDB的共享表空间,或者系统表空间)会越来越大,这会造成2 ...
随机推荐
- linux用户管理中两个重要的“父子”配置文件
在Linux中主要通过用户配置文件来查看和修改用户信息,因此下面我们将介绍两个重要的用户配置文件,让你能够更好的hold住你的用户. 一:父文件/etc/passwd 1.查看配置文件/etc/pas ...
- Kettle资源库采用SQLserver数据库需要注意的点
Kettle开源ETL工具有着自己的元数据存储方式,可以分为两种 1:File 2:DB 文件存储我这里就不多说了,下面说一下在用SQLserver2008 R2作为资源库在创建的过程中遇到的问题 K ...
- 移动立方体算法(Marching cubes algorithm)
百度百科: 医学图像三维重建的方法主要有两大类:一类是三维面绘制,另一类是三维体绘制.体绘制能够更真实地反映物体结构,但由于其运算量大,即使利用高性能的计算机也无法满足实际应用中交互操作的需要.因此, ...
- Cocos2d-x 3.0多线程异步资源载入代码
// AppDelegate.cpp bool AppDelegate::applicationDidFinishLaunching() { - - FlashScene* scene = Flash ...
- 成员函数的const究竟修饰的是谁
demo <pre name="code" class="cpp">class Test { public: const void OpVar(in ...
- 读取Mat文件中的汉字代码
拿到一种元数据,格式为*.mat,但Access打不开,百度也没看到比较好的解决方案. 但是用文本文档可以打开,那估计它和txt类似了,于是想自己写代码来处理了,立马写了读取的丑陋工具.读取是没什么问 ...
- wepy - Cannot read property 'Promise' of undefined
正当我们准备试探示例时,突然.... 造成这个错误有两个原因 (wepy以前的版本默认启动了Promise,自1.4.x以后需要手动开启) 1.未下载Promise 详情见启用文档:Promise ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- 如何分析Java虚拟机死锁
Thread Dump and Concurrency Locks Thread dumps are very useful for diagnosing synchronization relate ...
- spring aop的两种写法aspect和advisor
本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...