怎样用命令查看Mysql数据库大小?


怎样用命令查看Mysql数据库大小?

mysql> use information_schema;

mysql> select TABLE_SCHEMA,TABLE_NAME,DATA_LENGTH from tables;  #通过在information_schema数据库的tables表中的数据,可以查看每个数据库的各个表所占空间大小DATA_LENGTH是以Byte为单位

1、使用 schema 数据库(存放了其他的数据库的信息)

use information_schema;

2、查询所有数据的大小

select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES;

3、查看指定数据库的大小

比如说数据库test

select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='test';

4、查看指定数据库的表的大小

比如说数据库test中的test_user表

select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='test' and table_name='test_user';

==

MySQL数据库中的information_schema库的tables表的结构:
mysql> desc tables;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| TABLE_TYPE | varchar(64) | NO | | | |
| ENGINE | varchar(64) | YES | | NULL | |
| VERSION | bigint(21) unsigned | YES | | NULL | |
| ROW_FORMAT | varchar(10) | YES | | NULL | |
| TABLE_ROWS | bigint(21) unsigned | YES | | NULL | |
| AVG_ROW_LENGTH | bigint(21) unsigned | YES | | NULL | |
| DATA_LENGTH | bigint(21) unsigned | YES | | NULL | |
| MAX_DATA_LENGTH | bigint(21) unsigned | YES | | NULL | |
| INDEX_LENGTH | bigint(21) unsigned | YES | | NULL | |
| DATA_FREE | bigint(21) unsigned | YES | | NULL | |
| AUTO_INCREMENT | bigint(21) unsigned | YES | | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| CHECK_TIME | datetime | YES | | NULL | |
| TABLE_COLLATION | varchar(32) | YES | | NULL | |
| CHECKSUM | bigint(21) unsigned | YES | | NULL | |
| CREATE_OPTIONS | varchar(255) | YES | | NULL | |
| TABLE_COMMENT | varchar(2048) | NO | | | |
+-----------------+---------------------+------+-----+---------+-------+
21 rows in set (0.00 sec)

其中:
TABLE_SCHEMA 是数据库名
TABLE_NAME 是数据库中的表名
DATA_LENGTH 表的大小
INDEX_LENGTH 索引大小

==

参考网址:

http://www.apoyl.com/?p=458

更多MySQL命令小结:
use information_schema;
select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB') as data from TABLES;

select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB') as data from TABLES where table_schema= 'apoyl' ;

select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB') as data from TABLES where table_schema= 'apoyl' and table_name= 'apoyl_test' ;

show databases;
show tables;

show processlist;

show variables like "plugin%";

select @@basedir;
/*
system_user()   系统用户名
user()          用户名
current_user    当前用户名
session_user()  连接数据库的用户名
database()      数据库名
version()       MYSQL数据库版本
load_file()     MYSQL读取本地文件的函数
@@datadir       读取数据库路径
@@basedir       MYSQL 安装路径
@@version_compile_os    操作系统
*/
select Host,User,Password from mysql.user;
select * from mysql.func;

show variables like '%skip_networking%';
show variables like 'collation_%';
show variables like 'char_%';

use db_name;
status;         #查看某个数据库的编码

show create database db_name;   #查看某个数据库的编码
show [global] status;
show variables;
show engines;
show grants;    #查看权限分配情况
show columns from db_name.tb_name;  <=> desc db_name.tb_name
show create table db_name.tb_name;

mysql> show tables from information_schema;
mysql> show index from information_schema.USER_PRIVILEGES;
mysql> select * from information_schema.user_privileges;
,

《 “怎样用命令查看Mysql数据库大小?” 》 有 4 条评论

  1. 【MySQL】查询所有数据库占用磁盘空间大小和单个库中所有表的大小
    http://witmax.cn/mysql-query-db-size.html
    `
    查询所有数据库占用磁盘空间大小的SQL语句:
    select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),’ MB’) as data_size,
    concat(truncate(sum(index_length)/1024/1024,2),’MB’) as index_size
    from information_schema.tables
    group by TABLE_SCHEMA
    order by data_length desc;

    查询单个库中所有表磁盘占用大小的SQL语句:
    select TABLE_NAME, concat(truncate(data_length/1024/1024,2),’ MB’) as data_size,
    concat(truncate(index_length/1024/1024,2),’ MB’) as index_size
    from information_schema.tables where TABLE_SCHEMA = ‘TestDB’
    group by TABLE_NAME
    order by data_length desc;

    以上语句测试有效,注意替换以上的TestDB为数据库名
    `

  2. Difference between a user and a schema in Oracle?
    https://stackoverflow.com/questions/880230/difference-between-a-user-and-a-schema-in-oracle
    `
    You should consider a schema to be the user account and collection of all objects therein as a schema for all intents and purposes.

    SCOTT is a schema that includes the EMP, DEPT and BONUS tables with various grants, and other stuff.

    SYS is a schema that includes tons of tables, views, grants, etc etc etc.

    SYSTEM is a schema…..

    Technically — A schema is the set of metadata (data dictionary) used by the database, typically generated using DDL. A schema defines attributes of the database, such as tables, columns, and properties. A database schema is a description of the data in a database.

    ==
    可以简单认为Oracle中的 schema 基本上等同于一个 user account 的概念。
    Oracle’s schema (as explained in Nebakanezer’s answer): basically the set of all tables and other objects owned by a user account, so roughly equivalent to a user account.
    `

  3. How to see list of databases in Oracle?
    https://dba.stackexchange.com/questions/27725/how-to-see-list-of-databases-in-oracle
    `
    Question: Is there an equivalent to MySQL SHOW DATABASES statement?

    Answer: There is no such thing. You can query listeners on a machine (lsnrctl status) to see what services are registered there, but that doesn’t map one-to-one to database (and there could be multiple listeners on the same machine). Otherwise, the tools commonly used connect to one database instance, and an instance belongs to a single database.

    Oracle中没有mysql中的database的概念。

    Oracle has no databases but schemas, you can list them with
    SQL> SELECT USERNAME FROM ALL_USERS ORDER BY USERNAME;
    `

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注