Previous Section  < Day Day Up >  Next Section

11.2 Running MySQL 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 selecting an appropriate distribution, installing it, and arranging for server startup and shutdown.

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. This section discusses installation using a precompiled binary distribution. It's also fairly common to install MySQL on Unix by building it from source, a topic discussed in section 11.4, "Compiling MySQL from a Source Distribution."

The assumption here is that you're installing MySQL on a system that doesn't already have it. This might not be true for a given machine: MySQL might already be installed because several operating system distributions now include MySQL by default or allow it to be selected during the operating system installation procedure. However, you might find on such systems that the provided version of MySQL is older than the version currently available from MySQL AB. Section 11.5, "Upgrading MySQL," describes how to upgrade an existing MySQL installation to a newer version.

11.2.1 Installing MySQL Using RPM Files

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.

To install MySQL using RPM files, follow these steps:

  1. Obtain the RPM files. At a minimum, you should get the server RPM and client RPM. 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.

  2. Install the RPM files. You'll need to do this as root. The installation process sets up a login account with user and group names of mysql to use for administering and running the server, installs all the files, initializes the data directory and the mysql database that contains the initial MySQL accounts, registers a startup script, and starts the server.

A typical installation uses the server and client RPM files. If the current version of MySQL happens to be 4.0.15, the installation commands would look like this:






shell> rpm -i MySQL-server-4.0.15-0.i386.rpm

shell> rpm -i MySQL-client-4.0.15-0.i386.rpm


The MySQL accounts at this point have no passwords. Chapter 12 discusses how to set up passwords.

11.2.2 Installing MySQL Using a tar File

To install MySQL from a tar file distribution, use this procedure:

  1. Set up a login account for administering and running the server. The commands to do this vary for different versions of Unix; assume for purposes of this guide that you create an account with user and group names set to mysql. You need to perform this step as root. Most of the following steps can be executed while logged in as the mysql user.

  2. Obtain the tar file, place it in the location under which you want to install MySQL, and unpack it. The top-level MySQL directory created by unpacking the distribution will have a long name because the names of binary distributions include a version number and a platform name indicating the type of system for which the distribution is intended. For this reason, it's a good idea to create a symbolic link to that directory with a shorter name that is easier to refer to.

    The following example shows how to unpack a distribution and create the symbolic link, using a distribution file named mysql-standard-4.0.17-sun-solaris2.9-sparc.tar.gz that unpacks to create a MySQL installation directory named mysql-standard-4.0.17-sun-solaris2.9-sparc:

    
    
    
    

    
    shell> tar zxf mysql-standard-4.0.17-sun-solaris2.9-sparc.tar.gz
    
    shell> ln -s mysql-standard-4.0.17-sun-solaris2.9-sparc mysql
    
    

    Suppose that the directory in which you issue those commands is /usr/local. The resulting installation directory (that is, the MySQL base directory) can be referred to as /usr/local/mysql. 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.

  3. tar file distributions do not create the data directory automatically the way Windows and RPM distributions do. To initialize the data directory, change location into the installation base directory and run the mysql_install_db script. For a tar file distribution, this script normally is located in the scripts directory, so you can 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 shown 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.

  4. Install a startup script. Choosing a script and how to install it are covered in section 11.2.3, "Starting and Stopping the MySQL Server."

The MySQL accounts at this point have no passwords. Chapter 12 discusses how to set up passwords.

The procedure for installing a tar file distribution involves more steps than that for RPM installation. The RPM installation handles many details for you that tar file installation doesn't. For example, it creates the mysql login account, runs the mysql_install_db script, and installs the startup script automatically.

11.2.3 Starting and Stopping the MySQL Server

Under Unix, it's possible to start the server using several different methods:

  • You can invoke mysqld manually. This usually isn't done except for debugging purposes. If you invoke the server this way, error messages go to the terminal 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 the run-level directories.

  • 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 or not servers are running. Use of multiple servers is discussed further in Chapter 16, "Advanced Server Features."

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

  • 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.

    Previous Section  < Day Day Up >  Next Section