Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Start MySQL at startup (revisited) UNIX
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:
# None of the commands like the socket here so use next line

Next we need to prepair the directories for MySQL:
% mkdir -p /sw/var/run/mysqld
% touch /sw/var/run/mysqld/
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
% 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:


. /etc/rc.common


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
    chown -R mysql:mysql $datadir
    chmod 0755 $datadir
    /sw/bin/mysqld_safe --defaults-file=/sw/var/mysql/my.cnf > /dev/null 2>&1 & 

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

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

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:
And there you go.
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)

Start MySQL at startup (revisited) | 10 comments | Create New Account
Click here to return to the 'Start MySQL at startup (revisited)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Start MySQL at startup (revisited)
Authored by: feelgood on Dec 19, '03 12:51:34PM
Wow, this seems like serious overkill. What is wrong with downloading the OS X package from MySQL to install? It even comes with an extra package that will take care of all of the set up if you want MySQL to start automatically.

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: LFransson on Dec 19, '03 02:01:09PM

And if you're a fan of fink and install MySQL using fink (like the hint writer did), you can also install daemonic from fink (if it's not installed already), and at the command line enter 'daemonic enable mysql'. The startup item will be created for you. It won't have all the features of the one in this hint, but if you're rather averse to using the command line any more than you have to, daemonic is an easier way to go.

Larry Fransson
Seattle, WA

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: dansroka on Dec 19, '03 01:30:02PM

I installed MySQL using the excellent package made by the Server Logistics folks - it comes with a handy System PrefPane that lets you launch at startup, restart, and reset your root password.

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: gvaughn on Dec 19, '03 03:02:37PM

I'll second this. Server Logistics' 'Complete' line are the most Mac-like (read: easiest) way to install. In addition to MySQL they also have installers for Apache 2, PHP, and Jakarta Tomcat.

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: vanthal on Dec 24, '03 05:34:31PM

Seconded again - the ServerLogistics packages are excellently put together, very well documented and easy to install and remove. The preference panes they come with are very convenient. I use them at work and at home, but my only worry is that my brain will grow fat and lazy as a result, so that if they decide to stop distributing their packages I'll have to relearn the fine art of compiling Apahce and PHP. As it stands, though, I'm very grateful for their work, which IMO combines the best aspects of Open Source software and the Apple philosophy...

It's good to keep your hand in with the beneath-the-surface stuff, though, so hints and discussions like this are always important.

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: rofl on Dec 19, '03 03:20:11PM

okay i got my packages at
great stuff...

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: macubergeek on Dec 20, '03 09:18:42AM

Your Link for the earlier hint is pointed to localhost.

http://localhost/gl137files article.php story=20010411170957604

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: Erik Toh on Dec 20, '03 09:44:18AM

There's a problem with this hint; /System/Library/StartupItems is reserved for Apple-default services and messing with it is just asking for trouble. If you must do so, install your services in /Library/StartupItems.

Since MySQL is installed by Fink, why not just use daemonic? Installing it as a startup service is as simple as typing the following command:

daemonic install MySQL

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: brooks_lt on Dec 20, '03 11:12:14PM

Ok, now how does this work on MacOS X Server 10.3, where MySQL is installed by default, already? I imagine the basic idea is the same, but the actual scripts, etc., are different. Any help would be much appreciated.

If everyone used macs, we'd be working on how to get to Alpha Centauri rather than how to get to Mars.

[ Reply to This | # ]
Start MySQL at startup (revisited)
Authored by: viegelinsch on Mar 23, '04 04:26:15PM
Hi, a very cool solution, but there is one step missing after preparing the directories for the pid. The owner has to be changed. I would propose
chown -R mysql:mysql /sw/var/run/mysqld/
Hack on!
yogi viegelinsch

[ Reply to This | # ]