5

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:

log-error="${temp}/MySQL_Logs/error.log"
innodb_log_group_home_dir="%temp%/PS_Logs/Logs/MySQL/"
?
Synetech
  • 948
  • 1
  • 12
  • 27
  • 1
    This is what config management systems are for. Look into puppet/chef/etc – R. S. Feb 08 '13 at 18:49
  • 1
    @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 08 '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 09 '13 at 07: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 09 '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
  • @jcolebrand, I have no clue what you are talking about. Apache already lets you use system environment variables in the config file, how would adding a (potentially single) line to the config-file-parsing function to resolve environment variables “radically alter their config process”? I don’t know what you mean about DSLs, but yes, adding every each variable to the command-line is unnecessary and defeats the point to using a configuration file. – Synetech Feb 14 '13 at 22:44
  • From your comments. Look, here's the thing: The Apache config uses ENV vars _because the Apache team wrote that into their config system_. That's something _they programatically wrote_. Config files are by their very nature _plain text_. So for MySQL to do the same, they would have to do what the Apache team did. – jcolebrand Feb 14 '13 at 22:54
  • @jcolebrand, you’re not a programmer are you? Yes a configuration file is plain-text, but the function that processes the file is code and can easily have a call to [`ExpandEnvironmentStrings`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724265.aspx) (assuming it doesn’t already). I learned that ages ago when I wanted to allow my programs to let me enter variables instead of hard-coded paths to files and such. Like I said, the manual mentions environment variables, so there is already some sort variable-processing routine in the conf-parser. – Synetech Feb 14 '13 at 22:58
  • @Synetech how did you come to the conclusion from the linked page that it MySQL uses those variables while parsing conf files? – dezso Feb 14 '13 at 23:31
  • 1
    @dezso, where else would users make use of environment-variables that they would bother putting it in the manual? Besides, the very second paragraphs says: `Note that any options on the command line take precedence over values specified in option files and environment variables, and values in option files take precedence over values in environment variables.` – Synetech Feb 17 '13 at 00:54

2 Answers2

2

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

eg:

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
cduffin
  • 854
  • 7
  • 8
  • 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 08 '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 09 '13 at 05:13
0

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.

Hauke Laging
  • 5,285
  • 2
  • 24
  • 40