由于经常对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 distributionType 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \h
For information about MySQL products and services, visit:
For developer information, including the MySQL Reference Manual, visit: To buy MySQL Enterprise support, training, or other products, visit: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.logmysql> 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 distributionType '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 distributionType '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.1Connection id: 1
Current database: Current user: 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 secThreads: 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 distributionType 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \s
--------------Connection id: 1
Current database: Current user: 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 secThreads: 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,有知道原因的读者请多赐教,深表感谢!!!