mysql命令行的一些小技巧【实用:多屏显示,格式化输出等】

1.以html格式输出结果
使用mysql客户端的参数–html或者-T,则所有SQL的查询结果会自动生成为html的table代码
$ mysql -u root --html
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3286
Server version: 5.1.24-rc-log MySQL Community Server (GPL)
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> select * from test.test;
<TABLE BORDER=1><TR><TH>i</TH></TR><TR><TD>1</TD></TR><TR><TD>2</TD></TR></TABLE>
2 rows in set (0.00 sec)
2.以xml格式输出结果
跟上面差不多,使用–xml或者-X选项,可以将结果输出为xml格式
$ mysql -u root --xml
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3287
Server version: 5.1.24-rc-log MySQL Community Server (GPL)
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> select * from test.test;
<?xml version="1.0"?>
<resultset statement="select * from test.test;"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="i">1</field>
</row>
<row>
<field name="i">2</field>
</row>
</resultset>
2 rows in set (0.00 sec)
3.修改命令提示符
使用mysql的–prompt=选项,或者进入mysql命令行环境后使用prompt命令,都可以修改提示符
mysql> prompt u@d>
PROMPT set to 'u@d>'
root@(none)>use mysql
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
root@mysql>
其中u表示当前连接的用户,d表示当前连接的数据库,其他更多的可选项可以参考man mysql
4.使用G按行垂直显示结果
如果一行很长,需要这行显示的话,看起结果来就非常的难受。在SQL语句或者命令后使用G而不是分号结尾,可以将每一行的值垂直输出。这个可能也是大家对于MySQL最熟悉的区别于其他数据库工具的一个特性了。
mysql> select * from db_archivelogG
*************************** 1. row ***************************
id: 1
check_day: 2008-06-26
db_name: TBDB1
arc_size: 137
arc_num: 166
per_second: 1.6
avg_time: 8.7

5.使用pager设置显示方式
如果select出来的结果集超过几个屏幕,那么前面的结果一晃而过无法看到。使用pager可以设置调用os的more或者less等显示查询结果,和在os中使用more或者less查看大文件的效果一样。
使用more
mysql> pager more
PAGER set to 'more'
mysql> P more
PAGER set to 'more'
使用less
mysql> pager less
PAGER set to 'less'
mysql> P less
PAGER set to 'less'
还原成stdout
mysql> nopager
PAGER set to stdout
6.使用tee保存运行结果到文件
这个类似于sqlplus的spool功能,可以将命令行中的结果保存到外部文件中。如果指定已经存在的文件,则结果会附加到文件中。
mysql> tee output.txt
Logging to file 'output.txt'
或者
mysql> T output.txt
Logging to file 'output.txt'
mysql> notee
Outfile disabled.
或者
mysql>
Outfile disabled.
7.执行OS命令
mysql> system uname
Linux
mysql> ! uname
Linux
8.执行SQL文件
mysql> source test.sql
+----------------+
| current_date() |
+----------------+
| 2008-06-28 |
+----------------+
1 row in set (0.00 sec)
或者
mysql> . test.sql
+----------------+
| current_date() |
+----------------+
| 2008-06-28 |
+----------------+
1 row in set (0.00 sec)
其他还有一些功能,可以通过help或者?获得MySQL命令行支持的一些命令。
转自:http://hi.baidu.com/lushaojin/blog/item/acacc8fc6de6d2fafc037f89.html

原文地址:https://www.cnblogs.com/W-Kr/p/5504383.html