MySQL 实用技巧
概述:
MySQL有许多实用的技巧,利用这些技巧能提高工作的效率,减少一些不必要的麻烦。以下是几个我在MySQL日常维护从常用的技巧。
一、prompt 命令
功能:设置mysql客户端提示符
说明:默认使用mysql命令行登录MySQL服务器后只会有[mysql>]的提示符,利用prompt命令则可以修改默认提示符,有利于数据库的维护,也能有效的防止误操作的发生。
用法:可在配置文件中配置,也可在命令行手动设定,写法有些许不同,详细参数说明如下,也可访问官方文档查看细节,链接如下:
http://dev.mysql.com/doc/refman/5.7/en/mysql-commands.html 详细选项说明:(红色为常用选项)
| Option | Description |
|---|---|
\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 |
\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 |
\ |
|
以下为配置示例:
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
以下为使用示例:
[::](root@localhost)[(none)] > tee /tmp/command.log -- 设置将输出的结果保存到文件中
Logging to file '/tmp/command.log'
[::](root@localhost)[(none)] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| paralleldb |
| performance_schema |
| web |
+--------------------+
rows in set (0.00 sec) [::](root@localhost)[(none)] > system cat /tmp/command.log -- 通过system 命令查看系统文件内容与数据库中内容一致
[::](root@localhost)[(none)] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| paralleldb |
| performance_schema |
| web |
+--------------------+
rows in set (0.00 sec)
[03:46:55](root@localhost)[(none)] > notee -- 使用notee可结束文件的输出
Outfile disabled.
三、pager less/pager命令
功能:实现返回结果页面的分页显示,类似于Linux less 命令,可对分页进行上下翻页,搜索等操作,对返回结果过长很方便,如使用show engine innodb status命令时。
用法:
pager less -i 表示设置页面分页显示,-i表示不区分大小写,在搜索时十分实用。
pager 直接输入pager表示恢复默认值,也就是不分页显示。
示例:略,可自行测试效果
四、concat函数
功能:concat函数能对查询返回的结果进行拼接
用法:concat(s1,s2,...sn)
常用场景:批量生成对数据库的相关修改语句或导出语句
以下为具体示例: 1、批量生成将test数据库中所有表导出为.txt或csv文本的命令
-- 注意需要对单引号用反斜杠(\)进行转义
[04:06:57](root@localhost)[(none)] > select concat('select * into outfile \'/tmp/',table_name,'.csv\' from ',table_schema,'.',table_name,';')
from information_schema.tables where table_schema='test';
+---------------------------------------------------------------------------------------------------+
| concat('select * into outfile \'/tmp/',table_name,'.csv\' from ',table_schema,'.',table_name,';') |
+---------------------------------------------------------------------------------------------------+
| select * into outfile '/tmp/address.csv' from test.address; |
| select * into outfile '/tmp/address1.csv' from test.address1; |
| select * into outfile '/tmp/clone_t.csv' from test.clone_t; |
| select * into outfile '/tmp/lock_test.csv' from test.lock_test; |
| select * into outfile '/tmp/query_history.csv' from test.query_history; |
| select * into outfile '/tmp/query_review.csv' from test.query_review; |
| select * into outfile '/tmp/ram.csv' from test.ram; |
| select * into outfile '/tmp/t.csv' from test.t; |
| select * into outfile '/tmp/t1.csv' from test.t1; |
| select * into outfile '/tmp/t2.csv' from test.t2; |
| select * into outfile '/tmp/t3.csv' from test.t3; |
| select * into outfile '/tmp/z.csv' from test.z; |
+---------------------------------------------------------------------------------------------------+
当需要对数据库的表进行批量操作时,通过这种方式批量生成语句能大大提高效率
2、去除结果的虚线框并将结果输出到文件中
-- 通常我们需要将生成的语句保存到一个文件中,并且运行,这时就需要配合tee命令以及mysql客户端的参数了
mysql --skip-column-names --silent (简写为 mysql -ss)能以静默方式登录MySQL服务器并不显示输出结果的虚线框
tee /tmp/export.sql 将执行的结果保存到文件中
[root@manager ~]# mysql -uroot -p -ss -- 使用静默方式登录数据库
Enter password:
[04:18:55](root@localhost)[(none)] > tee /tmp/export.sql -- 将结果保存到外部文件中
Logging to file '/tmp/export.sql'
[04:19:04](root@localhost)[(none)] > select concat('select * into outfile \'/tmp/',table_name,'.csv\' from ',table_schema,'.',table_name,';')
from information_schema.tables where table_schema='test';
-- 可以看到输出的结果没有虚线框
select * into outfile '/tmp/address.csv' from test.address;
select * into outfile '/tmp/address1.csv' from test.address1;
select * into outfile '/tmp/clone_t.csv' from test.clone_t;
select * into outfile '/tmp/lock_test.csv' from test.lock_test;
select * into outfile '/tmp/query_history.csv' from test.query_history;
select * into outfile '/tmp/query_review.csv' from test.query_review;
select * into outfile '/tmp/ram.csv' from test.ram;
select * into outfile '/tmp/t.csv' from test.t;
select * into outfile '/tmp/t1.csv' from test.t1;
select * into outfile '/tmp/t2.csv' from test.t2;
select * into outfile '/tmp/t3.csv' from test.t3;
select * into outfile '/tmp/z.csv' from test.z;
[04:19:08](root@localhost)[(none)] > exit -- 查看tee输出文件中的内容
[root@manager ~]# cat /tmp/export.sql
[04:19:04](root@localhost)[(none)] > select concat('select * into outfile \'/tmp/',table_name,'.csv\' from ',table_schema,'.',table_name,';') from information_schema.tables where table_schema='test';
select * into outfile '/tmp/address.csv' from test.address;
select * into outfile '/tmp/address1.csv' from test.address1;
select * into outfile '/tmp/clone_t.csv' from test.clone_t;
select * into outfile '/tmp/lock_test.csv' from test.lock_test;
select * into outfile '/tmp/query_history.csv' from test.query_history;
select * into outfile '/tmp/query_review.csv' from test.query_review;
select * into outfile '/tmp/ram.csv' from test.ram;
select * into outfile '/tmp/t.csv' from test.t;
select * into outfile '/tmp/t1.csv' from test.t1;
select * into outfile '/tmp/t2.csv' from test.t2;
select * into outfile '/tmp/t3.csv' from test.t3;
select * into outfile '/tmp/z.csv' from test.z;
[04:19:08](root@localhost)[(none)] > exit
-- 文件内容记录了所有的操作及输出的结果,可以编辑文件,将文件头的查询语句及文件尾的退出语句删除,则完成了一个将数据库导出成文本文件的的脚本。
MySQL 实用技巧的更多相关文章
- MySQL实用技巧
自增Id重新计数 TRUNCATE TABLE 表名 获取最后插入数据的ID SELECT LAST_INSERT_ID(); 使用"id1,id2,id3"当参数 FIN ...
- PowerDesigner实用技巧小结(3)
PowerDesigner实用技巧小结(3) PowerDesigner 技巧小结 sqlserver数据库databasevbscriptsqldomain 1.PowerDesigner 使用 M ...
- .NET Core实用技巧(一)如何将EF Core生成的SQL语句显示在控制台中
目录 .NET Core实用技巧(一)如何将EF Core生成的SQL语句显示在控制台中 前言 笔者最近在开发和维护一个.NET Core项目,其中使用几个非常有意思的.NET Core相关的扩展,在 ...
- Notepad++ 实用技巧
Notepad++是一款开源的文本编辑器,功能强大.很适合用于编辑.注释代码.它支持绝大部分主流的编程语言. 本文主要列举了本人在实际使用中遇到的一些技巧. 快捷键 自定义快捷键 首先,需要知道的是: ...
- javascript实用技巧、javascript高级技巧
字号+作者:H5之家 来源:H5之家 2016-10-31 11:00 我要评论( ) 三零网提供网络编程. JavaScript 的技术文章javascript实用技巧.javascript高级技巧 ...
- 您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧
转载至: http://www.cnblogs.com/Imaigne/p/4153397.html 您的项目引用了最新实体框架:但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mys ...
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...
- iOS开发实用技巧—在手机浏览器头部弹出app应用下载提示
iOS开发实用技巧—在手机浏览器头部弹出app应用下载提示 本文介绍其简单使用: 第一步:在本地建立一个访问的服务端. 打开本地终端,在本地新建一个文件夹,在该文件夹中存放测试的html页面. ...
- iOS开发实用技巧—项目新特性页面的处理
iOS开发实用技巧篇—项目新特性页面的处理 说明:本文主要说明在项目开发中会涉及到的最最简单的新特性界面(实用UIScrollView展示多张图片的轮播)的处理. 代码示例: 新建一个专门的处理新特性 ...
随机推荐
- Spring的配置相关知识(学习spring boot的预备知识)
我们经常说的控制反转(Inversion of Control-IOC)和依赖注入(dependency injection-DI)在Spring环境下是等同的概念,控制反转是通过依赖注入实现的.所谓 ...
- JDK 中的监控与故障处理工具-02 (jps)
jps : JVM Process Status Tool jps 命令可以列出正在运行的虚拟机进程, 并显示虚拟机执行的 main class 的名称(main函数所在的类),以及这些进程的本地虚拟 ...
- WIN7环境安装informatica 提示 不能创建Domain或者node
查看infa安装的bat文件install.bat,会发现,它调用的是.\Server\Windows\Disk1\InstData\VM\install.exe.所以,我们在安装执行,右键insta ...
- 百度云如何免费扩容至2055G?
百度云如何免费扩容至2055G? 上篇说到整一个新的百度账号,那么5G的百度云内存肯定满足不了我们收集癖的需求.那么就来了解一下怎么扩容吧. 主要是在手机端实现的 用这个新的百度账号在手机APP上登录 ...
- Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'
Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String' 一.发现问题 <select ...
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...
- spring boot2.1读取 apollo 配置中心1
第一篇:搭建apollo配置中心 为什么选择apollo,我做了一些对比: Diamond Disconf Apollo Spring Cloud Config 数据持久性 mysql mysql ...
- python+opencv链接摄像头
import cv2 import numpy as numpy cap=cv2.VideoCapture(0) #设置显示分辨率和FPS ,不设置的话会非常卡 cap.set(cv2.CAP_PRO ...
- Django 2.0 的路由如何实现正则表达式
在django2.0的路由系统中,摒弃了1.x中的url,而改用path.需要导入path. from django.urls import path,re_path 在1.x中,使用url()即可实 ...
- 新东方雅思词汇---10.1、(a)esthet
新东方雅思词汇---10.1.(a)esthet 一.总结 一句话总结: 感觉 aesthetic 英 [i:sˈθetɪk] 美 [esˈθetɪk; ɛsˈθɛtɪk] adj. 美的:美学的 ...