使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小、字符集、用户等等。下面总结了一些查看数据库相关信息的命令

1:查看显示所有数据库

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | INVOICE |
  7. | mysql |
  8. | performance_schema |
  9. | test |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12.  
  13. mysql>

2:查看当前使用的数据库

  1. mysql> select database();
  2. +------------+
  3. | database() |
  4. +------------+
  5. | INVOICE |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>

3:查看数据库使用端口

  1. mysql> show variables like 'port';
  2. +---------------+-------+
  3. | Variable_name | Value |
  4. +---------------+-------+
  5. | port | 3306 |
  6. +---------------+-------+
  7. 1 row in set (0.00 sec)

4:查看当前数据库大小

例如,我要查看INVOICE数据库的大小,那么可以通过下面SQL查看

  1. mysql> use information_schema
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4.  
  5. Database changed
  6. mysql> select concat(round(sum(data_length)/(1024*1024),2) + round(sum(index_length)/(1024*1024),2),'MB') as 'DB Size'
  7. -> from tables
  8. -> where table_schema='INVOICE';
  9. +-----------+
  10. | DB Size |
  11. +-----------+
  12. | 7929.58MB |
  13. +-----------+
  14. 1 row in set, 1 warning (0.00 sec)

查看数据所占的空间大小

  1. mysql> use information_schema;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4.  
  5. Database changed
  6. mysql> select concat(round(sum(data_length)/(1024*1024),2),'MB') as 'DB Size'
  7. -> from tables
  8. -> where table_schema='INVOICE';
  9. +-----------+
  10. | DB Size |
  11. +-----------+
  12. | 6430.26MB |
  13. +-----------+
  14. 1 row in set, 1 warning (0.00 sec)
  15.  
  16. mysql>

查看索引所占的空间大小

  1. mysql> select concat(round(sum(index_length)/(1024*1024),2),'MB') as 'DB Size'
  2. -> from tables
  3. -> where table_schema='INVOICE';
  4. +-----------+
  5. | DB Size |
  6. +-----------+
  7. | 1499.32MB |
  8. +-----------+
  9. 1 row in set, 1 warning (0.13 sec)
  10.  
  11. mysql>

5:查看数据库编码

  1. mysql> show variables like 'character%';
  2. +--------------------------+----------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+----------------------------+
  5. | character_set_client | utf8 |
  6. | character_set_connection | utf8 |
  7. | character_set_database | utf8 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | utf8 |
  10. | character_set_server | latin1 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /usr/share/mysql/charsets/ |
  13. +--------------------------+----------------------------+
  14. 8 rows in set (0.00 sec)

character_set_client      为客户端编码方式;

character_set_connection  为建立连接使用的编码;

character_set_database    为数据库的编码;

character_set_results     为结果集的编码;

character_set_server      为数据库服务器的编码;

只要保证以上采用的编码方式一样,就不会出现乱码问题。

  1. mysql> show variables like 'collation%';
  2. +----------------------+-------------------+
  3. | Variable_name | Value |
  4. +----------------------+-------------------+
  5. | collation_connection | utf8_general_ci |
  6. | collation_database | utf8_general_ci |
  7. | collation_server | latin1_swedish_ci |
  8. +----------------------+-------------------+
  9. 3 rows in set (0.00 sec)

status也可以查看数据库的编码

  1. mysql> status;
  2. --------------
  3. mysql Ver 14.14 Distrib 5.6.20, for Linux (x86_64) using EditLine wrapper
  4.  
  5. Connection id: 1
  6. Current database: INVOICE
  7. Current user: root@localhost
  8. SSL: Not in use
  9. Current pager: stdout
  10. Using outfile: ''
  11. Using delimiter: ;
  12. Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)
  13. Protocol version: 10
  14. Connection: Localhost via UNIX socket
  15. Server characterset: latin1
  16. Db characterset: latin1
  17. Client characterset: utf8
  18. Conn. characterset: utf8
  19. UNIX socket: /var/lib/mysql/mysql.sock
  20. Uptime: 5 hours 18 min 51 sec
  21.  
  22. Threads: 1 Questions: 10884 Slow queries: 0 Opens: 650 Flush tables: 1 Open tables: 268 Queries per second avg: 0.568
  23. --------------
  24. mysql>

6:查看数据库的表信息

  1. mysql> show tables;
  2. +---------------------------------------+
  3. | Tables_in_information_schema |
  4. +---------------------------------------+
  5. | CHARACTER_SETS |
  6. | COLLATIONS |
  7. | COLLATION_CHARACTER_SET_APPLICABILITY |
  8. | COLUMNS |
  9. | COLUMN_PRIVILEGES |
  10. | ENGINES |
  11. | EVENTS |
  12. | FILES |
  13. | GLOBAL_STATUS |
  14. | GLOBAL_VARIABLES |
  15. | KEY_COLUMN_USAGE |
  16. | OPTIMIZER_TRACE |
  17. | PARAMETERS |
  18. | PARTITIONS |
  19. | PLUGINS |
  20. | PROCESSLIST |
  21. | PROFILING |
  22. | REFERENTIAL_CONSTRAINTS |
  23. | ROUTINES |
  24. | SCHEMATA |
  25. | SCHEMA_PRIVILEGES |
  26. | SESSION_STATUS |
  27. | SESSION_VARIABLES |
  28. | STATISTICS |
  29. | TABLES |
  30. | TABLESPACES |
  31. | TABLE_CONSTRAINTS |
  32. | TABLE_PRIVILEGES |
  33. | TRIGGERS |
  34. | USER_PRIVILEGES |
  35. | VIEWS |
  36. | INNODB_LOCKS |
  37. | INNODB_TRX |
  38. | INNODB_SYS_DATAFILES |
  39. | INNODB_LOCK_WAITS |
  40. | INNODB_SYS_TABLESTATS |
  41. | INNODB_CMP |
  42. | INNODB_METRICS |
  43. | INNODB_CMP_RESET |
  44. | INNODB_CMP_PER_INDEX |
  45. | INNODB_CMPMEM_RESET |
  46. | INNODB_FT_DELETED |
  47. | INNODB_BUFFER_PAGE_LRU |
  48. | INNODB_SYS_FOREIGN |
  49. | INNODB_SYS_COLUMNS |
  50. | INNODB_SYS_INDEXES |
  51. | INNODB_FT_DEFAULT_STOPWORD |
  52. | INNODB_SYS_FIELDS |
  53. | INNODB_CMP_PER_INDEX_RESET |
  54. | INNODB_BUFFER_PAGE |
  55. | INNODB_CMPMEM |
  56. | INNODB_FT_INDEX_TABLE |
  57. | INNODB_FT_BEING_DELETED |
  58. | INNODB_SYS_TABLESPACES |
  59. | INNODB_FT_INDEX_CACHE |
  60. | INNODB_SYS_FOREIGN_COLS |
  61. | INNODB_SYS_TABLES |
  62. | INNODB_BUFFER_POOL_STATS |
  63. | INNODB_FT_CONFIG |
  64. +---------------------------------------+
  65. 59 rows in set (0.00 sec)

或者使用下面SQL语句查看某个数据库的表信息。

select * from information_schema.tables where table_schema='databasename';

查看某种具体表的信息

select * from information_schema.tables where table_name ='table_name'

7:查看数据库的所有用户信息

  1. mysql> select distinct concat('user: ''',user,'''@''',host,''';') as query from mysql.user;
  2. +-------------------------------------+
  3. | query |
  4. +-------------------------------------+
  5. | user: 'root'@'127.0.0.1'; |
  6. | user: 'root'@'::1'; |
  7. | user: 'root'@'gettesx20.test.com'; |
  8. | user: 'root'@'localhost'; |
  9. +-------------------------------------+
  10. 4 rows in set (0.00 sec)
  11.  
  12. mysql>

8: 查看某个具体用户的权限

  1. mysql> show grants for 'root'@'localhost';
  2. +---------------------------------------------------------------------------------------------------------------------------------+
  3. | Grants for root@localhost |
  4. +---------------------------------------------------------------------------------------------------------------------------------+
  5. | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*C7B1594FD74578DA3A92A61720AC67C6DBE6FC23' WITH GRANT OPTION |
  6. | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
  7. +---------------------------------------------------------------------------------------------------------------------------------+
  8. 2 rows in set (0.00 sec)

9: 查看数据库的最大连接数

  1. mysql> show variables like '%max_connections%';
  2. +-----------------+-------+
  3. | Variable_name | Value |
  4. +-----------------+-------+
  5. | max_connections | 151 |
  6. +-----------------+-------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>

10:查看数据库当前连接数,并发数。

  1. mysql> show status like 'Threads%';
  2. +-------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------+-------+
  5. | Threads_cached | 0 |
  6. | Threads_connected | 1 |
  7. | Threads_created | 1 |
  8. | Threads_running | 1 |
  9. +-------------------+-------+
  10. 4 rows in set (0.00 sec)

Threads_cached : 代表当前此时此刻线程缓存中有多少空闲线程。

Threads_connected :代表当前已建立连接的数量,因为一个连接就需要一个线程,所以也可以看成当前被使用的线程数。

Threads_created :代表从最近一次服务启动,已创建线程的数量。

Threads_running :代表当前激活的(非睡眠状态)线程数。并不是代表正在使用的线程数,有时候连接已建立,但是连接处于sleep状态,这里相对应的线程也是sleep状态。

11:查看数据文件存放路径

  1. mysql> show variables like '%datadir%';
  2. +---------------+-------------------+
  3. | Variable_name | Value |
  4. +---------------+-------------------+
  5. | datadir | /mysqldata/mysql/ |
  6. +---------------+-------------------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>
原创文章请随便转载。愿和大家分享,并且一起进步

Mysql查询数据库状态及信息的更多相关文章

  1. mysql查询数据库大小和表

    每个mysql都有一个库information_schema,里面有一张表TABLES存储了所有数据库表的信息,因此,可以从这张表中查看数据库大小和表大小 查询数据库大小 ,),'GB') as da ...

  2. mysql 查询数据库或某张表有多大(字节)

    转载:https://www.cnblogs.com/diandiandidi/p/5582309.html 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了 select sum(DATA ...

  3. mysql查询数据库中包含某字段(列名)的所有表

    SELECT TABLE_NAME '表名',TABLE_SCHEMA '数据库名',ORDINAL_POSITION '顺序',COLUMN_NAME '字段',DATA_TYPE '类型' ,CH ...

  4. MySql 查询数据库中所有表名

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...

  5. MYSQL查询数据库表索引的硬盘空间占用

    查询数据库的占用 SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB') AS 'Total Index Size' , CONCA ...

  6. 【MySQL】MySQL查询数据库各表的行数

    #倒序查询数据库[各表记录数] use information_schema; select table_name,table_rows from tables where TABLE_SCHEMA ...

  7. MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...

  8. MySQL查询数据库中表名或字段名

    查询数据库中所有表名 select table_name from information_schema.tables where table_schema='csdb' and table_type ...

  9. mysql 查询数据库参数命令

    1.select @@tx_isolation;    查询数据库设置的事务隔离级别 2.desc table_name;  显示表设计 3.show create table table_name; ...

随机推荐

  1. 在Ubuntu18.04.2LTS上使用wine安装qq,微信,迅雷,百度网盘,网易云音乐等软件

    在Ubuntu18.04.2LTS上使用wine安装qq,微信,迅雷,百度网盘,网易云音乐等软件 一.前言 在Linux上办公有一点一直是大家的痛,那就是这些系统上没有我们常用的一些软件,比如QQ,微 ...

  2. webpack-dev-server 不是内部或外部命令,也不是可运行的程序 解决方案

    我看了网上的 一些解决方案,说是webpack版本不对,但我按照提示操作后依然不行: 要先确认是否安装了webpack-dev-server,如果没有安装,安装便可以解决: 粗暴的解决方案是删除nod ...

  3. 关于 Paket

    参考地址:https://fsprojects.github.io/Paket/editor-support.html 1. 安装 Paket for Visual Studio,一个类似于 Nuge ...

  4. Mybatis中的Mapper.xml映射文件sql查询接收多个参数

    ​ 我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQL,在dao层定义的接口中定义的参数传到xml文件中之后,在查询之前mybatis会对其进行动态解析,通常使用#{}接收 ...

  5. WPF ControlTemplate,DataTemplate

    The Control Template defines the visual appearance of a control. All of the UI elements have some ki ...

  6. 深入浅出《设计模式》之外观模式(C++)

    前言 模式介绍 外观模式相比较之下比较简单,模式设计中定义是为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口是的这一子系统更加容易使用. 如果不理解呢,简单些说就是外观模式提 ...

  7. vue如何导入外部js文件(es6)

    也许大家都是使用习惯了es5的写法喜欢直接用<Script>标签倒入js文件,但是很是可惜,这写法.在es6,或则说vue环境下不支持 真的写法是怎样? 首先.我们要改造我们要映入的外部j ...

  8. Html头部meta标签

      meta元素有4个属性:name.http-equiv.content.charset.meta标签通过name属性来表述页面文档的元信息,通过http-equiv属性设置http请求指令,通过c ...

  9. AutoLayout的使用

    虽然苹果提供了AutoresizingMask的布局方式,这个方式局限性太大:只能解决父控件和子控件间的相对关系: 因此,推出了AutoLayout:苹果官方也是推荐开发者尽量使用autolayout ...

  10. Python odoo中嵌入html简单的分页功能

    在odoo中,通过iframe嵌入 html,页面数据则通过controllers获取,使用jinja2模板传值渲染 html页面分页内容,这里写了判断逻辑 <!-- 分页 -->< ...