Chapter 49. Running Multiple MySQL Servers on the Same Machine

Table of Contents

Running Multiple Servers on Windows
Starting Multiple Windows Servers at the Command Line
Starting Multiple Windows Servers as Services
Running Multiple Servers on Unix
Using Client Programs in a Multiple-Server Environment

In some cases you might want to run multiple mysqld servers on the same machine. You might want to test a new MySQL release while leaving your existing production setup undisturbed. Or you may want to give different users access to different mysqld servers that they manage themselves. (For example, you might be an Internet service provider that wants to provide independent MySQL installations for different customers.)

To run multiple servers on a single machine, each server must have unique values for several operating parameters. These can be set on the command line or in option files. See Program Options.

At least the following options must be different for each server:

--port controls the port number for TCP/IP connections. --socket controls the socket file path on Unix and the name of the named pipe on Windows. (It's necessary to specify distinct pipe names on Windows only for those servers that support named pipe connections.) --shared-memory-base-name designates the shared memory name used by a Windows server to allow clients to connect via shared memory. --pid-file indicates the name of the file in which a Unix server writes its process ID.

If you use the following options, they must be different for each server:

If you want more performance, you can also specify the following options differently for each server, to spread load between several physical disks:

Having different temporary directories like above is also recommended because it will be easier for you in case you want to know to which MySQL server a certain temporary file belongs.

Generally, each server should also use a different data directory, which is specified using the --datadir=path option.

Warning: Normally you should never have two servers that update data in the same databases! This may lead to unpleasant surprises if your operating system doesn't support fault-free system locking! If (despite this warning) you run multiple servers using the same data directory and they have logging enabled, you must use the appropriate options to specify log file names that are unique to each server. Otherwise, the servers will try to log to the same files.

This warning against sharing a data directory among servers also applies in an NFS environment. Allowing multiple MySQL servers to access a common data directory over NFS is a bad idea!

Make it easy for yourself: Forget about sharing a data directory among servers over NFS. A better solution is to have one computer that contains several CPUs and use an operating system that handles threads efficiently.

If you have multiple MySQL installations in different locations, normally you can specify the base installation directory for each server with the --basedir=path option to cause each server to use a different data directory, log files, and PID file. (The defaults for all these values are determined relative to the base directory.) In that case, the only other options you need to specify are the --socket and --port options. For example, suppose you install different versions of MySQL using .tar file binary distributions. These will install in different locations, so you can start the server for each installation using the command ./bin/mysqld_safe under its corresponding base directory. mysqld_safe will determine the proper --basedir option to pass to mysqld, and you need specify only the --socket and --port options to mysqld_safe.

As discussed in the following sections, it is possible to start additional servers by setting environment variables or by specifying appropriate command-line options. However, if you need to run multiple servers on a more permanent basis, it will be more convenient to use option files to specify for each server those option values that must be unique to it.

Running Multiple Servers on Windows

You can run multiple servers on Windows by starting them manually from the command line, each with appropriate operating parameters. On Windows NT-based systems, you also have the option of installing several servers as Windows services and running them that way. General instructions for running MySQL servers from the command line or as services are given in Windows installation. This section describes how to make sure you start each server with different values for those startup options that must be unique per server, such as the data directory. (These options are described in Multiple servers.)

Starting Multiple Windows Servers at the Command Line

To start multiple servers manually from the command line, you can specify the appropriate options on the command line or in an option file. It's more convenient to place the options in an option file, but it's necessary to make sure that each server gets its own set of options. To do this, create an option file for each server and tell the server the filename with a --defaults-file option when you run it.

Suppose you want to run mysqld on port 3307 with a data directory of C:\mydata1, and mysqld-max on port 3308 with a data directory of C:\mydata2. To accomplish this, create two option files. For example, create one file named C:\my-opts1.cnf that looks like this:

[mysqld]
datadir = C:/mydata1
port = 3307

Create a second file named C:\my-opts2.cnf that looks like this:

[mysqld]
datadir = C:/mydata2
port = 3308

Then start each server with its own option file:

shell> mysqld --defaults-file=C:\my-opts1.cnf
shell> mysqld-max --defaults-file=C:\my-opts2.cnf

(On NT, the servers will start in the foreground, so you'll need to issue those two commands in separate console windows.)

To shut down the servers, you must connect to the appropriate port number:

shell> mysqladmin --port=3307 shutdown
shell> mysqladmin --port=3308 shutdown

Servers configured as just described will allow clients to connect over TCP/IP. If you also want to allow named pipe connections, use the mysqld-nt or mysqld-max-nt servers and specify options that enable the named pipe and specify its name. (Each server that supports named pipe connections must use a unique pipe name.) For example, the C:\my-opts1.cnf file might be written like this:

[mysqld]
datadir = C:/mydata1
port = 3307
enable-named-pipe
socket = mypipe1

Then start the server this way:

shell> mysqld-nt --defaults-file=C:\my-opts1.cnf

C:\my-opts2.cnf would be modified similarly for use by the second server.

Starting Multiple Windows Servers as Services

On NT-based systems, a MySQL server can be run as a Windows service. The procedures for installing, controlling, and removing a single MySQL service are described in NT start.

As of MySQL 4.0.2, you can install multiple servers as services. In this case, you must make sure that each server uses a different service name in addition to all the other parameters that must be unique per server.

For the following instructions, assume that you want to run the mysqld-nt server from two different versions of MySQL that are installed at C:\mysql-4.0.8 and C:\mysql-4.0.17, respectively. (This might be the case if you're running 4.0.8 as your production server, but want to test 4.0.17 before upgrading to it.)

The following principles are relevant when installing a MySQL service with the --install option:

  • If you specify no service name, the server uses the default service name of MySQL and the server reads options from the [mysqld] group in the standard option files.

  • If you specify a service name after the --install option, the server ignores the [mysqld] option group and instead reads options from the group that has the same name as the service. The server reads options from the standard option files.

  • If you specify a --defaults-file option after the service name, the server ignores the standard option files and reads options only from the [mysqld] group of the named file.

These principles also apply if you install a server using the --install-manual option.

Based on the preceding information, you have several ways to set up multiple services. The following instructions describe some examples. Before trying any of them, be sure you shut down and remove any existing MySQL services first.

  • Specify the options for all services in one of the standard option files. To do this, use a different service name for each server. Suppose you want to run the 4.0.8 mysqld-nt using the service name of mysqld1 and the 4.0.17 mysqld-nt using the service name mysqld2. In this case, you can use the [mysqld1] group for 4.0.8 and the [mysqld2] group for 4.0.17. For example, you can set up C:\my.cnf like this:

      # options for mysqld1 service
      [mysqld1]
      basedir = C:/mysql-4.0.8
      port = 3307
      enable-named-pipe
      socket = mypipe1
      # options for mysqld2 service
      [mysqld2]
      basedir = C:/mysql-4.0.17
      port = 3308
      enable-named-pipe
      socket = mypipe2
      

    Install the services as follows, using the full server pathnames to ensure that Windows registers the correct executable program for each service:

      shell> C:\mysql-4.0.8\bin\mysqld-nt --install mysqld1
      shell> C:\mysql-4.0.17\bin\mysqld-nt --install mysqld2
      

    To start the services, use the services manager, or use NET START with the appropriate service names:

      shell> NET START mysqld1
      shell> NET START mysqld2
      

    To stop the services, use the services manager, or use NET STOP with the appropriate service names:

      shell> NET STOP mysqld1
      shell> NET STOP mysqld2
      

    Note: Before MySQL 4.0.17, only a server installed using the default service name (MySQL) or one installed explicitly with a service name of mysqld will read the [mysqld] group in the standard option files. As of 4.0.17, all servers read the [mysqld group if they read the standard option files, even if they are installed using another service name. This allows you to use the [mysqld] group for options that should be used by all MySQL services, and an option group named after each service for use by the server installed with that service name.

  • Specify options for each server in separate files and use --defaults-file when you install the services to tell each server what file to use. In this case, each file should list options using a [mysqld] group.

    With this approach, to specify options for the 4.0.8 mysqld-nt, create a file C:\my-opts1.cnf that looks like this:

      [mysqld]
      basedir = C:/mysql-4.0.8
      port = 3307
      enable-named-pipe
      socket = mypipe1
      

    For the 4.0.17 mysqld-nt, create a file C:\my-opts2.cnf that looks like this:

      [mysqld]
      basedir = C:/mysql-4.0.17
      port = 3308
      enable-named-pipe
      socket = mypipe2
      

    Install the services as follows (enter each command on a single line):

      shell> C:\mysql-4.0.8\bin\mysqld-nt --install mysqld1
                 --defaults-file=C:\my-opts1.cnf
      shell> C:\mysql-4.0.17\bin\mysqld-nt --install mysqld2
                 --defaults-file=C:\my-opts2.cnf
      

    To use a --defaults-file option when you install a MySQL server as a service, you must precede the option with the service name.

    After installing the services, start and stop them the same way as in the preceding example.

To remove multiple services, use mysqld --remove for each one, specifying a service name following the --remove option if the service to remove has a name different than the default.