Tuesday, August 11, 2009

Installing MySQL

lthough MySQL runs on many platforms, I describe how to install it on
Linux, Unix, Windows, and Mac, which together account for the major­ ity of Web sites on the Internet. Be sure to read the instructions all the way through before beginning the installation.

MySQL can be installed most easily from binaries — precompiled, ready-to- install packages. Binaries are available for most operating systems: Linux, Windows, Mac, FreeBSD, many flavors of Unix, and others. If such a package is available for your operating system, use it. Only install MySQL from source if it’s totally necessary, such as when there’s no binary for your operating system or you need some functionality that’s not compiled into the binaries (for example, a different character set).

If you have trouble starting the MySQL server after installing it, check the error log for useful information. The error log is located in the data directory and has the extension .err.

On Windows

In most cases, when you download and install MySQL, the server is started automatically. If it isn’t or if you need to stop and start it for another reason, you can start it manually using the WinMySQLadmin utility that is installed with MySQL, as I describe in the upcoming section, “Starting the MySQL server.” You can also use WinMySQLadmin to set up MySQL so that it starts every time your computer starts.

Downloading and installing MySQL

To install MySQL on Windows, follow these steps:

1. Point your Web browser to www.mysql.com, the MySQL home page.

2. Click the Production version number link.

Look for the section under the heading Database Server. As of this writ­
ing, the production release is 4.0.17.

MySQL 4.0.x is supported by PHP 4 or 5. Support for MySQL 4.1.x is pro­
vided beginning with PHP 5.

3. Scroll down the screen until you come to the Windows Downloads heading.

4. Click the download link for the Windows binary labeled Windows 95/
98/NT/2000/XP/2003. This binary includes an installer. A dialog box opens.
5. Select the option to save the file.

A dialog box opens that lets you select where you want the file saved.

6. Navigate to where you want to save the file (for example,
c:\downloads). Then click Save.

After the download, you see a Zip file in the download location (for example, c:\downloads) containing the MySQL files. The file is named mysql-, followed by the version number and -win.zip — for instance, mysql-4.0.17-win.zip.

7. Use your favorite Zip utility to unzip the files and save them in a tem­
porary location (for example, c:\downloads\mysql).

Two popular Zip utilities are PKZIP at www.pkware.com and WinZip at
www.winzip.com.

8. Navigate to the temporary directory where the unzipped files are stored. Then double-click setup.exe.

Note: If you’re installing from a Windows NT/2000/XP system, be sure that you’re logged into an account with administrative privileges.

The opening screen shown in Figure A-1 is displayed.

9. Click Next.

The license is displayed.

10. Click the I Agree button to continue.

You see a screen showing the directory where MySQL will be installed.

11. If you want to install MySQL in the default directory, c:\mysql, click Next. If you want to install MySQL in a different directory, click Browse, select a directory, and click OK; then click Next.

You see a screen in which you can choose the type of installation.

12. Select Typical and then click Next.

The installation of MySQL begins. A message appears when the installa­
tion is complete.

The server might or might not have been started during installation. If it is running, you should see a traffic signal in your system tray (bottom of your screen) with a green light showing. If it isn’t running, check out the next section.

Starting the MySQL server

You can start and stop your server manually, although you’re more likely to want the MySQL server to be running whenever your computer is running. To see how to set up MySQL so that it starts when the computer boots up, see the next section.

On Windows 98/Me
You can start and stop your server with WinMySQLadmin, a program that was installed with MySQL. The WinMySQLadmin program is responsible for displaying the traffic signal in your system tray. If the traffic signal is displayed, WinMySQL is running. If the traffic signal is not in your tray, you need to start WinMySQLadmin.

If WinMySQLadmin is not running, you might be able to start it from your Startup menu: Choose Start➪Programs➪Startup➪WinMySQLadmin. If you can’t find it on your Startup menu, use Windows Explorer to navigate to the bin directory in the directory where MySQL is installed (for example, c:\ mysql\bin) and double-click WinMySQLadmin. When it’s started, you should be able to see the traffic signal in your system tray.

To start or stop the MySQL server by using WinMySQLadmin, follow these steps:

1. Right-click the traffic signal in your system tray.

You see a short menu.

2. Select your operating system: Windows 9x.

3. To start or stop, click either Start the Server or Stop the Server.

4. Exit WinMySQLadmin by right-clicking in the WinMySQLadmin
window and then clicking Hide Me.

On Windows NT/2000/XP
You can start your MySQL server directly by navigating to the bin subdirec­ tory in the directory where MySQL is installed (such as c:\mysql\bin) and then double-clicking the file mysqld.exe.

The above procedure might not work if the WinMySQLadmin program isn’t running (if there is no traffic signal on your system tray). In this case, you can either start the WinMySQL program by double-clicking it or start the MySQL server with a DOS Window console. If you are having problems, it is often useful to start the server with a console because some helpful error mes­ sages will be displayed.

To start the MySQL server with a console, follow these steps:

1. Open a command prompt window.
For instance, choose Start➪Programs➪Accessories➪Command Prompt.

2. Change to the bin directory for MySQL.

For instance, CD c:\mysql\bin.

3. Type mysqld —console.

A message is displayed telling you that the server started. The window remains open and ready to receive any more messages from the server. Don’t close the window.

You might be able to stop the server using WinMySQLadmin if it is running
by right clicking the traffic signal, selecting Win NT from the menu displayed, and then clicking Stop the Server. If this doesn’t work, you can use the follow­ ing steps:

1. Open a command prompt window.
For instance, choose Start➪Programs➪Accessories➪Command Prompt.

2. Change to the bin directory for MySQL.

For instance, CD c:\mysql\bin.

3. Type mysqladmin -u root –p shutdown.

The -u is followed by an account name — in this case, root, which was installed when MySQL was installed. If you use the -p, you will be prompted for a password. Unless you installed a password, you shouldn’t need one, so leave out the -p.

Setting up the server to start when the computer starts

In most cases, you want to set up your MySQL server so that it is running whenever your computer is running.

On Windows 98/Me
If you want to set up your MySQL server so that it starts every time your computer starts, start WinMySQLadmin as I describe in the previous section and then follow these steps:

1. Click the my.ini Setup tab.

2. Click Create ShortCut on Start Menu in the bottom-left corner of the screen.

3. Exit WinMySQLadmin by right-clicking in the WinMySQLadmin
window and then choosing Hide Me.

On Windows NT/2000/XP
To set up the MySQL server so that it starts every time the computer boots, set it up as a service. The installation procedure might have set it up as a ser­ vice. You can check whether MySQL is a service as follows:

1. Open the services window at Start➪Settings➪Control Panel➪
Administrative Tools->Services.

A window showing a list of all the services opens.

2. Scroll down the list to see whether MySQL is listed.

If it is not, it is not set up as a service. See how to set it up as a service later in this section.

3. Select the Startup Type.

It should say Automatic. If it doesn’t, do the following:

1. Right click the entry for MySQL.

2. Choose Properties from the menu that is displayed.

3. Select Automatic from the Startup Type drop-down list in the middle of the window.

4. Click OK.

If MySQL was not shown in the list of services, you can set it up as a service as follows:

1. Open a command prompt window.
For instance, choose Start➪Programs➪Accessories➪Command Prompt.

2. Change to the bin directory for MySQL.

For instance, CD c:\mysql\bin.

3. Type mysqld —install.

A message is displayed telling you the service was successfully installed.

If you need to remove the MySQL server from the services list, such as when you are about to upgrade to a newer version, follow the steps 1 and 2 above. For step 3, type mysqld —remove.

On Linux/Unix

Using RPM (Linux only)

MySQL can be installed on Linux using RPM. Although RPM stands for Red Hat Package Manager, RPM is available on most flavors of Linux, not just Red Hat. Using RPM is the easiest way to install on Linux. If installing from an RPM file doesn’t work for you, try using a ready-to-install package called a binary, which is also easy to install; for installation instructions, see the section,

“From binary files.” If neither of these methods works for you, you can always install MySQL from source files. To do this, follow the instructions in the sec­ tion, “From source files.”

You can download the RPM file using the following instructions. However, the RPM file might already be on the CD that your Linux operating system came on. Installing the RPM file from a CD saves you the trouble of downloading (you can skip Steps 1–9 in the following list), but if the version of MySQL on your CD is not the most recent, you might want to download an RPM file anyway.

To install MySQL on Linux from an RPM file, follow these steps:

1. Point your Web browser to www.mysql.com, the MySQL home page.

2. Click the Production version number link.

Look in the list on the right side of the screen, labeled MySQL Products. Look for the section under the heading Database Server. As of this writ­ ing, the production release is 4.0.17.

3. Scroll down the screen until you come to the Linux x86 RPM Downloads heading.

This section lists several downloads for Linux.

4. Click the download link for the standard server. This should be the first entry.

A dialog box opens.

5. Select the option to save the file.

A box opens that lets you select where you want to save the file.

6. Navigate to where you want to save the RPM (for example, /usr/src/
mysql). Then click Save.

7. Repeat Steps 5–7 to download the RPM file for Client Programs into the same download location.

8. Change to the directory where you saved the download.

For instance, type cd /usr/src/mysql. You see two files in the directory — one file named MySQL-server-, followed by the version number and .i386.rpm, and a second file named similarly with client embedded in its name. For example: MySQL-4.0.15-0.i386.rpm and MySQL-client-4.0.15-0.i386.rpm.

9. Install the RPM by entering this command:

rpm -i listofpackages

For instance, using the example in Step 10, the command would be this:

rpm -i MySQL-server-4.0.17-0.i386.rpm MySQL-client-
4.0.17-0.i386.rpm

This command installs the MySQL packages. It sets the MySQL account and group name that you need, and creates the data directory at /var/ lib/mysql. It also starts the MySQL server and creates the appropriate entries in /etc/rc.d so that MySQL starts automatically whenever your computer starts.

You need to be using an account that has permissions to successfully run the rpm command, such as a root account.

10. To test that MySQL is running okay, type this:

bin/mysqladmin --version

You should see the version number of your MySQL server.

From binary files

Ready-to-use, compiled binary files are available for several flavors of Linux and Unix. If none of the flavors work for your Linux/Unix machine, you can install MySQL from source files, but it’s better to use a binary if at all possi­ ble. As of this writing, MySQL binary files were available for the following fla­ vors of Unix, but more could be made available at any time:

Solaris

HP-UX

AIX

SCO

SGI Irix

Dec OSF

QNX

BSDi

OpenBSD

FreeBSD

To install a binary file version of MySQL on Linux or Unix, follow these steps:

1. Point your Web browser to www.mysql.com, the MySQL home page.

2. Click the Production version number link.

Look in the list on the right side of the screen, labeled MySQL Products. Look for the section under the heading Database Server. As of this writ­ ing, the production release is 4.0.17.

3. Scroll down the screen until you come to the heading for Linux or for your version of Unix (for example, Solaris Downloads).

Each section lists several downloads for that operating system.

4. Locate the correct package for your version of operating system.

For Linux, you probably want the Intel libc6 binary version. For Unix, select the correct version of the Unix system — for example, Solaris 9 (SPARC, 64-bit).

5. Click the download link for the standard version for your operating system.

A dialog box opens.

6. Select the option to save the file.

A box opens that lets you select where you want to save the file.

7. Navigate to where you want to install MySQL. Then click Save.

The standard location is /usr/local; it’s best to use this location if possible.

8. After the download is complete, change to the download directory —
for instance, cd-/usr/local.

You see a file named mysql-, followed by the version number, the name of the operating system, and .tar.gz — for instance, mysql-4.0.17- sun-solaris2.9-sparc.64bit-tar.gz. This file is a tarball.

9. Create a user and group ID for MySQL to run under by using these commands:

groupadd mysql
useradd -g mysql mysql

The syntax for the commands might differ slightly on different versions of Unix, or they might be called adduser and addgroup.

Note: You must be using an account that is authorized to add users and groups.

10. Unpack the tarball by typing this:

gunzip -c filename | tar -xvf –

For example:

gunzip -c mysql-4.0.17-sun-solaris2.9-sparc-64bit.tar.gz | tar -xvf –

Note: You must be using an account that is allowed to create files in
/usr/local.

11. Create a link to the new directory so that you can refer to it by a shorter name, rather than its current, difficult-to-type name. Type the following:

ln -s newdirectoryname mysql

For example:

ln -s mysql-4.0.17-sun-solaris2.9-sparc-64bit mysql

Now you can refer to the directory as mysql, instead of by its long name.

12. Change to the new directory by typing cd mysql.

You should see several subdirectories, including /bin and /scripts.

13. Add the path to the bin directory (for example, /usr/local/mysql/ bin) to your system path so that the MySQL programs can be accessed by any programs that need to access MySQL.

You should do this by editing the file that sets the system variables when your computer starts up.

14. Type the following:

scripts/mysql_install_db

This command runs a script that initializes your MySQL databases.

15. Make sure that the ownership and group membership of your MySQL
directories are correct. Set the ownership with these commands:

chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/data chgrp -R mysql /usr/local/mysql

These commands make root the owner of all the MySQL directories except data and make mysql the owner of data. All MySQL directories belong to group mysql.

16. Set up your computer so that MySQL starts automatically when your machine starts by copying the file mysql.server from /usr/local/ mysql/support-files to the location where your system has its startup files.

17. To test MySQL, you can start your server manually, without restarting your computer, by typing the following:

bin/safe_mysqld --user=mysql &

18. To test that MySQL is running okay, type

bin/mysqladmin --version

You should see the version number of your MySQL server.

From source files

Before you decide to install MySQL from source files, check for binary files for your operating system. MySQL binary files are precompiled, ready-to-install packages for installing MySQL. MySQL binary files are very convenient and reliable.

You install MySQL by downloading source files, compiling the source files, and installing the compiled programs. This process sounds terribly technical and daunting, but it’s not. Read all the way through the following steps before you begin the installation procedure.

To install MySQL from source code, follow these steps:

1. Point your Web browser to www.mysql.com, the MySQL home page.

2. Click the Production version number link.

Look in the list on the right side of the screen, labeled MySQL Products. Look for the section under the Database Server heading. As of this writ­ ing, the production release is 4.0.17.

3. Scroll to the bottom of the screen to the Source Downloads heading.

This section lists several downloads.

4. Locate the tarball version and click the download link next to it.

A dialog box opens.

5. Select the option to save the file.

A box opens that lets you select where the file will be saved.

6. Navigate to where you want to install MySQL and then click Save.

The standard location is /usr/local. It is best to use the standard loca­
tion if possible.

7. After the download is complete, change to the download directory —
for instance, cd-/usr/local.

You see a file named mysql-, followed by the version number and
.tar.gz. — for instance, mysql-4.0.17.tar.gz. This file is a tarball.

8. Create a user and group ID for MySQL to run under by using the fol­
lowing commands:

groupadd mysql
useradd -g mysql mysql

The syntax for the commands might differ slightly on different versions of Unix, or they might be called adduser and addgroup.

Note: You must be using an account that is authorized to add users and groups.

9. Unpack the tarball by typing

gunzip -c filename | tar -xvf –

For example:

gunzip -c mysql-3.23.44.tar.gz | tar -xvf –

You see a new directory named mysql-version — for instance,
mysql-4.0.15.

You must be using an account that is allowed to create files in
/usr/local.

10. Change to the new directory.

For instance, type cd mysql-4.0.17.

11. Type the following:

./configure --prefix=/usr/local/mysql

You see several lines of output. The output will tell you when configure is done. This might take some time.

12. Type make.

You see many lines of output. The output will tell you when make is done. make might run for some time.

13. Type make install.

make install will finish quickly.

Note: You might need to run this command as root.

14. Type the following:

scripts/mysql_install_db.

This command runs a script that initializes your MySQL databases.

15. Make sure that the ownership and group membership of your MySQL
directories are correct. Set the ownership with these commands:

chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/data chgrp -R mysql /usr/local/mysql

These commands make root the owner of all the MySQL directories except data and make mysql the owner of data. All MySQL directories belong to group mysql.

16. Set up your computer so that MySQL starts automatically when your machine starts by copying the file mysql.server from /usr/local/ mysql/support-files to the location where your system has its startup files.

17. To test MySQL, you can start your server manually, without restarting your computer, by typing the following:

bin/safe_mysqld --user=mysql &

18. To test that MySQL is running okay, type:

bin/mysqladmin --version

You should see the version number of your MySQL server.

On Mac

You can download MySQL using a Mac OS X 10.2 (Jaguar) PKG binary package. If your operating system is OS X 10.1 or earlier, you can’t use this package.
You will need to download a tarball and install MySQL from source code, as described in the previous section.

1. Point your Web browser to www.mysql.com, the MySQL home page.

2. Click the Production version number link.

Look in the list on the right side of the screen, labeled MySQL Products. Look for the section under the heading Database Server. As of this writ­ ing, the production release is 4.0.17.

3. Scroll down the screen to find the section with the heading Mac OS Package Installer Downloads.

This section lists several downloads.

4. Locate the standard version and then click the download link next to it.

A dialog box opens.

5. Select the option to save the file.

A box opens that lets you select where the file will be saved.

6. Navigate to where you want to install MySQL and then click Save.

The standard location is /usr/local. It is best to use the standard loca­
tion if possible.

7. After the download is complete, change to the download director —
for instance, /usr/local.

You see a package named mysql-standard, followed by the version number and dmg, such as mysql-standard-4.0.17.dmg. If the down­ loaded file does not have the extension .dmg, change the filename to give it the .dmg extension.

8. Create a user and a group named mysql for MySQL to run under. In
most newer Mac versions, this user and group already exist.

9. Mount the disk image by double-clicking its icon in the Finder.

10. Double-click the package icon to install the MySQL PKG.

The package installer will run and install the package. It installs MySQL
in the directory /usr/local/mysql-, followed by the version number. It also installs a symbolic link /usr/local/mysql/ pointing to the direc­ tory where MySQL is installed. It also initializes the database by running the script mysql_install_db, which creates a MySQL account called root.

11. You might need to change the owner of the mysql directory.

The directory where MySQL is installed (for example, /usr/local/
mysql-4.0.17) should be owned by root. The data directory (such as,
/usr/local/mysql-4.0.17/data) should be owned by the account mysql. Both directories should belong to the group mysql. If the user and group are not correct, change them with the following commands:

sudo chown -R root /usr/local/mysql-4.0.17
sudo chown -R mysql /usr/local/mysql-4.0.17/data sudo chown -R root /usr/local/mysql-4.0.17/bin

12. Start the MySQL server using the following commands:

cd /usr/local/mysql sudo ./bin/mysqld_safe
if necessary, enter your password
Press Ctrl-Z
bg
Press Ctrl-D or type exit

This starts the server manually, meaning you must start the MySQL server every time you restart your computer. To have your server start every time the computer is started, you need to install the MySQL Startup Item, which is included in the installation disk image in a sepa­ rate installation package. To install the Startup Item, double-click the MySQLStartupItem.pkg icon.

To stop the MySQL server, change to the bin subdirectory in the directory where MySQL is installed and type

mysqladmin –u root –p shutdown

The -p causes mysqladmin to prompt you for a password. If the account doesn’t require a password, don’t include -p.

Configuring MySQL

MySQL reads a configuration file when it starts up. If you use the defaults or an installer, you probably don’t need to add anything to the configuration file. However, if you install MySQL in a nonstandard location or want the databases to be stored somewhere other than the default, you might need to edit the configuration file.

The file is named my.ini or my.cnf. It’s located in your system directory (such as WINNT) if you are using Windows and in /etc on Linux/Unix/Mac. The file looks something like the following:

[mysqld] basedir=D:/mysql4 datadir=D:/mysql4/data
#port=3306

The basedir line tells the MySQL server where MySQL is installed. The datadir line tells the server where the databases are located. The # at the beginning of the last line makes the line inactive. You could remove the # and change the port number to tell the server to listen for database queries on a different port.

0 comments: