Mysql查询数据库状态及信息
使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小、字符集、用户等等。下面总结了一些查看数据库相关信息的命令
1:查看显示所有数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| INVOICE |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec) mysql>
2:查看当前使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| INVOICE |
+------------+
1 row in set (0.00 sec) mysql>
3:查看数据库使用端口
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
4:查看当前数据库大小
例如,我要查看INVOICE数据库的大小,那么可以通过下面SQL查看
mysql> use information_schema
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 concat(round(sum(data_length)/(1024*1024),2) + round(sum(index_length)/(1024*1024),2),'MB') as 'DB Size'
-> from tables
-> where table_schema='INVOICE';
+-----------+
| DB Size |
+-----------+
| 7929.58MB |
+-----------+
1 row in set, 1 warning (0.00 sec)
查看数据所占的空间大小
mysql> use information_schema;
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 concat(round(sum(data_length)/(1024*1024),2),'MB') as 'DB Size'
-> from tables
-> where table_schema='INVOICE';
+-----------+
| DB Size |
+-----------+
| 6430.26MB |
+-----------+
1 row in set, 1 warning (0.00 sec) mysql>
查看索引所占的空间大小
mysql> select concat(round(sum(index_length)/(1024*1024),2),'MB') as 'DB Size'
-> from tables
-> where table_schema='INVOICE';
+-----------+
| DB Size |
+-----------+
| 1499.32MB |
+-----------+
1 row in set, 1 warning (0.13 sec) mysql>
5:查看数据库编码
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
character_set_client 为客户端编码方式;
character_set_connection 为建立连接使用的编码;
character_set_database 为数据库的编码;
character_set_results 为结果集的编码;
character_set_server 为数据库服务器的编码;
只要保证以上采用的编码方式一样,就不会出现乱码问题。
mysql> show variables like 'collation%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
status也可以查看数据库的编码
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.6.20, for Linux (x86_64) using EditLine wrapper Connection id: 1
Current database: INVOICE
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 5 hours 18 min 51 sec Threads: 1 Questions: 10884 Slow queries: 0 Opens: 650 Flush tables: 1 Open tables: 268 Queries per second avg: 0.568
--------------
mysql>
6:查看数据库的表信息
mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema |
+---------------------------------------+
| CHARACTER_SETS |
| COLLATIONS |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS |
| COLUMN_PRIVILEGES |
| ENGINES |
| EVENTS |
| FILES |
| GLOBAL_STATUS |
| GLOBAL_VARIABLES |
| KEY_COLUMN_USAGE |
| OPTIMIZER_TRACE |
| PARAMETERS |
| PARTITIONS |
| PLUGINS |
| PROCESSLIST |
| PROFILING |
| REFERENTIAL_CONSTRAINTS |
| ROUTINES |
| SCHEMATA |
| SCHEMA_PRIVILEGES |
| SESSION_STATUS |
| SESSION_VARIABLES |
| STATISTICS |
| TABLES |
| TABLESPACES |
| TABLE_CONSTRAINTS |
| TABLE_PRIVILEGES |
| TRIGGERS |
| USER_PRIVILEGES |
| VIEWS |
| INNODB_LOCKS |
| INNODB_TRX |
| INNODB_SYS_DATAFILES |
| INNODB_LOCK_WAITS |
| INNODB_SYS_TABLESTATS |
| INNODB_CMP |
| INNODB_METRICS |
| INNODB_CMP_RESET |
| INNODB_CMP_PER_INDEX |
| INNODB_CMPMEM_RESET |
| INNODB_FT_DELETED |
| INNODB_BUFFER_PAGE_LRU |
| INNODB_SYS_FOREIGN |
| INNODB_SYS_COLUMNS |
| INNODB_SYS_INDEXES |
| INNODB_FT_DEFAULT_STOPWORD |
| INNODB_SYS_FIELDS |
| INNODB_CMP_PER_INDEX_RESET |
| INNODB_BUFFER_PAGE |
| INNODB_CMPMEM |
| INNODB_FT_INDEX_TABLE |
| INNODB_FT_BEING_DELETED |
| INNODB_SYS_TABLESPACES |
| INNODB_FT_INDEX_CACHE |
| INNODB_SYS_FOREIGN_COLS |
| INNODB_SYS_TABLES |
| INNODB_BUFFER_POOL_STATS |
| INNODB_FT_CONFIG |
+---------------------------------------+
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:查看数据库的所有用户信息
mysql> select distinct concat('user: ''',user,'''@''',host,''';') as query from mysql.user;
+-------------------------------------+
| query |
+-------------------------------------+
| user: 'root'@'127.0.0.1'; |
| user: 'root'@'::1'; |
| user: 'root'@'gettesx20.test.com'; |
| user: 'root'@'localhost'; |
+-------------------------------------+
4 rows in set (0.00 sec) mysql>
8: 查看某个具体用户的权限
mysql> show grants for 'root'@'localhost';
+---------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*C7B1594FD74578DA3A92A61720AC67C6DBE6FC23' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
9: 查看数据库的最大连接数
mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec) mysql>
10:查看数据库当前连接数,并发数。
mysql> show status like 'Threads%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 0 |
| Threads_connected | 1 |
| Threads_created | 1 |
| Threads_running | 1 |
+-------------------+-------+
4 rows in set (0.00 sec)
Threads_cached : 代表当前此时此刻线程缓存中有多少空闲线程。
Threads_connected :代表当前已建立连接的数量,因为一个连接就需要一个线程,所以也可以看成当前被使用的线程数。
Threads_created :代表从最近一次服务启动,已创建线程的数量。
Threads_running :代表当前激活的(非睡眠状态)线程数。并不是代表正在使用的线程数,有时候连接已建立,但是连接处于sleep状态,这里相对应的线程也是sleep状态。
11:查看数据文件存放路径
mysql> show variables like '%datadir%';
+---------------+-------------------+
| Variable_name | Value |
+---------------+-------------------+
| datadir | /mysqldata/mysql/ |
+---------------+-------------------+
1 row in set (0.00 sec) mysql>
Mysql查询数据库状态及信息的更多相关文章
- mysql查询数据库大小和表
每个mysql都有一个库information_schema,里面有一张表TABLES存储了所有数据库表的信息,因此,可以从这张表中查看数据库大小和表大小 查询数据库大小 ,),'GB') as da ...
- mysql 查询数据库或某张表有多大(字节)
转载:https://www.cnblogs.com/diandiandidi/p/5582309.html 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了 select sum(DATA ...
- mysql查询数据库中包含某字段(列名)的所有表
SELECT TABLE_NAME '表名',TABLE_SCHEMA '数据库名',ORDINAL_POSITION '顺序',COLUMN_NAME '字段',DATA_TYPE '类型' ,CH ...
- MySql 查询数据库中所有表名
查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...
- MYSQL查询数据库表索引的硬盘空间占用
查询数据库的占用 SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB') AS 'Total Index Size' , CONCA ...
- 【MySQL】MySQL查询数据库各表的行数
#倒序查询数据库[各表记录数] use information_schema; select table_name,table_rows from tables where TABLE_SCHEMA ...
- MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同
查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...
- MySQL查询数据库中表名或字段名
查询数据库中所有表名 select table_name from information_schema.tables where table_schema='csdb' and table_type ...
- mysql 查询数据库参数命令
1.select @@tx_isolation; 查询数据库设置的事务隔离级别 2.desc table_name; 显示表设计 3.show create table table_name; ...
随机推荐
- python-7-数据结构与类型转换
前言 python除了前面所说的基础类型,我们这里也需要讲解下数据结构,数据结构里面存放的是基础类型,如数字等同时也可以嵌套. 不可变数据(3 个):Number(数字).String(字符串).Tu ...
- 明解C语言 入门篇 第十三章答案
练习13-1 /* 打开与关闭文件 */ #include <stdio.h> int main(void) { ]; FILE* fp; printf("请输入你要打开的文件& ...
- python asyncio 使用ThreadPoolExecutor和asyncio完成阻塞IO请求
#使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket fro ...
- MySQL性能诊断与调优
LAMP 系统性能调优,第 3 部分: MySQL 服务器调优http://www.ibm.com/developerworks/cn/linux/l-tune-lamp-3.html LoadRun ...
- mysql 各版本对比
具体请看:mysql 各版本差异性对比
- SpringBoot与Swagger整合
1 SpringBoot与Swagger整合https://blog.csdn.net/jamieblue1/article/details/99847744 2 Swagger详解(SpringBo ...
- 2018-8-10-win10-uwp-拖动控件
原文:2018-8-10-win10-uwp-拖动控件 title author date CreateTime categories win10 uwp 拖动控件 lindexi 2018-08-1 ...
- 分享windows 10 下部署 elasticsearch 和 logstash (二)
接上一篇,es部署很简单,很快就弄好了. 但是还是有很多不玩美. 比如说:主机是本地的IP或机器名,端口是固定的9200. 而且是只有一个节点,我要在一台机器上部署多个节点呢. 经过一段时间的摸索,做 ...
- 【设计模式】Singleton
前言 Singleton设计模式,确保全局只存在一个该类的实例.将构造器声明为private,防止调用(虽然还是可以使用反射来调用).声明一个静态的类实例在类中,声明一个公共的获取实例的方法.这篇博文 ...
- 前端vue项目js中怎么保证链式调用后台接口
在一个for循环中对同一接口调用多次,如何保证逐步执行,同步执行. html部分 <DcFileUpload v-for="(item, index) of fileLengthLis ...