Search the MySQL manual:

2.4.3 Starting and Stopping MySQL Automatically

The mysql.server and mysqld_safe scripts can be used to start the server automatically at system startup time. mysql.server can also be used to stop the server.

The mysql.server script can be used to start or stop the server by invoking it with start or stop arguments:

shell> mysql.server start
shell> mysql.server stop

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. Note that if you use the Linux RPM package (MySQL-server-VERSION.rpm), the mysql.server script has already been installed as `/etc/init.d/mysql' - you don't have to install it manually. See section 2.1.2 Installing MySQL on Linux for more information on the Linux RPM packages.

Before mysql.server starts the server, it changes the directory to the MySQL installation directory, then invokes mysqld_safe. 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 mysqld_safe. If you want the server to run as some specific user, add an appropriate user line to the `/etc/my.cnf' file, as shown later in this section.

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

You need to add these start and stop commands to the appropriate places in your `/etc/rc*' files when you want to start up MySQL automatically on your server.

On most current Linux distributions, it is sufficient to copy the file mysql.server into the `/etc/init.d' directory (or `/etc/rc.d/init.d' on older Red Hat systems). Afterwards, run the following command to enable the startup of MySQL on system bootup:

shell> chkconfig --add mysql.server

On FreeBSD startup scripts generally should go in `/usr/local/etc/rc.d/'. The rc(8) manual page also states that scripts in this directory are only executed, if their basename matches the shell globbing pattern *.sh. Any other files or directories present within the directory are silently ignored. In other words, on FreeBSD you should install the file `mysql.server' as `/usr/local/etc/rc.d/mysql.server.sh' to enable automatic startup.

As an alternative to the above, some operating systems also use `/etc/rc.local' or `/etc/init.d/boot.local' to start additional services on bootup. To start up MySQL using this method, you could append something like the following to it:

/bin/sh -c 'cd /usr/local/mysql ; ./bin/mysqld_safe --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: datadir, basedir, and pid-file.

The following table shows which option groups each startup script reads from option files:

Script Option groups
mysqld [mysqld] and [server]
mysql.server [mysql.server], [mysqld], and [server]
mysqld_safe [mysqld], [server], and [mysqld_safe]

For backward compatibility, mysql.server also reads the [mysql_server] group and mysqld_safe also reads the [safe_mysqld] group. However, you should update your option files to use the [mysql.server] and [mysqld_safe] groups instead.

See section 4.1.2 `my.cnf' Option Files.

User Comments

Posted by [name withheld] on Tuesday July 29 2003, @4:21pm[Delete] [Edit]

Remember that on FreeBSD or any *nix system that supports running scripts from /usr/local/etc/rc.d
you should do for security purposes:
#chmod 100 *

Add your own comment.