我们在平常在开发环境使用 MySQL 的过程中,经常需要命令行登录, 比如:

mysql -h localhost -P 3306 -u root -D steel_server -p
******

mysql>
  • -h 服务地址
  • -P 服务端口
  • -u 服务用户
  • -D 默认数据库
  • -p 输入密码

在开发环境每次都输入这么长的内容, 也挺不方便的,于是看了下, mysql 命令是否支持本地配置的功能, man mysql 中有这部分描述

       ·   Use an option file.  You can set the prompt option in the [mysql] group of any MySQL option file, such as /etc/my.cnf or the .my.cnf file in your home directory. For example:

               [mysql]
               prompt=(\\u@\\h) [\\d]>\\_
mysql supports the following options, which can be specified on the command line or in the [mysql] and [client] option file groups.  mysql also supports the options for processing option files described at Section 4.2.3.3.1,
       “Command-Line Options that Affect Option-File Handling”.

可选文件 .my.cnf 用于用户环境下的配置信息, 将下述内容添加到该文件中.

[client]
host        = 192.168.56.10
user        = root
password    = admin
port        = 3306
database    = example

之后,你在命令行下要登录开发库直接输入 mysql 就可以顺利访问进对应database了.

在本地环境/开发环境等安全性要求不是很高的地方才建议配置默认环境信息,一般来说在生产环境是严禁这么配置的.

【腾讯云】境外1核2G服务器低至2折,半价续费券限量免费领取!
https://cloud.tencent.com/act/cps/redirect?redirect=1068&cps_key=e4b50f6c64a4480367f8a8d16fd07c5a&from=console

标签: MySQL, .my.cnf, 默认配置信息

添加新评论