由于经常对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,

有知道原因的读者请多赐教,深表感谢!!!

mysql中的tee_详解mysql的tee功能 并利用其记录相关操作相关推荐

  1. pdo mysql limit_PHP mysql中limit用法详解(代码示例)

    在MySQL中,LIMIT子句与SELECT语句一起使用,以限制结果集中的行数.LIMIT子句接受一个或两个offset和count的参数.这两个参数的值都可以是零或正整数. offset:用于指定要 ...

  2. mysql 日期比较_详解Mysql中日期比较大小的方法

    假如有个表product有个字段add_time,它的数据类型为datetime,有人可能会这样写sql: 代码如下 select * from product where add_time = '2 ...

  3. lsdyna如何设置set中的node_list_详解MySQL数据库如何实现类似Oracle的序列?

    概述 众所周知,Oracle一般使用序列(Sequence)来处理主键字段,而MySQL则提供了自增长(increment)来实现类似的目的. 不过小编在实际使用过程中发现,MySQL的自增长有诸多的 ...

  4. mysql inner join where_详解mysql 使用left join添加where条件的问题分析

    当前需求: 有group和factor两张表,一个group对应多个factor,现在想查询有效的group和对应的有效的factor,两个表都有isDel逻辑删除标志. 最开始的错误写法一: SEL ...

  5. mysql sql组合_详解mysql 组合查询

    使用UNION 多数SQL查询都只包含一个或多个表中返回数据的单条SELECT语句.MySQL也允许执行多个查询(多条SELECT语句),并将结果作为单个查询结果集返回.这些组合查询通常称为并(uni ...

  6. phpstudy mysql恢复数据_MySQL_详解MySQL误操作后怎样进行数据恢复,一、开启binlog。 首先查看binlo - phpStudy...

    详解MySQL误操作后怎样进行数据恢复 一.开启binlog. 首先查看binlog是否开启 mysql> show variables like "log_bin"; +- ...

  7. mysql 数据分组_详解MySQL 数据分组

    创建分组 分组是在select语句中的group by 子句中建立的. 例: select vend_id, count(*) as num_prods from products group by ...

  8. java mysql 自动提交_详解MySQL与Spring的自动提交(autocommit)

    1 MySQL的autocommit设置 MySQL默认是开启自动提交的,即每一条DML(增删改)语句都会被作为一个单独的事务进行隐式提交.如果修改为关闭状态,则执行DML语句之后要手动提交 才能生效 ...

  9. mysql 事务 for update,mysql事务锁_详解mysql 锁表 for update

    摘要 腾兴网为您分享:详解mysql 锁表 for update,智慧农业,真还赚,悦读小说,学习帮等软件知识,以及电池管家,三国群英传3,userland,运满满货主版,王者荣耀,简单3d动画,嘉丽 ...

最新文章

  1. Nutanix CE on Lenovo W520 初探
  2. 书单 | 春日必读书,少看一本都是遗憾
  3. 052_Unicode字符官方标准三
  4. 物联网:应用创新带来万亿元市场前景
  5. for ie无效 in js_关于js中for in的缺陷浅析
  6. 线性代数二之矩阵加速DP——数学作业,Arc of Dream
  7. unity中怎么在InspectorI面板加LOGO
  8. FreeCodeCamp纳什维尔聚会的回顾
  9. 浙江工业大学计算机应用基础,浙江工业大学期终考试命题稿-浙江大学人文学院.doc...
  10. Vue 2.x 实战之后台管理系统开发(二)
  11. 生宣、熟宣、半生半熟宣纸各有什么特点?初学书法用哪种宣纸好?
  12. 为什么说:“你不合适学Python?”醍醐灌顶!
  13. java 易错题_java错题集(1-3)
  14. Image Gallery
  15. 易用性软件测试用例,软件性能测试规定——易用性测试规定
  16. 常见电子元器件检测方法。——Arvin
  17. javasctip中文手册javascript视频教程下载
  18. dwg文件打开的步骤具体是什么
  19. 局域网联机_红警如何局域网联机?详细联机教程,方法特别简单
  20. COGS 577 蝗灾

热门文章

  1. 思必驰自研AI芯片不仅方式独特,首代毫瓦级AI语音芯片也仅用1年...
  2. 计算机教学情景教学法列子,情景案例教学法在计算机教学中的应用.doc
  3. 基于某大学的论文系统的开发实例分析
  4. 8086汇编指令笔记
  5. Windows Server - 对域控制器进行重命名和修改 IP Address
  6. libevent与libev简介
  7. 机器学习基石上思维导图
  8. adobe都有什么软件
  9. 淘宝装修HTML代码大全
  10. 全球地名中英文对照表(D)