配置登录路径
MySQL 5.6.6 以及以上版本支持使用登录路径(Login Path)来存储连接信息,包括用户名和密码。 通过
mysql_config_editor工具可以安全地将登录路径与密码关联,而不会暴露密码。 执行命令后,会在当前用户下创建一个加密的文件(~/.mylogin.cnf)保存登录信息。
mysql_config_editor set --login-path=client --host=localhost --user=username --password
MySQL 5.7.33 和 8.0.23 之前的版本,存在 BUG 95597 ,即:密码存在
#字符,会被当做注释,修复:使用双引号将字符串引起来。
然后,连接数据库时可以简单地使用登录路径:
mysql --login-path=client
其他常用操作
# 打印当前创建的所有登录路径
mysql_config_editor print --all
# 删除指定的登录路径:client, 如果不指定则删除当前所有登录路径。
mysql_config_editor remove client
使用登录路径
mysql --login-path=client
# 无需密码就可以直接连接到配置好的数据库
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 94306
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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.
mysql>
评论