在多个数据库的主目录中配置 .my.cnf 不起作用。适用于单个数据库

Configuring .my.cnf in home directory for multiple databases does not work. Works for single database

这是我的 .my.cnf 文件:

 [client]
 user=user1
 password=somePasswd1
 database=someDb
 [client2]
 user=user1
 password=somePassed2
 database=someotherDb

如果我只有一个条目就可以工作,如:

 [client]
 user=user1
 password=somePasswd1
 database=someDb

[client]的意义是什么?它应该有什么?另外,如果我只想限制本地主机的这些用户密码怎么办?

https://dev.mysql.com/doc/refman/8.0/en/option-files.html 说:

The [client] option group is read by all client programs provided in MySQL distributions (but not by mysqld).

The [client] group enables you to specify options that apply to all clients. For example, [client] is the appropriate group to use to specify the password for connecting to the server. (But make sure that the option file is accessible only by yourself, so that other people cannot discover your password.) Be sure not to put an option in the [client] group unless it is recognized by all client programs that you use.

MySQL客户端程序在手册中:https://dev.mysql.com/doc/refman/8.0/en/programs-client.html

如果您使用像 [client2] 这样的选项组,除非您使用 --defaults-group-suffix 选项,否则不会使用它。

https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html 说:

--defaults-group-suffix=str

Read not only the usual option groups, but also groups with the usual names and a suffix of str. For example, the mysql client normally reads the [client] and [mysql] groups. If the --defaults-group-suffix=_other option is given, mysql also reads the [client_other] and [mysql_other] groups.

对于你的情况,你可以 运行:

mysql --defaults-group-suffix=2

这将使 mysql 客户端从您的选项文件中的 [client2] 组读取选项。

“此外,如果我只想将这些用户密码限制为本地主机怎么办?”

这是在您向用户授予权限时处理的。

GRANT ... ON *.* TO 'user1'@'localhost';

通过在 user1 之后指定主机,这意味着授权仅在 user1 从本地主机连接时有效。如果 user1 试图从任何其他主机连接,授权将不起作用。这包括密码凭证本身。阅读 https://dev.mysql.com/doc/refman/8.0/en/grant.html 了解更多信息。