24.3. Starting and Stopping MySQL Server on Unix


MySQL runs on many Unix and Unix-like systems, including those based on BSD Unix, System V Unix, and Linux. This section describes general procedures for running MySQL on them. The topics include the prerequisites to check prior to running the server, and arranging for server startup and shutdown.

On Unix, it's best to use a dedicated login user and group for administering and running MySQL Server so that it can be run with permissions other than those of the root login. In this study guide, it's assumed that the user and group names both are mysql.

24.3.1. Server Startup Prerequisites on Unix

Before attempting to run MySQL Server on Unix, you should make sure that everything has been installed and that there is a login account to use for administering and running the server. You should also initialize the data directory if necessary.

On Unix, precompiled MySQL distributions come in the form of RPM files or as compressed tar files. RPMs are used on Linux systems. tar files are available for many platforms.

RPM installation for MySQL typically requires more than one RPM file because the distribution is split up into different RPMs. The most important RPM files are for the server and for the client programs, so at a minimum, you normally install both of them. If you want to run a Max version of the server, you'll also need a Max server RPM, which must be installed after the regular server RPM.

The installation process for RPM files sets up a login account that has user and group names of mysql to use for administering and running the server. It also installs all the files, initializes the data directory and the mysql database that contains the initial MySQL accounts, registers a startup script named mysql in the /etc/init.d directory, and starts the server. The server is installed in /usr/sbin and the data directory is created at /var/lib/mysql.

A tar file distribution is installed simply by unpacking it. For example, if you have a distribution named mysql-max-5.0.10-beta-sun-solaris2.9-sparc.tar.gz on Solaris, you can unpack it in /usr/local to create a subdirectory named mysql-max-5.0.10-beta-sun-solaris2.9-sparc. It's common to create a symbolic link named mysql in /usr/local that points to the installation directory so that it can be referred to more easily as /usr/local/mysql. This study guide assumes the latter pathname of the installation directory for MySQL as installed from a tar file on Unix.

One advantage of setting up a symbolic link (besides that it's shorter than the name created by the tar file) is that when you upgrade to a newer version of MySQL, you can easily retarget the link to the new installation directory. Just delete the link and re-create it to point to the new directory.

With tar file distributions, there is no automatic creation of a login account for running the server, and the data directory is not set up. You must create the login account and initialize the data directory yourself.

The commands to set up a login account for administering and running the server vary for different versions of Unix. For purposes of this guide, it's assumed that you create an account that has user and group names of mysql.

To initialize the data directory, change location into the installation directory and run the mysql_install_db script. For a tar file distribution, this script normally is located in the scripts directory, so you run it like this:

 shell> cd /usr/local/mysql shell> scripts/mysql_install_db 

To make sure that all directories and files that mysql_install_db creates have the proper ownership, run the script as just described while logged in as the mysql user. Alternatively, you can run it as root with the --user=mysql option:

 shell> cd /usr/local/mysql shell> scripts/mysql_install_db --user=mysql 

mysql_install_db creates the data directory and initializes the mysql and test databases. If you do not run this script, the server will complain when you run it later that it cannot find files in the mysql database. For example, the server will issue an error message such as Can't find file: ./host.frm.

With a tar file distribution, you'll also need to install a startup script. That is covered in Section 24.3.2, "Choosing a Server Startup Method on Unix."

Regardless of how you install MySQL on Unix (whether from RPM files or a tar file), the initial MySQL accounts have no passwords. After the server has been started, you should set up passwords as described in Section 35.5.1, "Securing the Initial MySQL Accounts."

24.3.2. Choosing a Server Startup Method on Unix

The server can be started on Unix using any of several different methods:

  • You can invoke mysqld manually. This is usually not done except for debugging purposes. If you invoke the server this way, error messages go to the terminal by default rather than to the error log.

  • mysqld_safe is a shell script that invokes mysqld. The script sets up the error log, and then launches mysqld and monitors it. If mysqld terminates abnormally, mysqld_safe restarts it.

  • mysql.server is a shell script that invokes mysqld_safe. It's used as a wrapper around mysqld_safe for systems such as Linux and Solaris that use System V run-level directories. Typically, this script is renamed to mysql when it is installed in a run-level directory.

  • mysqld_multi is a Perl script intended to make it easier to manage multiple servers on a single host. It can start or stop servers, or report on whether servers are running. Use of multiple servers is discussed further in Chapter 42, "Scaling MySQL."

To have the server run automatically at system startup time, a startup script that's appropriate for your system must be installed:

  • On BSD-style Unix systems, it's most common to invoke mysqld_safe from one of the system startup scripts, such as the rc.local script in the /etc directory.

  • Linux and System V Unix variants that have run-level directories under /etc/init.d use the mysql.server script. If you install the server RPM on Linux, the installation command automatically installs mysql.server under the name mysql for the appropriate run levels. It can be invoked manually with an argument of start or stop to start or stop the server:

     shell> /etc/init.d/mysql start shell> /etc/init.d/mysql stop 

    The operating system startup and shutdown procedures issue those commands automatically.

If the server does not start properly, look in the error log. The default error log name on Unix is host_name.err in the data directory, where host_name is the name of your server host.

To stop the server manually, use one of the following techniques:

  • The mysqladmin program has a shutdown command. It connects to the server as a client and can shut down local or remote servers.

  • The mysql.server script can shut down the local server when invoked with an argument of stop.

  • The mysqld_multi script has a stop command and can shut down any of the servers that it manages. It does so by invoking mysqladmin.

mysqld_safe has no server shutdown capability. You can use mysqladmin shutdown instead. Note that if you forcibly terminate mysqld by using the kill -9 command to send it a signal, mysqld_safe will detect that mysqld terminated abnormally and will restart it. You can work around this by killing mysqld_safe first and then mysqld, but it's better to use mysqladmin shutdown, which initiates a normal (clean) server shutdown.

The mysqld_multi script can be used to manage multiple servers. However, MySQL AB currently is developing another program called MySQL Instance Manager to be used for multiple-server management. This program will offer some significant improvements over mysqld_multi and eventually will replace it:

  • mysqld_multi can stop local or remote servers, but can start only local servers. With MySQL Instance Manager, it will be possible to start remote servers as well.

  • mysqld_multi runs on Unix systems. MySQL Instance Manager will offer cross-platform compatibility and run on Windows or Unix.

  • mysqld_multi requires the Perl DBI module to be installed. MySQL Instance Manager is a standalone binary executable.



MySQL 5 Certification Study Guide
MySQL 5.0 Certification Study Guide
ISBN: 0672328127
EAN: 2147483647
Year: 2006
Pages: 312

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net