Distributions are available for several versions of MySQL. Currently, stable releases have version numbers in the 4.0 series and development releases are in the 4.1 series. Generally, you should use the highest-numbered version in the series you want to use that is available in the distribution format you want to work with. MySQL distributions come in binary, RPM, and source formats. Binary and RPM distributions are easier to install, but you must accept the installation layout and configuration defaults that are built into the distribution. Source distributions are more difficult to install because you must compile the software, but you also get more control over configuration parameters. For example, you can compile the distribution for client support only without building the server, and you can install the software wherever you want. MySQL distributions contain one or more of the following components:
Source and binary distributions contain all of these items. RPM files contain only some of them, so you may need to install multiple RPMs to get everything you need. If you plan to connect to a server that's running on another machine, you don't need to install a server. But you should always install client software for the following reasons:
Overview of UNIX MySQL InstallationInstalling MySQL on UNIX involves the following steps:
Creating a Login Account for the MySQL UserThis step is necessary only for a first-time installation and only if you're going to run a MySQL server. You can skip it for an upgrade or if you're running MySQL client software only. The MySQL server can be run as any UNIX user on your system, but for security and administrative reasons, you should not run the server as root. I recommend that you create a separate account to use for MySQL administration and that you run the server as that user. That way, you can log in as that user and have full privileges in the data directory for performing maintenance and troubleshooting. Procedures for creating user accounts vary from system to system. Consult your local documentation for specific details. This book uses mysqladm and mysqlgrp for the UNIX user and group names of the MySQL administrative account. If you plan to install MySQL only for your own use, you can run it as yourself, in which case you'll use your own login and group names wherever you see mysqladm and mysqlgrp in this book. If you use RPM files, the RPM installation procedure will create a login account for a user named mysql automatically for you. In that case, you will want to substitute mysql for mysqladm. Advantages of using a separate, unprivileged account rather than root for running MySQL are as follows:
Obtaining and Installing a MySQL Distribution on UNIXThe following instructions use version to stand for the version number of the MySQL distribution you're working with and platform to stand for the name of the platform on which you're installing it. These are used in distribution filenames so that distributions can be identified easily and distinguished from one another. A version number is something like 3.23.52 or 4.0.5-beta. A platform name is something like sun-solaris2.8-sparc or pc-linux-gnu-i686. Binary distribution files that have -max in the filename include a server built with additional features that the standard server does not have. The particular features change occasionally, so you should check the MySQL Web site to see how the standard and max distributions currently differ. If you're compiling from source, you enable or disable features by running the configure script with the appropriate options. Installing a Binary DistributionBinary distribution files have names such as mysql-version-platform.tar.gz. Obtain the distribution file for the version and platform you want and put it in the directory under which you want to install MySQL for example, /usr/local. Unpack the distribution using one of the following commands (use the second command if your version of tar doesn't understand the z option): % tar zxf mysql-version-platform.tar.gz % gunzip < mysql-version-platform.tar.gz | tar xf - Unpacking a distribution file creates a directory named mysql-version-platform that contains the distribution's contents. To make it easier to refer to this directory, create a symbolic link to it named mysql: % ln -s mysql-version-platform mysql Now you can refer to the installation directory as /usr/local/mysql if you installed MySQL under /usr/local. To allow invocation of MySQL client programs from the command line without typing their full pathnames, set your PATH environment variable to include the bin directory located under the MySQL installation directory. Your path is set in one of your shell's startup files. If you are going to use only the client support provided by the distribution and are not running a server, you're done installing MySQL. If you are installing MySQL for the first time, go to the "Initializing the Data Directory and Grant Tables" section later in this appendix. If you are updating an existing installation, go to the "Starting the Server" section later in this appendix. Installing an RPM DistributionRPM files are available for installing MySQL on Linux systems. These have filenames like the following:
You don't need to be in any particular directory when you install from an RPM file because RPMs include information indicating where their contents should be installed. For any RPM file rpm_file, you can determine where it will be installed with the following command: % rpm -qpl rpm_file To install an RPM file, use the following command (you'll probably need to do this as root): # rpm -i rpm_file Various parts of MySQL are divided into different RPM files, so you may need to install more than one RPM. To install client support, use the following command: # rpm -i MySQL-client-version-platform.rpm For server support, use the following commands (skip the second one if you don't want the additional features provided by the Max server): # rpm -i MySQL-version-platform.rpm # rpm -i MySQL-Max-version-platform.rpm If you plan to write your own programs using the client programming support, make sure to install the development RPM file: # rpm -i MySQL-devel-version-platform.rpm If you want to install from the source RPM file, the following command should be sufficient: # rpm recompile MySQL-version.src.rpm To allow invocation of MySQL client programs from the command line without typing their full pathnames, set your PATH environment variable to include the bin directory located under the MySQL installation directory. Your path is set in one of your shell's startup files. If you plan to use only the client support provided by the distribution and are not running a server, you're done installing MySQL. If you are installing MySQL for the first time, go to the "Initializing the Data Directory and Grant Tables" section later in this appendix. If you are updating an existing installation, go to the "Starting the Server" section later in this appendix. Installing a Source DistributionMySQL source distributions have names such as mysql-version.tar.gz, where version is the MySQL version number. Pick the directory under which you want to unpack the distribution and move into it. Obtain the distribution file and unpack it using one of the following commands (use the second command if your version of tar doesn't understand the z option): % tar zxf mysql-version.tar.gz % gunzip < mysql-version.tar.gz | tar xf - Unpacking a distribution file creates a directory named mysql-version that contains the distribution's contents; change the location into that directory: % cd mysql-version Now you need to configure and compile the distribution before you can install it. If the following steps fail, check the installation chapter in the MySQL Reference Manual, particularly any system-specific notes it may contain about your type of machine. Use the configure command to configure the distribution: % ./configure You may want to specify options for configure. To obtain a list of available options, run the following command: % ./configure --help The following list shows some configuration options many people find helpful:
After you run configure, compile the distribution and then install it: % make % make install You may need to be root to run the install command if you didn't run configure with a --prefix option that specifies an installation directory in which you have write permission. To allow invocation of MySQL client programs from the command line without typing their full pathnames, set your PATH environment variable to include the bin directory located under the MySQL installation directory. Your path is set in one of your shell's startup files. If you are going to use only the client support provided by the distribution and are not running a server, you're done installing MySQL. If you are installing MySQL for the first time, go to the "Initializing the Data Directory and Grant Tables" later in this appendix. If you are updating an existing installation, go to the "Starting the Server" section later in this appendix. Initializing the Data Directory and Grant TablesBefore you can use your MySQL installation, you need to initialize the mysql database that contains the grant tables controlling network access to your server. This step is needed only for a first-time installation and only if you will run a server. It is not needed for client-only installations. In the following instructions, DATADIR represents the pathname to your data directory. Normally, you run the commands shown here as root. If you're logged in as the MySQL user (that is, mysqladm) or you've installed MySQL under your own account because you intend to run it for yourself, you can execute the commands without being root. In that case, you can skip the chown and chmod commands. To initialize the data directory, the mysql database, and the default grant tables, change location into the MySQL installation directory and run the mysql_install_db script. (You need not do this if you are installing from RPM files because mysql_install_db will be run for you automatically.) For example, if you installed MySQL into /usr/local/mysql, the commands look like this: # cd /usr/local/mysql # ./bin/mysql_install_db If mysql_install_db fails, consult the installation chapter in the MySQL Reference Manual to see if it says anything about the problem you're encountering, and then try again. Note that if mysql_install_db doesn't run to completion successfully, any grant tables it creates are likely incomplete. You should remove them because mysql_install_db may not try to re-create any tables that it finds already existing. You can remove the entire mysql database as follows: # rm -rf DATADIR/mysql After running mysql_install_db, change the user and group ownership and the mode of all files under the data directory. Assuming that the user and group are mysqladm and mysqlgrp, the commands are as follows: # chown -R mysqladm.mysqlgrp DATADIR # chmod -R go-rwx DATADIR The chown command changes the ownership to the MySQL user, and chmod changes the mode to keep everybody out of the data directory except that user. Starting the ServerThis step is needed only if you plan to run a server. Skip it for a client-only installation. Run the commands in this section from the MySQL installation directory (just as for the commands in the previous section). Normally, you run the commands as root. If you're logged in as the MySQL user (that is, mysqladm) or you've installed MySQL under your own account, you can execute the commands without being root and should omit the --user option. Change location into the MySQL installation directory (for example, /usr/local/mysql) and then use the following command to start the server: # cd /usr/local/mysql # ./bin/mysqld_safe --user=mysqladm & The --user option tells the server to run as mysqladm. (Note that prior to MySQL 4, mysqld_safe is named safe_mysqld.) There are other actions that you'll probably want to perform at this point:
Instructions for performing these actions are given in Chapter 11. Installing Perl DBI Support on UNIXInstall the DBI software if you want to write Perl scripts that access MySQL databases. You must install the DBI module that provides the general DBI driver, and the DBD::mysql module that provides the MySQL-specific driver. DBI requires Perl 5.005_003 or later. (If you don't have Perl installed, visit http://www.perl.com/, download a Perl distribution, and install it before you install DBI support.) The MySQL C client library must be available as well because DBD::mysql uses it. (The C library should have been installed as part of the MySQL installation procedure.) You also can install the CGI.pm module if you want to write Web-based DBI scripts. To find out if a given Perl module is already installed, use the perldoc command; if a module is installed, perldoc will display its documentation: % perldoc DBI % perldoc DBD::mysql % perldoc CGI The easiest way to install Perl modules under UNIX is to use the CPAN shell. Issue the following commands as root: # perl -MCPAN -e shell cpan> install DBI cpan> install DBD::mysql cpan> install CGI You also can download source distributions from cpan.perl.org as compressed tar files. Unpack a distribution file dist_file.tar.gz using one of the following commands (use the second command if your version of tar doesn't understand the z option): % tar zxf dist_file.tar.gz % gunzip < dist_file.tar.gz | tar xf - Then change location into the distribution directory created by the tar command and run the following commands (you may need to be root to run the installation step): % perl Makefile.PL % make % make test # make install Whichever installation method you choose, Perl will ask you some questions when you install the DBD::mysql module:
If you have problems installing the Perl modules, consult the README file for the relevant distribution as well as the mail archives for the DBI mailing list. The answers for most installation problems can be found there. Installing Apache and PHP on UNIXThe following instructions assume that you'll run PHP as a DSO (dynamic shared object) module using the Apache httpd server. This means that Apache should be installed first, and then you can build and install PHP. If Apache is not installed on your system already, you can either install a binary distribution that has DSO support enabled or compile a source distribution to include DSO support. After Apache has been installed, configure your PHP distribution by using one of the following commands: % ./configure --with-mysql --with-apxs % ./configure --with-mysql --with-apxs=/path/to/apxs The --with-mysql option indicates that you want to build PHP to include MySQL client support. This option must be included for MySQL-based PHP scripts to work correctly. If the first command fails because configure cannot tell where the apxs script is located, use the second command to indicate its location explicitly. (The apxs Apache Extension Tool is a helper script that provides other modules with information about your Apache configuration.) After configuring PHP, build and install it as follows (you may need to be root to perform the installation commands): % make # make install # cp php.ini-dist /usr/local/lib/php.ini The cp command installs a baseline PHP initialization file where PHP can find it. You can substitute php.ini-recommended for php.ini-dist if you like; take a look at both and choose the one you prefer. After PHP is installed, edit the Apache configuration file, httpd.conf. You'll need to instruct Apache to load the PHP module when it starts up and also how to recognize PHP scripts. To tell Apache to load the PHP module, httpd.conf will need to include LoadModule and AddModule directives in the appropriate sections (look for other similar directives). The directives may be added for you during the installation step. If not, they should look something like the following: LoadModule php4_module libexec/libphp4.so AddModule mod_php4.c Next, edit httpd.conf to tell Apache how to recognize PHP scripts. PHP recognition is based on the filename extension that you use for PHP scripts. .php is the most common extension and is the one used for examples in this book. Another popular extension is .phtml. You can simply enable them both if you want; that may be best anyway if you're going to be installing pages that you obtain from other people that use one or another of these extensions. To do so, include the following lines in the configuration file: AddType application/x-httpd-php3 .php AddType application/x-httpd-php3 .phtml You can also tell Apache to recognize index.php and index.phtml as allowable default files for a directory when no filename is specified at the end of a URL. You'll probably find a line in httpd.conf that looks like the following: DirectoryIndex index.html Change it to DirectoryIndex index.php index.phtml index.html After editing the Apache configuration file, bring down the httpd server, if one was already running, and then restart it. On many systems, the following commands (executed as root) accomplish this: # /usr/local/apache/bin/apachectl stop # /usr/local/apache/bin/apachectl start You can also set up Apache to start up and shut down at system startup and shutdown time. See the Apache documentation for instructions. Normally, this involves running apachectl start at boot time and apachectl stop at shutdown time. If you encounter problems setting up PHP, check the "VERBOSE INSTALL" section of the INSTALL file included with the PHP distribution. (It's not a bad idea to read that file anyway. It contains lots of useful information.) |