1、修改默认的命令分隔符

我们可以通过 DELIMITER 来修改我们的默认命令分隔符:

1
2
3
4
5
6
7
8
9
10
11
12
sing> DELIMITER ,
sing> SELECT NOW();
-> ;
-> ,
+---------------------+
| NOW() |
+---------------------+
| 2017-08-15 22:19:27 |
+---------------------+
1 row in set (0.00 sec)

sing>

可以看出我们执行完 DELIMITER , 之后再执行命令以“;”结尾时已经不能结束命令了,而必须使用我们设置的逗号“,”,当然也可以在登录的时候修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Sing-2:~ Sing$ mysql -uroot -p --delimiter=,
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT NOW();
-> ,
+---------------------+
| NOW() |
+---------------------+
| 2017-08-15 22:31:49 |
+---------------------+
1 row in set (0.00 sec)

mysql>

从第一行我们可以看出已经将分隔符改为了“,”,然后我们看到命令也无法用分号“;”结束。

2、开启语句导出到文件

我们可以用户 \T 将所有输如的命令导出到指定的文件:

1
2
3
mysql> \T /Users/Sing/Documents/Workspace/mysql/log.txt
Logging to file '/Users/Sing/Documents/Workspace/mysql/log.txt'
mysql>

这样就开启了输出,我们每次输入的语句都将记录到该文件里面,如果我们不想记录的时候,可以用 \t 来结束:

1
2
3
mysql> \t
Outfile disabled.
mysql>

执行完之后将不再记录我们输入的命令。