Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It is possible to use environment variables in an Apache .conf file, but what about MySQL, is there a way to use them in the MySQL configuration file (my.ini/my.cfg)?

The closest thing I could find was a MySQL man-page that mentions some variables but does not indicate how to use them or if they are even system variables or internal variables.

In my case, I need a way to specify that the logs should be stored in a subdirectory of the system temp-directory, but cannot figure out how to indicate that. For example:

share|improve this question
This is what config management systems are for. Look into puppet/chef/etc – kormoc Feb 8 '13 at 18:49
@kormoc, that can be quite overkill, especially considering that it would be so simple to fix by adding env-var support like Apache did. – Synetech Feb 8 '13 at 21:07
Storing the InnoDB redo logs (innodb_log_group_home_dir) in a temporary directory -- really? You do know that those are essential, critical files during routine operation as well as crash recovery, right? – Michael - sqlbot Feb 9 '13 at 7:12
@Michael-sqlbot, that was just a (bad) example, it would be in another directory like something under %userappdata%, %programfiles%, etc. (if used at all). Temp is just a common variable on all systems. – Synetech Feb 9 '13 at 15:19
So ... your solution is for the MySQL project to radically alter their config process because it's "overkill" for a SysAdmin to script his environment using specialized DSLs? – jcolebrand Feb 14 '13 at 21:15

You can't do it in the config file, but you can modify the start command to set them that way.


Set LOG_DIR="your log dir" in the console.

Then modify the start script to include the log-error option.

mysqld --log-error=$LOG_DIR/error_log.log
share|improve this answer
Arg! I was afraid of that. It would be horribly inconvenient to have to do that for each and every single variable that needs it: --innodb_log_group_home_dir=… --innodb_log_arch_dir=… --log-bin=… --basedir=… --datadir=… --innodb_data_home_dir=… …. And that’s assuming that every variable even can be specified on the command-line. (Thankfully I have commented all but two of them for now, but if I ever had to uncomment them…) – Synetech Feb 8 '13 at 17:19
I don’t like compromising, so I posted a thread at the MySQL forums to confirm if it is not possible and to request that it be added if not. In the mean-time, I am using a batch-file to run the server with the settings like you suggested. That way, I can start it with a simple command (passing in any extra arguments) which greatly simplifies some aspects of the configuration, although I now have to be wary of unwanted console windows. – Synetech Feb 9 '13 at 5:13

Probably not the most elegant solution but perhaps the static configuration file can be replaced by dynamic content:

You could put a FIFO at the log file path. Before you start MySQL you would have to start a mini daemon which writes to that FIFO in an endless loop. Race conditions should not be a problem here.

The daemon would write two blocks of data to the FIFO, first a dummy line like # environment aware config file following in order to open the FIFO. As soon as this first write returns it would get the PID of the reading process (e.g. via fuser), get the variables which occur in the file from /proc/${PID}/environ, create commands for e.g. sed to replace the references and write the sed output to the FIFO.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.