`
kongzimengsheng1
  • 浏览: 66539 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mysql 常用命令

阅读更多
MySQL的客户端命令行工具,有很多方便使用者的特性,某些方面甚至可以说比Oracle的sqlplus更加人性化。当然从整体来说,还是sqlplus更加方便些,这么说或许是我对sqlplus更加熟悉吧。这里记录下MySQL命令行几个比较常用的特性。

1.使用\G按行垂直显示结果
如果一行很长,需要这行显示的话,看起结果来就非常的难受。在SQL语句或者命令后使用\G而不是分号结尾,可以将每一行的值垂直输出。这个可能也是大家对于MySQL最熟悉的区别于其他数据库工具的一个特性了。
mysql> select * from db_archivelog\G
*************************** 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

2.使用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

3.使用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> \t
Outfile disabled.

4.执行OS命令
mysql> system uname
Linux
mysql> \! uname
Linux

5.执行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命令行支持的一些命令



//===============================================================================
MySQL命令行的几个用法(续)
作者:NinGoo | 【转载须以超链接形式标明文章原始出处和作者信息】

继续上一篇的话题,介绍mysql命令行的一些小技巧。

1.以html格式输出结果
使用mysql客户端的参数–html或者-T,则所有SQL的查询结果会自动生成为html的table代码

$ mysql -uroot --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 -uroot --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



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics