Start MySQL at startup (revisited)

Dec 19, '03 11:53:00AM

Contributed by: Anonymous

I have been playing with MySQL and it was getting to the point where I needed it on all the time, so now I have it start whenever I power on or boot my system. The control script is nice as well, allowing stop, start, and restart options. First you must install MySQL. I used Fink to do so; the actual installation process for MySQL is beyond the scope of this hint.

Read the rest of the article for the how-to...

[robg adds: There's an earlier hint on starting MySQL at startup. This version, however, has more features and more detail, so I figured it was worth sharing.]

First you need to figure out the defaults for MySQL to start up. This is a copy of my my.cnf file that must be placed in /sw/var/mysql.

-rw-r--r--    1 mysql    mysql         281 Dec 12 11:54 /sw/var/mysql/my.cnf
This is what I have in this file:
[mysqld]
datadir=/sw/var/mysql
# None of the commands like the socket here so use next line
socket=/sw/var/mysql/mysql.sock
socket=/tmp/mysql.sock
     
[mysql.server]
user=mysql
basedir=/sw/var/

[safe_mysqld]
err-log=/sw/var/log/mysqld.log
pid-file=/sw/var/run/mysqld/mysqld.pid
Next we need to prepair the directories for MySQL:
% mkdir -p /sw/var/run/mysqld
% touch /sw/var/run/mysqld/mysqld.pid
Now the fun part. And please remember to be careful. The area we are playing with is responsible for your system starting up and such.
% cd /System/Library/StartupItems/
Now copy one of the directories over. I used PrintingServices. Also in the next section please make sure you are in the right directory before you delete. Read on; you will know what I mean.
% cp -r PrintingServices mysql
% cd mysql
% pwd
/System/Library/StartupItems/mysql
% rm PrintingServices
Now edit your StartupParameters.plist; it should look like this:
{
  Description     = "MySQL";
  Provides        = ("mysql");
  Requires        = ("Network");
  Uses            = ("Network Time");
  OrderPreference = "Late";
}
Now make your startup script. Now we need to make the startup file. To do this, edit mysql within the /System/Library/StartupItems/mysql directory and put this inside:
#!/bin/sh

##
# MySQL
##

. /etc/rc.common

datadir="/sw/var/mysql"

StartService ()
{
  if [ "${MYSQL:=-YES-}" = "-YES-" ]; then
    ConsoleMessage "Starting MYSQL database services"
    touch /sw/var/log/mysqld.log
    chown mysql:mysql /sw/var/log/mysqld.log
    if [ ! -d $datadir/mysql ] ; then
      /sw/bin/mysql_install_db > /dev/null 2>&1
    fi
    chown -R mysql:mysql $datadir
    chmod 0755 $datadir
    /sw/bin/mysqld_safe --defaults-file=/sw/var/mysql/my.cnf > /dev/null 2>&1 & 
        fi
}

StopService ()
{
  # Stop mysql
  ConsoleMessage "Stopping MYSQL database services"
  /bin/kill `cat /sw/var/run/mysqld/mysqld.pid 2> /dev/null ` > /dev/null 2>&1
}

RestartService ()
{
  # Restart mysql
  StopService
  ConsoleMessage "Restarting MYSQL database services: this will take 5 seconds for the database to catch up."
  sleep 5 
  StartService
}

RunService "$1"
Now executing this with the start, stop and restart commands will do the obvious. Before that can happen though you must make it executable.
% chmod +x mysql
Now make an entry in the /etc/hostconfig file to tell mysql to start up on system boot/startup. Mine looks like this:
...snip...
MYSQL=-YES-
And there you go.

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031212142956308