1、如何查看MySQL自带的官方文档
2、掌握MySQL服务器提供的常用命令(mysql
、mysqladmin
、mysqldump
、mysqlimport
、mysqlshow
等命令)
3、掌握如何导入导出海量数据
提前准备一张表,和一个存储过程,方便后面插入测试数据
CREATE TABLE `userinfo` (
`id` int(10) NOT NULL COMMENT '用户id',
`username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
`age` int(3) NULL DEFAULT NULL COMMENT '年龄',
`phone` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',
`gender` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别: ‘0’-男 ‘1’-女',
`desc` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '自我介绍',
`register_time` datetime(0) NULL DEFAULT NULL COMMENT '注册时间',
`login_time` datetime(0) NULL DEFAULT NULL COMMENT '上一次登录时间',
`pic` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '头像地址',
`look` int(10) NULL DEFAULT NULL COMMENT '查看数',
PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
create procedure test_insert(count int)
begin
declare i int default 1;
while i<=count do
INSERT INTO userinfo values(
i, -- id
uuid(), -- username
CEILING(RAND()*90+10), -- age
FLOOR(RAND()*100000000000), -- phone
round(FORMAT(rand(),1)), -- gender
uuid(), -- desc
now(), -- register_time
now(), -- login_time
uuid(), -- pic
CEILING(RAND()*90+10) -- look
);
set i=i+1;
end while;
end;
我们在操作MySQL时,经常会忘记一些语法,比如show
、alter
、grant
等命令,或者想查询一些函数的使用、说明文档等。MySQL提供了一套非常完善的帮助文档工具,方便我们快捷的查询。
如果不知道帮助文档能够提供些什么, 可以用? contents
命令来显示所有可供查询的分类
mysql> ? contents;
You asked for help about help category: "Contents"
For more information, type 'help - '
, where <item> is one of the following
categories:
Account Management
Administration
Compound Statements
Contents
Data Definition
Data Manipulation
Data Types
Functions
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility
mysql>
我们可以在跟目录里面选择某个组进行详细查询
mysql> ? Data Types;
You asked for help about help category: "Data Types"
For more information, type 'help - '
, where <item> is one of the following
topics:
AUTO_INCREMENT
BIGINT
BINARY
BIT
BLOB
BLOB DATA TYPE
BOOLEAN
CHAR
CHAR BYTE
DATE
DATETIME
DEC
DECIMAL
DOUBLE
DOUBLE PRECISION
ENUM
FLOAT
INT
INTEGER
LONGBLOB
LONGTEXT
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
SET DATA TYPE
SMALLINT
TEXT
TIME
TIMESTAMP
TINYBLOB
TINYINT
TINYTEXT
VARBINARY
VARCHAR
YEAR DATA TYPE
mysql>
接着还可以查询具体的某个类型的详细介绍:
mysql> ? INT
Name: 'INT'
Description:
INT[(M)] [UNSIGNED] [ZEROFILL]
A normal-size integer. The signed range is -2147483648 to 2147483647.
The unsigned range is 0 to 4294967295.
URL: https://dev.mysql.com/doc/refman/5.7/en/numeric-type-syntax.html
mysql>
mysql> ? Account Management;
You asked for help about help category: "Account Management"
For more information, type 'help - '
, where <item> is one of the following
topics:
ALTER USER
CREATE USER
DROP USER
GRANT
RENAME USER
REVOKE
SET PASSWORD
mysql>
还可以查询更详细的用法:
mysql> ? ALTER USER;
Name: 'ALTER USER'
Description:
Syntax:
ALTER USER [IF EXISTS]
user [auth_option] [, user [auth_option]] ...
[REQUIRE {NONE | tls_option [[AND] tls_option] ...}]
[WITH resource_option [resource_option] ...]
[password_option | lock_option] ...
ALTER USER [IF EXISTS]
USER() IDENTIFIED BY 'auth_string'
........
比如我们选择几个常用而且容易忘记命令查询:
mysql> ? alter
Many help items for your request exist.
To make a more specific request, please type 'help - '
,
where <item> is one of the following
topics:
ALTER DATABASE
ALTER EVENT
ALTER FUNCTION
ALTER INSTANCE
ALTER LOGFILE GROUP
ALTER PROCEDURE
ALTER SCHEMA
ALTER SERVER
ALTER TABLE
ALTER TABLESPACE
ALTER USER
ALTER VIEW
GRANT
SPATIAL INDEXES
mysql>
查询更详细的说明:
mysql> ? ALTER DATABASE;
Name: 'ALTER DATABASE'
Description:
Syntax:
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
......
mysql> ? show
Name: 'SHOW'
Description:
SHOW has many forms that provide information about databases, tables,
columns, or status information about the server. This section describes
those following:
SHOW {BINARY | MASTER} LOGS
SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
SHOW CHARACTER SET [like_or_where]
SHOW COLLATION [like_or_where]
SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [like_or_where]
SHOW CREATE DATABASE db_name
SHOW CREATE EVENT event_name
SHOW CREATE FUNCTION func_name
SHOW CREATE PROCEDURE proc_name
SHOW CREATE TABLE tbl_name
SHOW CREATE TRIGGER trigger_name
SHOW CREATE VIEW view_name
SHOW DATABASES [like_or_where]
SHOW ENGINE engine_name {STATUS | MUTEX}
SHOW [STORAGE] ENGINES
SHOW ERRORS [LIMIT [offset,] row_count]
SHOW EVENTS
SHOW FUNCTION CODE func_name
SHOW FUNCTION STATUS [like_or_where]
SHOW GRANTS FOR user
SHOW INDEX FROM tbl_name [FROM db_name]
SHOW MASTER STATUS
SHOW OPEN TABLES [FROM db_name] [like_or_where]
SHOW PLUGINS
SHOW PROCEDURE CODE proc_name
SHOW PROCEDURE STATUS [like_or_where]
SHOW PRIVILEGES
SHOW [FULL] PROCESSLIST
SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n]
SHOW PROFILES
SHOW RELAYLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
SHOW SLAVE HOSTS
SHOW SLAVE STATUS [FOR CHANNEL channel]
SHOW [GLOBAL | SESSION] STATUS [like_or_where]
SHOW TABLE STATUS [FROM db_name] [like_or_where]
SHOW [FULL] TABLES [FROM db_name] [like_or_where]
SHOW TRIGGERS [FROM db_name] [like_or_where]
SHOW [GLOBAL | SESSION] VARIABLES [like_or_where]
SHOW WARNINGS [LIMIT [offset,] row_count]
......
查询更详细的用法说明:
mysql> ? SHOW CREATE TABLE
Name: 'SHOW CREATE TABLE'
Description:
Syntax:
SHOW CREATE TABLE tbl_name
........
Examples:
mysql> SHOW CREATE TABLE t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`s` char(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
mysql>
总之,MySQL提供的帮助文档,哪里不会问哪里(?)。
mysql -?
或者
mysql --help
mysql [OPTIONS] [database]
--host=name
):mysql服务器的IP--port
):mysql服务器的端口--user=name
):mysql用户名--password=name
):mysql密码--database
):指定连接的数据库完整语法示例:
简写:
mysql -h127.0.0.1 -P 3306 -uroot -padmin -D test
全写:
mysql --host=127.0.0.1 --port 3306 --user=root --password=admin --database test
mysqladmin命令用于临时执行某些操作,比如创建一个数据库,删除一个数据库,查看数据库状态等。
mysqladmin -?
mysqladmin [OPTIONS] command command....
示例:
[root@localhost ~]# mysqladmin -uroot -padmin create test01;
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysqladmin -uroot -padmin drop test01;
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'test01' database [y/N] y
Database "test01" dropped
[root@localhost ~]#
[root@localhost ~]# mysqladmin -uroot -padmin version;
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin Ver 8.42 Distrib 5.7.30, for Linux on x86_64
Copyright (c) 2000, 2020, 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.
Server version 5.7.30
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 5 hours 59 min 31 sec
Threads: 2 Questions: 734 Slow queries: 0 Opens: 260 Flush tables: 1 Open tables: 234 Queries per second avg: 0.034
[root@localhost ~]#
mysqldump命令主要用于数据库的备份。
mysqldump [OPTIONS] database [tables]
参数:
options:
--no-create-db
):不包含创建数据库语句--no-create-info
):不包含创建表数据--no-data
):不包含数据--database
):指定导出的数据库--all-databases
):导出所有数据库mysqldump -hip地址 -P端口 -u用户名 -p密码 数据库 表1 表2... > 文件路径
mysqldump -h127.0.0.1 -P3306 -uroot -padmin test test2 > /root/test.sql
mysqldump -uroot -padmin test > /root/test.sql
-h 默认值为127.0.0.1 -P 默认值为3306
mysqldump -u用户名 -p密码 --databases 库1 库2... > 文件路径
mysqldump -uroot -padmin --databases test > test.sql
mysqldump -uroot -padmin -B test > test.sql
导出库和导出表的区别在于导出库的sql语句里面加了
create database db_name
语句。
mysqldump -u用户名 -p密码 --all-databases > 文件路径
mysqldump -uroot -padmin --all-databases > /root/test.sql
mysqldump -uroot -padmin -A > /root/test.sql
mysqldump -uroot -padmin -t test userinfo > /root/test.sql
mysqldump -uroot -padmin -t -B test > /root/test.sql
mysqldump -uroot -padmin -d test userinfo > /root/test.sql
mysqldump -uroot -padmin -d -B test> /root/test.sql
mysqldump -uroot -padmin -d -A> /root/test.sql
-T
:导出规则数据--fields-terminated-by
:数据列与列之间的分隔符(默认为一个制表位\t)--fields-enclosed-by
:数据列左右的字符通过-T
参数导出的数据会生成两份文件,一份是.sql
,里面包含了表结构文件,另一份是.txt
文件,里面是通过参数指定后生成的规则数据文件。另外,参数-T
导出的数据目录mysql已经规定好了,通过secure_file_priv
维护着导出的数据文件所在目录。
mysql> select @@secure_file_priv;
+-----------------------+
| @@secure_file_priv |
+-----------------------+
| /var/lib/mysql-files/ |
+-----------------------+
1 row in set (0.01 sec)
mysql>
通过上面的参数我们知道,
-T
参数导出的数据必须在/var/lib/mysql-files/
目录。
mysqldump -uroot -padmin -T /var/lib/mysql-files/ --fields-terminated-by=',' test userinfo
导出数据格式如下:
1,c3e0e008-999e-11ea-943a-000c29a7676a,32,83188418748,0,c3e0e525-999e-11ea-943a-000c29a7676a,2020-05-19 15:02:55,2020-05-19 15:02:55,c3e0e56c-999e-11ea-943a-000c29a7676a,69
示例:
mysqldump -uroot -padmin -T /var/lib/mysql-files/ --fields-terminated-by=',' --fields-enclosed-by='+' test userinfo
导出数据格式如下:
+1+,+c3e0e008-999e-11ea-943a-000c29a7676a+,+32+,+83188418748+,+0+,+c3e0e525-999e-11ea-943a-000c29a7676a+,+2020-05-19 15:02:55+,+2020-05-19 15:02:55+,+c3e0e56c-999e-11ea-943a-000c29a7676a+,+69+
这里说明一下,使用mysqldump导出数据的效率极快,300W的数据不到6s全部导出(每个人测试的值会有偏差)。
准备数据:
use test;
insert into userinfo values(1,'aca850a8-98d7-11ea-90c1-000c29a7676a',69,'68226931307',0,'aca850c7-98d7-11ea-90c1-000c29a7676a','2020-05-18 15:17:47.0','2020-05-18 15:17:47.0','aca850cc-98d7-11ea-90c1-000c29a7676a',32);
insert into userinfo values(2,'aca8532e-98d7-11ea-90c1-000c29a7676a',85,'43250332533',1,'aca8535b-98d7-11ea-90c1-000c29a7676a','2020-05-18 15:17:47.0','2020-05-18 15:17:47.0','aca85362-98d7-11ea-90c1-000c29a7676a',15);
insert into userinfo values(3,'aca8547c-98d7-11ea-90c1-000c29a7676a',33,'8483425256',1,'aca85494-98d7-11ea-90c1-000c29a7676a','2020-05-18 15:17:47.0','2020-05-18 15:17:47.0','aca8549a-98d7-11ea-90c1-000c29a7676a',24);
insert into userinfo values(4,'aca85588-98d7-11ea-90c1-000c29a7676a',74,'6538729841',0,'aca8559e-98d7-11ea-90c1-000c29a7676a','2020-05-18 15:17:47.0','2020-05-18 15:17:47.0','aca855a3-98d7-11ea-90c1-000c29a7676a',92);
insert into userinfo values(5,'aca85990-98d7-11ea-90c1-000c29a7676a',87,'55627691983',0,'aca859bb-98d7-11ea-90c1-000c29a7676a','2020-05-18 15:17:47.0','2020-05-18 15:17:47.0','aca859c5-98d7-11ea-90c1-000c29a7676a',51);
insert into userinfo values(6,'aca85ae8-98d7-11ea-90c1-000c29a7676a',62,'50662035449',1,'aca85b02-98d7-11ea-90c1-000c29a7676a','2020-05-18 15:17:47.0','2020-05-18 15:17:47.0','aca85b07-98d7-11ea-90c1-000c29a7676a',64);
mysql -uroot -padmin < /root/import.sql
source是属于mysql的命令,需要登录进mysql执行(实质也是把里面的sql执行一遍)。
mysql> source /root/import.sql
mysqlimport
命令是用于导入sql语句的一个命令。与前面两个数据导入不同,mysqlimport适用于导入规则数据。
注意的是,mysqlimport导入数据的所在目录也被secure_file_priv
变量维护着,因此需要导入的数据必须在/var/lib/mysql-files/
目录下。
mysqldump -uroot -padmin -T /var/lib/mysql-files/ --fields-terminated-by=',' test userinfo
导出300W数据,花了8秒钟(我手敲命令也会有时间误差)。
-- 清空表
truncate userinfo;
mysqlimport
命令导入300W数据mysqlimport -uroot -padmin /var/lib/mysql-files/ --fields-terminated-by=',' test userinfo
需要注意的是
mysqlimport
命令的导入数据必须和mysqldump
导入出来的数据格式一致(按什么规则导出,就按照什么规则导入)。
mysqlshow
命令用于显示mysql数据库的一些信息,如当前有几个数据库、指定的数据库有什么表等信息。
[root@localhost ~]# mysqlshow -uroot -padmin;
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Databases |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
[root@localhost ~]#
[root@localhost ~]# mysqlshow -uroot -padmin --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
+--------------------+--------+--------------+
| Databases | Tables | Total Rows |
+--------------------+--------+--------------+
| information_schema | 61 | 15181 |
| mysql | 31 | 3067 |
| performance_schema | 87 | 43550 |
| sys | 101 | 5380 |
| test | 8 | 10002 |
+--------------------+--------+--------------+
5 rows in set.
[root@localhost ~]#
[root@localhost ~]# mysqlshow -uroot -padmin test --count;
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: test
+-----------------+
| Tables |
+-----------------+
| class |
| student |
| test |
| test2 |
| userinfo |
| userinfo_1 |
| userinfo_2 |
| userinfo_myisam |
+-----------------+
[root@localhost ~]#
本篇到这里就结束了,在这里提一下,我个人认为:很多软件都带有官方文档,包括redis手册、Java的jdk文档等,手册是给那些已经会了的人看的,他们忘记,看下手册就能回想起完整的语法,不是给那些完全不懂的人看的,我相信没有人是跟着手册从0到1学习的(学霸大佬勿喷)。因此手册只是作为参考作用。