Give .my.cnf to mysql command line - mysql

How to give a specific .my.cnf config file to the mysql command line?
$ mysql --my-config=.my.cnf

The --defaults-file option lets you specify which options file you want to use. It would be a good idea to give it the full path to your options file.
mysql --defaults-file=/home/user/.my.cnf database

In here: http://dev.mysql.com/doc/refman/5.5/en/option-files.html it says you can also specify "defaults-extra-file" for additional params. Otherwise it defaults to the various my.cnf (depending on OS).

Related

Where does the my.cnf resides on macOS High Sierra?

I wanted to disable some options in the MySql server strict mode, but for some reason I'm not even able to find its configuration file (my.cnf)
Currently installed MySql:
Ver 14.14 Distrib 5.7.20, for macos10.12 (x86_64)
Output generated by
mysql --help
Default options are read from the following files in the given order:
/etc/my.cnf
/etc/mysql/my.cnf
/usr/local/mysql/etc/my.cnf
~/.my.cnf
But there is no such file as my.conf
I had done some research work on SO but none of the advises has worked for me.
What I'm supposed to do?
There are defaults built into MySQL. The configuration file(s), if any, override the defaults.
Note that the config files are not looked at except during startup. So, editing or creating such a file has no effect until you restart mysqld.
If you mess up the syntax, mysqld will not start. Then you need to find the error, either during startup, or in a log file. (Or you could ask here "what is my syntax error".)
As for the location, and name, of the config files that will be used:
$ mysql --help
gives you the list for mysql, but perhaps you need it for the server, so try
$ mysqld --help --verbose
gives you long output; maybe 50 lines from the top, you should see something like:
Usage: mysqld [OPTIONS]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
You asked about my.conf; was that a typo?
Regardless, If those files are missing, you can create them, make them readable by mysql, and put things into it preceded by [mysqld] so that the server will see them.
Personal override
Regardless of what is in the defaults and/or config files, you can add to the last file given. In the examples above that is 'hidden' .my.cnf in your home directory. All you need to do is create a few lines:
[mysql]
some_setting = somevalue
[client]
some_setting = somevalue
That helps for the "mysql" commandline tool and/or other clients (maybe).
But if you need to make changes to the server mysqld, it needs to be in one of the other files. Again, the minimum is something like
[mysqld]
some_setting = somevalue
Note the mysqld to refer to the server. And remember to restart the service.
Another note: If you see (in an existing config file), !includedir ..., then go to that directory to find any number of further files. You could add your own file, say z.cnf (so it would be picked last) with the two (or more) lines as indicated above.
If you have installed Mysql 8 or Above using the package installer from mysql website, then follow what i did
Create the config file in your home directory vim ~/.my.cnf
Do open System preferences in mac
Then Select the configuration file you created on the option.
The content in this website says.
I had the urgent need to configure some specific stuff in MySQL 5.7 on my developer machine, a MacBook Pro running Mac OS Sierra. Unfortunately, I did not find a my.cnf file that could be customized anywhere?
Internet research showed that MySQL Database Server on Mac OS runs without a my.cnf config file by default, simply started with default values.
Luckily, it is pretty simple to customize the MySQL Server installation by creating and editing a custom my.cnf file like this:
sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
Then edit / customize the /etc/my.cnf file as required and restart your MySQL Server.
Or you can refer to this answer
You could try:
$ locate mysql | grep my.cnf
It should reveal the location(s) if it exists.

how do I reference location of .my.cnf in bash/mysql script?

I have a bash script that connects to a mysql server and pulls information. I have my .my.cnf file in my home directory. I want to change the location of the .cnf file to ~myname/.my.cnf
My question is how do I reference this new location in my script or in my mysql call to the database?
Thanks
Here's the doc reference for how MySQL looks at Options Files:
https://dev.mysql.com/doc/refman/5.7/en/option-files.html
File Name Purpose
/etc/my.cnf Global options
/etc/mysql/my.cnf Global options
SYSCONFDIR/my.cnf Global options
$MYSQL_HOME/my.cnf Server-specific options (server only)
defaults-extra-file The file specified with --defaults-extra-file, if any
~/.my.cnf User-specific options
~/.mylogin.cnf User-specific login path options (clients only)
Note: you want to use defaults-extra-file option when connecting with mysql client. mysql --defaults-extra-file=~/path/to/file/.filename < script
You should consider using --login-path as an alternative. Login path reference. The file is not plain text. You use mysql_config_editor to create your login paths, which stores username, password, host, etc.
I don't know what you mean by "~myname/.my.cnf". This would expand to a directory made of a concatenation of your username and "myname" which doesn't make any sense. I guess you might mean "~/something/.my.cnf"
The '~' expands to $HOME hence simply redefining $HOME before invoking mysql should suffice (although this will confuse further references to '~'.
HOME=$HOME/myname
mysql <somescript.sql

What if there are more than one my.cnf files on my server?

What if there are more than one my.cnf files on my server?
Which configuration file will be looked by MySQL?
Assuming one runs mysql under linux, there the option-files will be read in that order:
the file specified by option --defaults-file=/PATH/TO/my.cnf while startup of mysqld
/etc/my.cnf
/etc/mysql/my.cnf
SYSCONFDIR/my.cnf (usually sysconfdir is /etc)
$MYSQL_HOME/my.cnf
the path set by --defaults-extra-file=/PATH/TO/my.cnf
~/my.cnf (your homedir, usually used for mysql-client configuration)
Also keep in mind that there are three options which change this behavior in MySQL:
-defaults-file=/PATH/TO/my.cnf - only read that option-file
-defaults-extra-file=/PATH/TO/myextra.cnf - read all global option-files, then this
-no-defaults - ignore every option-file and start with default
Please also refer to the docs on option-files: MySQL-Docs
The one specified on the command line that starts the mysqld process, or if no configuration file is specified, the default location. On Linux it is in /etc. I don't know where the Windows default is, but you can look in the documentation.

Use environment variables in my.ini/my.cnf

Is it possible to read env vars in MySQL option file my.ini/my.cnf, the way it is possible in httpd.conf and php.ini with the ${ENVVAR} syntax :
datadir="${MYSQL_DATA_HOME}/Data/"
If yes, what is the syntax ?
I think it is not possible. I tried making the same changes in MySQL option file, but failed to start. But there is another way though.
Set MYSQL_DATA_HOME=<some dir> in the console. And in the same terminal start mysql not by service start but with this command mysqld --datadir=$MYSQL_DATA_HOME/whatever

Specifying separate config file on mysqldump command line

My main MySQL user's /home//my.cnf file looks like this (Linux system):
[client]
host=localhost
user=<user>
password=********
database=h2o_amr
I want to point mysqldump to this config file, which is in the directory where the mysqldump file will be written:
[client]
host=localhost
user=<user>
password=********
How do I point mysqldump to this config file? I have searched for an answer to this in the man pages and the Internet before posting this question.
There are three options to change the default behavior:
--no-defaults: Don't read default options from any option file.
--defaults-file=#: Only read default options from the given file #.
--defaults-extra-file=#: Read this file after the global files are read.
This information was copied from the integrated manual, i.e.mysqldump --help | grep -A7 'Default options'.
Note: The parameter needs to be in first position if you add others parameters, if not it will not be recognized.
According to the documentation, there is a --defaults-file=PATH option that does exactly what you need. I tried it myself with mysqldump and it appears to have worked.
There are also other ways to have MySQL read option files without using that argument: using the default paths MySQL clients search through to compile the list of options (1st and 2nd table).