MySQL Client 小技巧

文章
林里克斯

mysql client 命令的一些小技巧

1、help,?

mysql> help

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.

2、设置 CLI 提示 \R

mysql> \R \u@\h [\d]>
PROMPT set to '\u@\h [\d]>'
root@127.0.0.1 [(none)]>use blog;

Database changed
root@127.0.0.1 [blog]>

3、编辑模式 \e

进入 vi/vim 中编辑 sql,编辑完成之后保存,回到 mysql CLI 中,输入 “;” 后按 enter 键即可执行刚才编辑的 sql 语句

root@127.0.0.1 [blog]>\e
use mysql;
show tables;
(wq vim保存代码)

-> ;

root@127.0.0.1 [blog]>\e
    -> ;
Query OK, 0 rows affected (0.00 sec)

+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| db                        |
| user                      |
+---------------------------+
2 rows in set (0.00 sec)

4、记录操作日志 \T,tee

差不多就是 linux tee 命令的功能,把执行的 sql 语句以及输出结果保存到指定的文件中。\t,取消把查询和输出结果记录到文件中。

root@127.0.0.1 [blog]>\T /tmp/sql.log
Logging to file '/tmp/sql.log'
root@127.0.0.1 [blog]>use mysql;
Database changed
root@127.0.0.1 [mysql]>show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| db                        |
| user                      |
+---------------------------+
2 rows in set (0.00 sec)

$ cat /tmp/sql.log 
root@127.0.0.1 [blog]>use mysql;
Database changed
root@127.0.0.1 [mysql]>show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| db                        |
| user                      |
+---------------------------+
2 rows in set (0.00 sec)

5、执行系统命令!

root@127.0.0.1 [mysql]>\! cat /tmp/netcount.txt
eth0
eth1
lo
lo

6、查看当前连接的状态信息 \s

root@127.0.0.1 [mysql]>\s
--------------
mysql  Ver 14.14 Distrib 5.6.39, for Linux (x86_64) using  EditLine wrapper

Connection id:      751
Current database:   mysql
Current user:       root@172.22.0.1
SSL:            Not in use
Current pager:      stdout
Using outfile:      '/tmp/sql.log'
Using delimiter:    ;
Server version:     5.7.22 MySQL Community Server (GPL)
Protocol version:   10
Connection:     127.0.0.1 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:       3306
Uptime:         6 hours 39 min 27 sec

Threads: 1  Questions: 30425  Slow queries: 0  Opens: 379  Flush tables: 1  Open tables: 372  Queries per second avg: 1.269
--------------

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

712 0 2020-09-19


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-05-18

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 223042 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!