mysql prompt用法详解

  当我们用mysql client连接mysql实例的时候我们想要显示我们连接的是那个实例、用哪个账号登录的、现在在哪个database中、现在系统时间等等很多信息的时候,那么mysql prompt将需要配置,以下将介绍prompt的配置详解,以及配置方法:

1、我们可以放在默认配置文件的mysql项目中,当我们登录的时候我们就可以看到如期显示了我们配置的信息:

root@DESKTOP-1T4MD6P:~# more /etc/my.cnf
[mysql]
prompt="\\u@\\h [\\d]>"
root@DESKTOP-1T4MD6P:~# mysql -S /tmp/mysql3306.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

root@localhost [(none)]>

2、如果我们的配置文件不是默认配置文件,那么我们登录的时候需要指定配置文件也可以:

root@DESKTOP-1T4MD6P:~# mysql --defaults-file=/etc/myback.cnf -S /tmp/mysql3306.sockWelcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

root@localhost [(none)]>

3、如果我们只是临时用一下不想创建或者修改配置文件,那么我们可以在登录的时候直接指定参数:

root@DESKTOP-1T4MD6P:~# mysql -S /tmp/mysql3306.sock --prompt="\\u@\\h [\\d]>"

接下来我们整理一下可以定制哪些信息以及哪个字符对应什么信息:

\C 当前连接的标志符,也就是说从show processlist中看到当前连接的ID
\c 每次新连接执行语句计数器
\D 当前完整时间,包括年月日时分秒
\d 当前数据库,比如user test则显示test,如果没有执行use命令则显示(none)
\h 实例连接地址
\l ";"分界符,可以用于多个配置之间
\m 当前时间分钟
\n 换行符
\O 三个字母的月份
\o 数字格式的月份
\P 上午下午
\p 当前TCP/IP端口
\R 当前时间小时,24时制
\r 当前时间小时,12时制
\S 分号
\s 当前时间秒
\t 制表符
\U 完整账户名称user_name@host_name
\u 用户名user_name
\v MySQL服务器版本
\w 当前周几
\Y 当前4位数字年
\y 当前2位数字年
\_ 空格
\  
\' 单引号
\" 双引号
\\  
\x  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

其实我们日常用的比较多的可能就是用户名、连接地址、数据库名、时间等参数,可以根据需求定制;

posted @ 2021-04-02 18:01  小有志气  阅读(1696)  评论(0编辑  收藏  举报