Bug #1276 MYSQL_UNIX_PORT ignored by mysqld
Submitted: 14 Sep 2003 3:29 Modified: 27 Sep 2003 9:48
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:3.23.56, 3.23.57 OS:Linux (linux)
Assigned to: CPU Architecture:Any

[14 Sep 2003 3:29] [ name withheld ]
Description:
The MYSQL_UNIX_PORT environment variable is ignored by mysqld if there is a default socket specified at compile time (as in support-files/mysql-3.23.57.spec). This seems to conflict with documentation at http://mysql.us.themoes.org/doc/M/u/Multiple_servers.html, for example, since safe_mysqld handles --socket by setting the MYSQL_UNIX_PORT environment variable.

How to repeat:
Make sure that there are no other instances of mysqld running.

Set the environment variable MYSQL_UNIX_PORT to a writeable filename + full path (e.g. /tmp/mysqltest.sock).

Check that /var/lib/mysql/mysql.sock does not exist.

Run safe_mysqld (or mysqld).

/var/lib/mysql/mysql.sock should now be present while the filename specified in MYSQL_UNIX_PORT will not exist.

Suggested fix:
I have a copy of the 3.23.57 MySQL source code. In my copy, mysqld.cc contains a call to load_defaults on line 1720 (in main) near the beginning of the main function. This call adds "--datadir=/var/lib/mysql" and "--socket=/var/lib/mysql/mysql.sock" to the set of command line arguments (the variable argv).

These arguments are then passed to the get_options (line 1733 in main) function which parses them. Since the option "--socket=/var/lib/mysql/mysql.sock" appears in argv, the string mysql_unix_port is set to /var/lib/mysql/mysql.sock on line 3639.

server_init is eventually called in main (line 1845) and it calls set_ports (line 915). set_ports checks whether mysql_unix_port has been defined already (line 819). Since it will always have been set (given the above), the code:

    if ((env = getenv("MYSQL_UNIX_PORT")))
      mysql_unix_port = env;			/* purecov: inspected */

will never be executed. That seems to be the place where mysql_unix_port should be set to the value of the environment variable.

The code needs to be rearranged to allow the environment variable MYSQL_UNIX_PORT to override the compiled in default while not overwriting potentially higher priority methods of specifying the unix socket. I do not know enough about the implementation or your policies to give an exact fix.
[15 Sep 2003 12:25] [ name withheld ]
The additional arguments load_defaults places in argv actually seem to come from the [mysqld] section of /etc/my.cnf (as opposed to being compiled in as I originally wrote).
[18 Sep 2003 7:01] Sergei Golubchik
I can see no bug here. /etc/my.cnf is supposed to override settings from environment variables
[18 Sep 2003 19:54] [ name withheld ]
Then problem is with safe_mysqld. safe_mysqld sets the MYSQL_UNIX_PORT variable from the value given for --socket on the command line. But:

(from http://www.mysql.com/doc/en/Environment_variables.html)
Note that any options on the command-line will take precedence over values specified in configuration files and environment variables, and values in configuration files take precedence over values in environment variables.

So, the command line option should take precedence, but it's internally turned into an environment variable which doesn't take precedence.
[18 Sep 2003 19:59] [ name withheld ]
Personally, I think environment variables should take precedence anyway. If you're playing with a client and server, and you want to quickly try something else, it's very easy to set an environment variable. No file editing, it can be automated very easily etc.

Specifically, ideas like the "quick and dirty" method from:

http://www.mysql.com/doc/en/Multiple_Unix_servers.html

are made less useful...

Environment variables are nice for quick experimentation... files for real configuration.
[27 Sep 2003 9:48] Sergei Golubchik
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

as this is a minor issue, I fixed it in 4.0.16