Starting and Shutting Down MySQL

MySQL runs on most flavors of operating system, but the procedures differ with each. The following sections are divided into Windows and Unix discussions.

Starting and Shutting Down in Unix

The most basic way to start MySQL is to run mysqld directly. However, most distributions come with a starter script called mysqld_safe (older versions of MySQL called it safe_ mysqld), which you should use instead when starting MySQL manually. This script has some extra safety features, such as logging errors and automatically restarting the server when an error occurs. To start MySQL, log in as root and run the following command from the directory in which MySQL was installed (usually /usr/local/mysql):

 % bin/mysqld_safe  --user=mysql & 

Note that the user is set as mysql. It's important to run MySQL as the mysql user to avoid permission and security problems.

Warning 

If you are unfamiliar with the system—especially if you didn't install it—speak to your systems administrator before trying to start the server in this way. There are multiple ways to start a server, and trying the wrong one could cause problems! Most production systems use a script to automatically start MySQL upon booting up, and you should use this if one is in place.

Some distributions come with a script called mysql.server, which may even be automatically installed for you (sometimes renamed just to mysql). This would usually be placed in a directory where processes are automatically activated upon booting. If this is the case, you'd use this script to start. mysql.server takes start and stop options. The following is a common startup used on Red Hat Linux:

 % /etc/rc.d/init.d/mysql start % Starting mysqld daemon with databases from /usr/local/mysql/data

On FreeBSD, the file may be placed in /usr/local/etc/rc.d, in which case you'd use the following:

 % /usr/local/etc/rc.d/mysql.sh start 

To shut down, you can use mysqladmin:

 % mysqladmin shutdown -uroot -p         Enter password:  020706 16:56:02  mysqld ended

or the equivalent option from the startup script, such as this:

 % /etc/rc.d/init.d/mysql stop Killing mysqld with pid 2985 Wait for mysqld to exit\c .\c .\c .\c .\c .\c .\c 020706 17:07:49  mysqld ended

Starting MySQL Automatically upon Booting Up

In production systems, all but the greatest of control freaks will want MySQL to be running as soon as the system boots up. To achieve this, if it isn't already done for you, you'll need to know how your version of Unix starts and stops processes upon booting or shutting down. It can differ quite markedly from system to system. If you're unsure of this, especially if you didn't install the version of MySQL yourself, the best person to speak to is your system administrator. They should know your system's ins and outs better than I can explain in a book.

This example is from Red Hat Linux:

 % cp /usr/share/mysql/mysql.server /etc/rc.d/init.d 

This line copies the mysql.server script to the initialization directory.

The next two lines ensure that MySQL is started when the system boots up and reaches multiuser mode (run level 3) and shuts down with the system (run level 0). They create a link from the appropriate level to the mysql.server script:

 % ln -s /etc/rc.d/init.d/mysql.server /etc/rc.d/rc3.d/S99mysql % ln -s /etc/rc.d/init.d/mysql.server /etc/rc.d/rc0.d/S01mysql 

The next example is from a recent version of FreeBSD, where the startup scripts just need to be copied to the rc.d directory and given an .sh extension:

 % cp /usr/local/mysql/support-files/mysql.server /usr/local/etc/rc.d/mysql.sh 

Make sure your script is executable, and not accessible to any unauthorized eyes, with the following:

 % chmod 700 /usr/local/etc/rc.d/mysql.sh 

Some older systems may use /etc/rc.local to start scripts, in which case you should add something like the following to the file:

/bin/sh -c 'cd /usr/local/mysql ; ./bin/safe_mysqld --user=mysql &'

Avoiding Common Problems with Starting MySQL in Unix

The most common cause for problems is permissions. If you're not logged in as root and you try to start MySQL, you'll get an error such as the following:

% /usr/local/mysql/bin/mysqld_safe --user=mysql & % The file /usr/local/mysql/libexec/mysqld doesn't exist or is not executable Please do a cd to the mysql installation directory and restart this script from there as follows: ./bin/mysqld_safe.

To solve it, log in as root:

% su Password: % /usr/local/mysql/bin/mysqld_safe --user=mysql & [1] 24756 % Starting mysqld daemon with databases from  /usr/local/mysql-max-4.0.2-alpha-unknown-freebsdelf4.6-i386/data

For other problems, the MySQL error log may give you some assistance. See the section titled "The Error Log" later in this chapter.

Starting and Shutting Down in Windows

There are a number of executable files that come with a Windows distribution (see Table 10.1). You'll need to choose which one you'd like to run depending on how you're going to be using MySQL.

Table 10.1: The MySQL Executables

Executable

Description

mysqld

A binary that supports debugging, automatic memory allocation checking, transactional tables (InnoDB and BDB), and symbolic links.

mysqld-opt

An optimized binary that comes with no support for transactional tables (InnoDB or BDB).

mysqld-nt

An optimized binary that supports named pipes (for use with NT/2000/XP). It can run on 95/ 98/Me, but no named pipes will be created, as these operating systems do not support it.

mysqld-max

An optimized binary that supports transactional tables (InnoDB and BDB) and symbolic links.

mysqld-max-nt

An optimized binary that supports transactional tables (InnoDB and BDB) as well as named pipes when run on NT/2000/XP and symbolic links.

To start MySQL, simply run the executable you'd like to use, for example:

C:\> c:\mysql\bin\mysqld-max 020706 18:53:45  InnoDB: Started

Replace \mysql\bin\ with the directory where you've installed MySQL, if it is different. Many Windows users prefer to use the following:

C:\> c:\progra~1\mysql\bin\mysqld-max 

You can also use the winmysqladmin utility that comes with Windows distributions to start MySQL.

Note 

If you're still using Windows 95, make sure Winsock 2 is installed. Older versions of Windows 95 do not come with Winsock 2, and MySQL will not run. You can download it from www.microsoft.com.

Starting MySQL Automatically

With Windows 95/98/Me, create a shortcut to the executable file winmysqladmin in the StartUp folder. This file is stored in the same place as the other executables—in other words, in c:\mysql\bin by default.

Make sure your my.ini file contains the executable you want to use. You may run mysqld-max manually and then want to start it automatically with winmysqladmin. However, if your file contains the following, for example:

[WinMySQLAdmin] Server=C:/PROGRAM FILES/MYSQL/bin/mysqld-opt.exe

you won't be able to use the transactional capability you may have been expecting. You can edit the my.ini file manually, or you can use winmysladmin to modify it, selecting my.ini Setup and changing the mysqld file (see Figure 10.1).

click to expand
Figure 10.1: Using winmysqladmin to update the my.ini configuration file

With NT/2000/XP, install MySQL as a service as follows:

C:\> c:\mysql\bin\mysqld-max-nt -install 

If you don't want MySQL to start automatically, but you still want it as a service, run the same command with the manual option:

C:\mysql\bin> mysqld-max-nt --install-manual 

You can then start the service with the following:

C:\> net start mysql The MySql service is starting. The MySql service was started successfully.

And stop it with the usual mysqladmin shutdown or the following:

C:\> net stop mysql The MySql service is stopping............ The MySql service was stopped successfully.

To remove it as a service, run mysqld with the remove option, as follows:

C:\> c:\mysql\bin\mysqld-max-nt -remove 

You can also use the Services Control Panel, and click Start or Stop (see Figure 10.2).

click to expand
Figure 10.2: Starting MySQL as a service in Windows 2000

Avoiding Common Problems with Starting MySQL in Windows

A common problem with starting MySQL on Windows occurs when MySQL is installed in a nondefault directory (such as c:\Program Files\MySQL\bin). Sometimes the locations are not correctly reflected in the my.ini configuration file. For example, if you've installed MySQL in c:\Program Files\MySQL, then my.ini should contain something like the following:

[mysqld] basedir=C:/Program Files/mysql  datadir=C:/Program Files/data  [WinMySQLAdmin] Server=C:/Program Files/mysql/bin/mysqld-max-nt.exe 
Note 

Windows pathnames are specified with forward slashes, not the usual Windows backslash, in option files. If you want to use backslashes, you'll need to escape them (with another backslash), as the backslash is a special MySQL character, for example: Server=C:\\Program Files\\mysql\\bin\\mysqld-opt.exe.

You may have wanted to use spaces in your filename and tried something like this:

 C:/program files/mysql 

instead of this:

 C:/progra~1/mysql 

Starting winmysqladmin may create a my.ini file that interferes with an existing configuration. Try removing the newly created my.ini file (restoring the original if necessary).

If the problem still eludes you, try examining the error log to see if there is an obvious reason. The error log is in C:\MySQL\data\mysql.err by default.

You can also start MySQL in stand-alone mode (mysqld --standalone), which may give more useful output, or, as a final roll of the dice, in debug mode, which will write a trace file (usually to C:\mysqld.trace) that may be of some use.



Mastering MySQL 4
Mastering MySQL 4
ISBN: 0782141625
EAN: 2147483647
Year: 2003
Pages: 230
Authors: Ian Gilfillan

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