Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is my .my.cnf file:

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

It works if I just have one entry, as in:

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

What is the significance of [client]? What should it have? Also, what if I want to restrict these user-passwords for localhost only?

like image 759
Sunny Avatar asked Sep 09 '18 11:09

Sunny


People also ask

How do I include all CNF files in a directory?

The !includedir directive can be used to include all .cnf files (and potentially .ini files) in a given directory. The option files within the directory are read in alphabetical order. See the Including Option File Directories section below for more information on this syntax.

How to replace mysqluser and mysqlpass with CNF?

This one is for lazy ones! If you are paranoid about security, do not use this. Create file ~/.my.cnf and add following lines in it and replace mysqluser & mysqlpass values. For safety, make this file readable to you only by running chmod 0600 ~/.my.cnf

Is there a SQL command to list all CNFs in a database?

It is not an SQL command though. Rather, execute: In the very first lines you will find a message with a list of all my.cnf locations it looks for. On my machine it is:

Where can I find the installation directory for MariaDB?

For example, if mysqld.exe is in C:\Program Files\ MariaDB 10.3 \bin, then INSTALLDIR would be C:\Program Files\ MariaDB 10.3 . MARIADB_HOME (from MariaDB 10.6) or MYSQL_HOME is the environment variable containing the path to the directory holding the server-specific my.cnf file.


1 Answers

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

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.

The MySQL client programs are in the manual: https://dev.mysql.com/doc/refman/8.0/en/programs-client.html

If you use an option group like [client2] this is not used unless you use the --defaults-group-suffix option.

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

--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.

In your case, you could run:

mysql --defaults-group-suffix=2

That would make the mysql client read options from the [client2] group in your option file.

"Also, what if I want to restrict these user-passwords for localhost only?"

This is handled when you GRANT privileges to your user.

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

By specifying the host after user1, it means the grant only works when user1 connects from localhost. If user1 is trying to connect from any other host, the grants won't work. That includes the password credential itself. Read https://dev.mysql.com/doc/refman/8.0/en/grant.html for more information.

like image 179
Bill Karwin Avatar answered Oct 06 '22 17:10

Bill Karwin