• 有效的利用MYSQL命令行工具


    原文链接:http://www.mysqlperformanceblog.com/2012/12/21/be-productive-with-the-mysql-command-line/

    Even if you are using a GUI tool to connect to your MySQL servers, one day or another, you will have to deal with the command line. So it is nice to know a few tips that can really make your work easier.

    即使你是使用GUI工具去连接MYSQL服务器做操作,但是时不时的你还是需要用命令行工具做一些操作。所以了解一些小技巧能够让你的工作变得容易起来

    Note: The commands below are only available for Unix/Linux.  注意:下面的命令只在Unix/Linux下有效

    Using pager

    Most of the graphical tools paginate results, which is very handy. But this is not the way the command line client works: it just outputs all results. It can be annoying but it is easily solved by using the pager command:

    大部分的图形化工具都可以对结果集分页显示,这很有用。但是MYSQL命令行工具不是这么做的:它输出所有的结果行。这个特性很讨厌,使用pager命令可以很简单的解决这个问题。

    mysql> pager more
    PAGER set to 'more'
    mysql> select title from sakila.film;
    +-----------------------------+
    | title                       |
    +-----------------------------+
    | ACADEMY DINOSAUR            |
    | ACE GOLDFINGER              |
    | ADAPTATION HOLES            |
    | AFFAIR PREJUDICE            |
    | AFRICAN EGG                 |
    | AGENT TRUMAN                |
    | AIRPLANE SIERRA             |
    | AIRPORT POLLOCK             |
    | ALABAMA DEVIL               |
    | ALADDIN CALENDAR            |
    | ALAMO VIDEOTAPE             |
    | ALASKA PHANTOM              |
    | ALI FOREVER                 |
    | ALICE FANTASIA              |
    | ALIEN CENTER                |
    | ALLEY EVOLUTION             |
    | ALONE TRIP                  |
    | ALTER VICTORY               |
    | AMADEUS HOLY                |
    --Plus--

    Another example of the pager command is when you want to estimate a good size for you InnoDB redo logs: the estimation is based on the variation of the Log Sequence Number during a given period of time. Instead of manually looking for the right line in the output of SHOW ENGINE INNODB STATUS (which can be huge), you can call pager to the rescue:

    另外一个pager命令的例子是,当你想要计算出合适的redo log大小时,这个计算规则基于在给定时间内的日志增长量。你可以使用pager而不是人工去看SHOW ENGINE INNODB STATUS里的数据(这太难了)

    mysql> pager grep sequence
    PAGER set to 'grep sequence'
    mysql> show engine innodb status\Gselect sleep(60);show engine innodb status\G
    Log sequence number 380166807992
    1 row in set (0.41 sec)
    
    1 row in set (1 min 0.00 sec)
    
    Log sequence number 380170274979
    1 row in set (0.00 sec)

    When you are done and you want to disable paging, you can simply run:

    完成后恢复到默认值,执行一下pager就行了。

    mysql> pager
    Default pager wasn't set, using stdout.

    Using edit

    When you try to optimize a query, it often involves manipulating the text of the query, and sometimes it would be great to have a text editor inside the client. Well, this can be achieved by using the edit command.

    Let’s say you have the following query:

    当你想优化一个SQL的时候,经常需要修改SQL命令,有时候在客户端里把SQL放到一个文本编辑器里会好很多,使用edit命令可以做到这点。看下面的SQL

    mysql> select count(*) from film left join film_category using(film_id) left join category using(category_id) where name='Music';

    and let’s say you want to change the left joins to inner joins and use capital letters for reserved SQL words. Instead of manually editing the statement, which will be boring, you simply call edit:

    现在你想把left join改成inner join,把关键字大写。你可以简单的运行edit命令,而不是手动去编辑SQL命令

    mysql> edit

    and it will open your default text editor with the text of the last query. The default text editor is vi, so you now have the power of vi inside the mysql client! Once you have made your changes, save and exit the editor: you are back in the mysql client where you can type ; or \G to execute the query.

    它将打开默认的文本编辑器,编辑器里包含最后的SQL命令。默认的编辑器是VI,你在mysql客户端里拥有vi的功能了!完成编辑之后,保存退出VI,然后回到了mysql客户端,输入;或者\G执行新SQL

    Using tee

    In some situations, like when you are testing a set of commands to write documentation or when you are in the middle of an emergency, you want to be able to record the queries that you have executed. The command line client offers the tee command, which will log to a file the statements you typed and their output, pretty much like the Unix tee command:

    在一些场景下,比如你想测试一组命令来编写文档,或者在处理紧急情况,你希望记录下执行过的所有命令。命令行工具提供tee命令,它可以把你输入的和输出的全部记录到一个文件里,和Unix 下的tee命令非常相似

    mysql> tee queries.log
    Logging to file 'queries.log'
    mysql> use sakila
    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
    mysql> select count(*) from sakila;
    ERROR 1146 (42S02): Table 'sakila.sakila' doesn't exist
    mysql> select count(*) from film;
    +----------+
    | count(*) |
    +----------+
    |     1000 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> exit

    And now if you look at the content of the queries.log file, you will see a copy of your session.

    现在你可以看看queries.log文件里的内容,它完全是你操作看到的内容的复制

    Conclusion    结论

    The mysql command line client is not as glossy as most of the graphical tools, but if you know some of its hidden features, it can be very powerful. If you enjoyed these tips, I will write another post with other useful but overlooked features.

    mysql命令行工具不像大部分图形化工具那么友好,但是如果你了解一些它内部的特性,它就很强悍了。如果你喜欢这些小技巧,我将再写一篇文章讲讲别的有用但是经常被忽视的特性。

  • 相关阅读:
    .Net业务搭配实用技术栈(转)
    基于WebGL/Threejs技术的BIM模型轻量化之图元合并
    设计模式之六大原则
    osgearth介绍
    OSG中的示例程序简介
    共有49款Windows GUI开发框架开源软件 【转】
    地球坐标系与投影方式的理解(关于北京54,西安80,WGS84;高斯,兰勃特,墨卡托投影)(转)
    c#串口编程(转)
    c++消息队列的实现
    SQL总结 连表查询
  • 原文地址:https://www.cnblogs.com/zuoxingyu/p/2914618.html
Copyright © 2020-2023  润新知