Mysql笔记

date: 2019.06.27; modification:2019.07.01

目录:

1 常用命令

本地登录: mysql -u root -p 
远程登录: mysql -h 127.16.1.2 -P 3306 -u root -p
查询: SELECT id,name FROM my_table WHERE id = 123

2 杂项

2.1 查看mysql库大小,表大小,索引大小

通过MySQL的 information_schema 数据库,可查询数据库中每个表占用的空间、表记录的行数;该库中有一个 TABLES 表,这个表主要字段分别是:

其他字段请参考MySQL的手册,查看一个表占用空间的大小,那就相当于是 数据大小 + 索引大小 。

查看所有库的大小:

mysql> use information_schema;
Database changed
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES;
+----------+
| data     |
+----------+
| 104.21MB |
+----------+
1 row in set (0.11 sec)

查看指定库的大小:

mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES where table_schema='test';
+---------+
| data    |
+---------+
| 26.17MB |
+---------+
1 row in set (0.01 sec)

查看指定库的指定表的大小:

mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES where table_schema='test' and table_name='a_ya';
+--------+
| data   |
+--------+
| 0.02MB |
+--------+
1 row in set (0.00 sec)

查看指定库的索引大小:

mysql> SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB') AS 'Total Index Size' FROM TABLES  WHERE table_schema = 'test'; 
+------------------+
| Total Index Size |
+------------------+
| 0.94 MB          |
+------------------+
1 row in set (0.01 sec)

查看一个库中的情况:

mysql>  SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', CONCAT(ROUND(table_rows/1000000,4),'M') AS 'Number of Rows', CONCAT(ROUND(data_length/(1024*1024*1024),4),'G') AS 'Data Size', CONCAT(ROUND(index_length/(1024*1024*1024),4),'G') AS 'Index Size', CONCAT(ROUND((data_length+index_length)/(1024*1024*1024),4),'G') AS'Total'FROM information_schema.TABLES WHERE table_schema LIKE 'test';
+---------------+----------------+-----------+------------+---------+
| Table Name    | Number of Rows | Data Size | Index Size | Total   |
+---------------+----------------+-----------+------------+---------+
| test.a_br     | 0.4625M        | 0.0259G   | 0.0171G    | 0.0431G |
| test.a_skuclr | 0.7099M        | 0.0660G   | 0.0259G    | 0.0919G |
| test.a_yuser  | 1.0736M        | 0.0497G   | 0.0213G    | 0.0710G |
| test.test     | 0.0000M        | 0.0000G   | 0.0000G    | 0.0000G |
+---------------+----------------+-----------+------------+---------+
4 rows in set (0.13 sec)

2.2 存储空间配置

官方文档: The System Tablespace