首页 > 数据库技术 > 详细

MySQL 实用技巧

时间:2016-07-22 22:43:42      阅读:387      评论:0      收藏:0      [点我收藏+]

概述:

  MySQL有许多实用的技巧,利用这些技巧能提高工作的效率,减少一些不必要的麻烦。

一、prompt 命令

功能:设置mysql客户端提示符
说明:默认使用mysql命令行登录MySQL服务器后只会有[mysql>]的提示符,利用prompt命令则可以修改默认提示符,有利于数据库的维护,也能有效的防止误操作的发生。
用法:可在配置文件中配置,也可在命令行手动设定,写法有些许不同,详细参数说明如下,也可访问官方文档查看细节,链接如下:
http://dev.mysql.com/doc/refman/5.7/en/mysql-commands.html

详细选项说明:(红色为常用选项)
OptionDescription
\C The current connection identifier (MySQL 5.7.6 and up)
\c A counter that increments for each statement you issue
\D The full current date
\d The default database
\h The server host
\l The current delimiter
\m Minutes of the current time
\n A newline character
\O The current month in three-letter format (Jan, Feb, …)
\o The current month in numeric format
\P am/pm
\p The current TCP/IP port or socket file
\R The current time, in 24-hour military time (0–23)
\r The current time, standard 12-hour time (1–12)
\S Semicolon
\s Seconds of the current time
\t A tab character
\U

Your full user_name@host_name account name

\u Your user name
\v The server version
\w The current day of the week in three-letter format (Mon, Tue, …)
\Y The current year, four digits
\y The current year, two digits
\_ A space
A space (a space follows the backslash)
\‘ Single quote
\" Double quote
\\ A literal \ backslash character
\x

x, for any x not listed above


以下为配置示例:
1、在my.cnf配置文件[client]部分中增加以下配置 prompt="[\\r:\\m:\\s](\\U)[\\d] > " -- 在配置文件中配置都需要用\对配置的参数进行转义

[root@manager ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.10-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

[03:11:57](root@localhost)[(none)] >  -- 显示时间 + 登录用户 + 当前使用的数据库(未使用则为none)
[03:15:40](root@localhost)[test] >
2、在mysql客户端内直接配置
mysql>prompt [\r:\m:\s](\U)[\d] >    
PROMPT set to [\r:\m:\s](\U)[\d] >
[03:22:31](root@localhost)[(none)] > -- 显示时间 + 登录用户 + 当前使用的数据库
[03:22:31](root@localhost)[(none)] >prompt (\U)[\d] >
PROMPT set to (\U)[\d] >
(root@localhost)[(none)] > -- 只显示 登录用户 + 当前使用的数据库
 

 

二、tee/notee

功能:tee实现将命令行中的输入输出结果保存到文本文件中,使用该命令能方便的将命令进行日志记录。 
用法:tee /tmp/command.log
以下为使用示例:
[03:43:49](root@localhost)[(none)] > tee /tmp/command.log    -- 设置将输出的结果保存到文件中
Logging to file ‘/tmp/command.log‘
[03:44:02](root@localhost)[(none)] > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| paralleldb         |
| performance_schema |
| web                |
+--------------------+
10 rows in set (0.00 sec)

[03:44:18](root@localhost)[(none)] > system cat /tmp/command.log    -- 通过system 命令查看系统文件内容与数据库中内容一致
[03:44:02](root@localhost)[(none)] > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| paralleldb         |
| performance_schema |
| web                |
+--------------------+
10 rows in set (0.00 sec)

[03:46:55](root@localhost)[(none)] > notee         -- 使用notee可结束文件的输出
Outfile disabled.

 

MySQL 实用技巧

原文:http://www.cnblogs.com/zhenxing/p/5697223.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!