- How can I start and shut down the MySQL server on a Unix system?
There are a few ways in which the MySQL server may be started. To start the daemon manually on any operating system in which the MySQL server is installed, entering mysqld from the command-line will work. To start it manually on a Unix system, something like the following may be entered from the command-line:
mysqld -user=mysql &
In this line, the file system user mysql is used. It's poor security to run the mysqld daemon as root. The ampersand at the end of the line instructs the shell to send the process to the background.
One problem with starting the MySQL server daemon directly is that if it crashes, you may not be aware of it until a user complains. To be assured of the daemon being restarted automatically, use the mysqld_safe script.
The user is assumed to be mysql with this script. So you don't need to specify the user. This script will run in the background, per the ampersand. In turn, it will start a few instances of mysqld to listen for client requests on TCP/IP port 3306 for access to the MySQL server. The users do not interact with mysqld_safe. If the MySQL server crashes, mysqld_safe will restart mysqld. Incidentally, on older versions of MySQL, mysqld_safe was named safe_mysqld.
Another method for starting the server is to use the mysql.server script. It can be found in the directory where MySQL is installed. On a Unix system, this will typically be located in the /usr/share/mysql directory. To use this script, the parameter of start, stop, or restart may be given like so:
If MySQL is not already set to start automatically when the system starts, this file could be copied to the init.d sub-directory in the /etc directory. This directory contains scripts for daemons that are to be initialized at start up. To copy the mysql.server script to the initial directory, something like the following may be entered at the command-line:
cp /usr/share/mysql/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql
It's common to rename the server file to just mysql when in the init.d sub-directory. This same file will be used to shut down the MySQL server when the system is shut down. On some Linux systems, the init.d sub-directory is located in /etc/rc.d. In which case, the above cp line will need to be adjusted. The second line above adds execute privileges to the file for all users.
On FreeBSD sytems, the /usr/local/etc/rc.d directory is used for initial daemon scripts. There isn't an init.d sub-directory. In some versions of Linux, the service command may be used to start or to stop the MySQL service. This would be entered like so:
service mysql start
This will actually start the mysqld_safe script. Using the stop parameter will stop both the mysqld daemon and the mysqld_safe script.
If your server uses multiple servers, then they can be started with the mysqld_multi script. So that the --user option won't have to be entered each time from the command-line, the following line can be added to the MySQL configuration file (i.e., /etc/my.cnf):
This simple line could go in the [mysqld] group within the configuration file. If you're using mysqld_safe to start the server, it could go under the [mysqld_safe] group.