詳解mysql的tee功能 並利用其記錄相關操作

由於經常對mysql數據庫進行大量的更改操作,比如更改字段,添加或刪除索引等等,我們把這些操作放到sql語句中,然後登陸mysql,通過source執行該sql文件,爲了做好相關記錄,方便以後的工作中進行覈對查詢,將MySQL中sql運行結果保存到文件,類似Oracle sqlplus中利用spool的功能,方法大致如下,僅供參考:

1、登陸mysql之後
mysql> \T mysql_result.log
Logging to file 'mysql_result.log'
或者
mysql> tee mysql_result.log
Logging to file 'mysql_result.log'

2、[root@tech ~]# mysql --tee=mysql_result.log

3、關閉mysql,然後修改mysql的配置文件中[client]選項段,添加以下內容(tee = /tmp/mysql_result.log)
例如:
[root@tech ~]# vim /usr/local/mysql/etc/my.cnf
[client]
tee             = /tmp/mysql_result.log

以下是我的操作詳細記錄:
[root@tech ~]# mysql -V
mysql  Ver 14.14 Distrib 5.1.45, for pc-linux-gnu (i686) using readline 5.1
[root@tech ~]# mysql --help | grep tee
  --tee=name          Append everything into outfile. See interactive help (\h)
                      --disable-tee. This option is disabled by default.
  --no-tee            Disable outfile. See interactive help (\h) also. WARNING:
                      Option deprecated; use --disable-tee instead.
[root@tech ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.45 Source distribution

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

mysql> \h

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'


mysql> \T mysql_result.log
Logging to file 'mysql_result.log'
mysql> tee mysql_result.log
Logging to file 'mysql_result.log'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| postfix            |
| test               |
| ucenter            |
| wordpress          |
| zabbix             |
+--------------------+
7 rows in set (0.00 sec)

mysql> show processlist;
+----+------+-----------+---------+---------+------+-------+------------------+
| Id | User | Host      | db      | Command | Time | State | Info             |
+----+------+-----------+---------+---------+------+-------+------------------+
|  4 | root | localhost | ucenter | Sleep   | 5564 |       | NULL             |
|  5 | root | localhost | NULL    | Query   |    0 | NULL  | show processlist |
+----+------+-----------+---------+---------+------+-------+------------------+
2 rows in set (0.01 sec)

mysql> \q
Bye
[root@tech ~]# cat mysql_result.log

mysql> tee mysql_result.log
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| postfix            |
| test               |
| ucenter            |
| wordpress          |
| zabbix             |
+--------------------+
7 rows in set (0.00 sec)

mysql> show processlist;
+----+------+-----------+---------+---------+------+-------+------------------+
| Id | User | Host      | db      | Command | Time | State | Info             |
+----+------+-----------+---------+---------+------+-------+------------------+
|  4 | root | localhost | ucenter | Sleep   | 5564 |       | NULL             |
|  5 | root | localhost | NULL    | Query   |    0 | NULL  | show processlist |
+----+------+-----------+---------+---------+------+-------+------------------+
2 rows in set (0.01 sec)

mysql> \q
[root@tech ~]# mysql --tee=mysql_result.log
Logging to file 'mysql_result.log'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.45 Source distribution

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

mysql> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| FEDERATED  | NO      | Federated MySQL storage engine                             | NULL         | NULL | NULL       |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
6 rows in set (0.00 sec)

mysql> \q
Bye
[root@tech ~]# mysqladmin shutdown
[root@tech ~]# vim /usr/local/mysql/etc/my.cnf
[client]
tee             = /tmp/mysql_result.log
[root@tech ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf --datadir=/data/mysql/data --user=mysql &
[1] 2620
[root@tech ~]# 120731 20:28:30 mysqld_safe Logging to '/data/mysql/data/tech.err'.
120731 20:28:30 mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
[root@tech ~]# mysql
Logging to file '/tmp/mysql_result.log'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.45 Source distribution

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

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.1.45, for pc-linux-gnu (i686) using readline 5.1

Connection id:          1
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          '/tmp/mysql_result.log'
Using delimiter:        ;
Server version:         5.1.45 Source distribution
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /tmp/mysql.sock
Uptime:                 37 sec

Threads: 1  Questions: 4  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 4  Queries per second avg: 0.108
--------------

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.45    |
+-----------+
1 row in set (0.00 sec)

mysql> \q
Bye
[root@tech ~]# ll /tmp/mysql_result.log
-rw-r--r-- 1 root root 932 Jul 31 20:29 /tmp/mysql_result.log
[root@tech ~]# more /tmp/mysql_result.log
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.45 Source distribution

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

mysql> \s
--------------

Connection id:          1
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          '/tmp/mysql_result.log'
Using delimiter:        ;
Server version:         5.1.45 Source distribution
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /tmp/mysql.sock
Uptime:                 37 sec

Threads: 1  Questions: 4  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 4  Queries per second avg: 0.108
--------------

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.45    |
+-----------+
1 row in set (0.00 sec)

mysql> \q

通過以下來關閉mysql的tee功能:
mysql> \t
Outfile disabled.
或者
mysql> notee
Outfile disabled.

針對不同版本的mysql可能有所差異,本次測試用的mysql數據庫版本是5.1.45,本人也親自測試了,5.1.45 和5.1.56都沒有問題,有些版本的mysql數據庫的tee功能在寫入配置文件的時候不生效,但是支持終端下命令行直接操作,例如:4.0.26和5.1.60,目前還不清楚原因何在,猜測也有可能是某些版本存在bug,有知道原因的讀者請多賜教,深表感謝!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章