MySQL DBA Tips Corner

     Return to the MySQL DBA Tips Corner.


Starting and Stopping MySQL Automatically

by Jeff Hunter, Sr. Database Administrator

There are two options for starting and stopping the the MySQL Database Server:

Either of these two options (scripts) can be used to start and stop the server automatically at system startup/shutdown.

mysql.server can be found in the share/mysql directory under the MySQL installation directory or in the support-files directory of the MySQL source tree.

Before mysql.server starts the server, it changes the directory to the MySQL installation directory, then invokes safe_mysqld. You might need to edit mysql.server if you have a binary distribution that you've installed in a non-standard location. Modify it to cd into the proper directory before it runs safe_mysqld. If you want the server to run as some specific user, add an appropriate user line to the /etc/my.cnf file, as show later in this article.

mysql.server stop brings down the server by sending a signal to it. You can take down the server manually by executing mysqladmin shutdown.

You might want to add these start and stop commands to the appropriate places in your /etc/rc* files when you start using MySQL for production applications. Note that if you modify mysql.server, and then upgrade MySQL, your modified version will be overwritten, so you should make a copy of your edited version that you can reinstall.

If your system uses /etc/rc.local to start external scripts, you should append the following to it:

  /bin/sh -c "cd /usr/local/mysql ; ./bin/safe_mysqld --user=mysql &"

You can also add options for mysql.server in a global /etc/my.cnf file. A typical /etc/my.cnf file might look like this:

  [mysqld]
  datadir=/usr/local/mysql/var
  socket=/var/tmp/mysql.sock
  port=3306
  user=mysql

  [mysql.server]
  basedir=/usr/local/mysql

The mysql.server script understands the following options:

The following table shows which option groups each of the startup scripts read from option files:

Script Option Groups
mysqld mysqld and server
mysql.server mysql.server, mysqld and server
safe_mysqld mysql.server, mysqld and server

/etc/init.d/mysql
  
# +------------------------------------------------------------------------+
# | FILE         : /etc/init.d/mysql                                       |
# +------------------------------------------------------------------------+

# +---------------------------+
# | START/STOP CASE STATEMENT |
# +---------------------------+
case "$1" in

start)
        echo "+---------------------------------------------------+"
        echo "| About to start MySQL Database Server              |"
        echo "+---------------------------------------------------+"

        sh -c "cd /usr/local/mysql ; ./bin/safe_mysqld --user=mysql &"

        ;;

stop)
        echo "+---------------------------------------------------+"
        echo "| About to stop MySQL Database Server               |"
        echo "+---------------------------------------------------+"

        sh -c "cd /usr/local/mysql ; ./bin/mysqladmin -u root shutdown"

        ;;

*)
        echo $"Usage: $prog {start|stop}"
        exit 1

esac

echo "+---------------+"
echo "| ENDING SCRIPT |"
echo "+---------------+"

exit


Copyright (c) 1998-2017 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Monday, 25-Jul-2005 20:27:40 EDT
Page Count: 11063