MySQL
NinGoo's blog

MySQL命令行的几个用法(续二)

前面两篇(看这里,看这里)介绍了MySQL命令行的一些技巧,这里再介绍下通过配置文件来设置MySQL命令行的这些参数。

通过/etc/my.cnf配置文件的[mysql]部分,可以设置MySQL命令行的一些运行参数。例如:

[mysql]
prompt=\\u@\\d \\r:\\m:\\s>
pager='less -S'
tee='/tmp/mysql.log'

通过prompt设置显示用户名,当前数据库和当前时间,注意在配置文件里最好使用双斜杠:

root@poster 10:26:35>

通过pager设置使用less来显示查询结果,-S表示截断超过屏幕宽度的行,一行太长MySQL的显示格式就显得很乱,如果要看完整的行,建议使用\G将行垂直输出。当然,你也可以添加更多less的参数来控制输出。

tee则将MySQL执行的所有输出保存到一个日志文件中,即使使用less -S截断了超长行,在日志中还是会记录整个的结果,另外,前面通过prompt设置了当前时间显示,这样也便于在日志文件中查看每次操作的时间。由于tee的结果是附加到文件中的,日志文件需要定期清除。

缓存为王:Memcached和MySQL的结合应用

在计算机和网络的世界里,缓存无处不在,从CPU的寄存器到一级缓存二级缓存,从硬盘的文件到阵列卡的缓存到文件的缓存,从浏览器的缓存到web服务器的缓存到数据库的缓存,Cache is King

Memcached是一个分布式的内存缓存系统,在很多大型网站中都有成功的应用案例。设计思想说起来也很简单,服务器端保存精悍而独立,基本上就是一个slab对象缓存管理器,而在客户端实现负载均衡,采用hash算法来选择不同的服务端缓存key-value数据。

FotologFrank同学最近在MySQL官方网站上做了一个Memcached结合MySQL应用的webinar

MySQL命令行的几个用法(续)

继续上一篇的话题,介绍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

MySQL如何获取当前执行的SQL

在MySQL的客户端中通过show [full] processlist可以看到当前所有线程,以及线程的状态,当然也包括执行中的sql,但是显示的语句不完全,并且有很多空闲线程会造成干扰。

这里要介绍一个非常好用的开源工具innotop(sourceforge已经被墙了,要过去先找梯子),由于是用perl写的,所以需要安装相关的perl模块,包括DBIDBD::mysqlTerm::ReadKeyTime::HiRes,这些模块都可以到CPAN找到。

innotop的Q模式则可以完美的解决获取当前运行的SQL的问题。innotop -m Q 或者innotop进入后再按shift+q进入Query list模式:

Query List (? for help) mysql01, 75+03:16:16, 774.20 QPS, 83 thd, 5.1.24-rc-log

CXN    When   Load  QPS     Slow  QCacheHit  KCacheHit  BpsIn    BpsOut
my120  Now    0.00  774.20     0     40.22%    100.00%  207.98k    1.46M
my120  Total  0.00  212.69     2     29.70%    100.00%   56.90k  402.15k

CXN     Cmd    ID      User    Host           DB      Time   Query
mysql01  Query   20936  poster  192.168.1.1    poster  00:00
select a.poster_id, a.pic_id, a.gmt_create, a.gmt_modified, a.url, a.no

然后按e并输入thread ID显示执行计划或者按f显示完整sql语句,或者按o显示系统优化过的语句(需要MySQL的版本支持EXPLAIN EXTENDED)。个人感觉,还是e最有用,其他两个选项,则有点鸡肋了。

Query List (? for help) mysql01, 75+03:16:16, 774.20 QPS, 83 thd, 5.1.24-rc-log
EXPLAIN PARTITIONS
select a.poster_id, a.pic_id, a.gmt_create, a.gmt_modified, a.url, a.notes,
a.type, a.indexed, a.user_id, a.user_nick, b.pic_path
from poster.poster_pic a inner join poster.picture b on
(a.pic_id = b.id) where a.poster_id = 3390 order by a.indexed
______________ Sub-Part 1 ______________  ________ Sub-Part 1 ________
Select Type: SIMPLE                       Select Type: SIMPLE
      Table: a                                  Table: b
 Partitions:                               Partitions:
       Type: ref                                 Type: eq_ref
 Poss. Keys: PRIMARY                       Poss. Keys: PRIMARY
      Index: PRIMARY                            Index: PRIMARY
 Key Length: 4                             Key Length: 8
  Index Ref: const                          Index Ref: poster.a.PIC_ID
  Row Count: 14                             Row Count: 1
    Special: Using where; Using filesort      Special:                

Press e to explain, f for full query, o for optimized query
Query List (? for help) mysql01, 75+03:16:16, 774.20 QPS, 83 thd, 5.1.24-rc-log
select a.poster_id, a.pic_id, a.gmt_create, a.gmt_modified, a.url, a.notes,
a.type, a.indexed, a.user_id, a.user_nick, b.pic_path
from poster.poster_pic a inner join poster.picture b on
(a.pic_id = b.id) where a.poster_id = 3390 order by a.indexed

Press e to explain, f for full query, o for optimized query
Query List (? for help) mysql01, 75+03:16:16, 774.20 QPS, 83 thd, 5.1.24-rc-log
select a.poster_id, a.pic_id, a.gmt_create, a.gmt_modified, a.url, a.notes,
a.type, a.indexed, a.user_id, a.user_nick, b.pic_path
from poster.poster_pic a inner join poster.picture b on
(a.pic_id = b.id) where a.poster_id = 3390 order by a.indexed
Note:
select `poster`.`a`.`POSTER_ID` AS `poster_id`,`poster`.`a`.`PIC_ID` AS
`pic_id`,`poster`.`a`.`GMT_CREATE` AS `gmt_create`,
`poster`.`a`.`GMT_MODIFIED` AS `gmt_modified`,
`poster`.`a`.`URL` AS `url`,`poster`.`a`.`NOTES` AS `notes`,
`poster`.`a`.`TYPE` AS `type`,`poster`.`a`.`INDEXED` AS `indexed`,
`poster`.`a`.`USER_ID` AS `user_id`,`poster`.`a`.`user_nick` AS
`user_nick`,`poster`.`b`.`PIC_PATH` AS `pic_path`
from `poster`.`poster_pic` `a` join `poster`.`picture` `b`
where ((`poster`.`b`.`ID` = `poster`.`a`.`PIC_ID`) and
(`poster`.`a`.`POSTER_ID` = 3390))
order by `poster`.`a`.`INDEXED`

Press e to explain, f for full query, o for optimized query

那么innotop是从哪里取的数据呢?应该是通过information_schema.processlist来获得完整的sql语句,并且根据COMMAND来过滤掉空闲线程的。

mysql> desc information_schema.processlist;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ID      | bigint(4)   | NO   |     | 0       |       |
| USER    | varchar(16) | NO   |     |         |       |
| HOST    | varchar(64) | NO   |     |         |       |
| DB      | varchar(64) | YES  |     | NULL    |       |
| COMMAND | varchar(16) | NO   |     |         |       |
| TIME    | bigint(7)   | NO   |     | 0       |       |
| STATE   | varchar(64) | YES  |     | NULL    |       |
| INFO    | longtext    | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)