News:

Build a stunning handcrafted website with IT Acumens

Main Menu

MYSQL Installation In Linux

Started by sivaji, Jan 11, 2008, 12:58 PM

Previous topic - Next topic

sivaji

Linux Installation

This section covers the procedure for installing PHP and MySQL under most current distributions of Linux. These instructions were tested under Fedora Core 2; however, they should work on other distributions such as Debian, SUSE, and Mandrake without much trouble. The steps involved will be very similar, if not
identical.

As a user of one of the handful of Linux distributions available, you may be tempted to download and install packaged distributions of PHP and MySQL. Debian users will be used to installing software using the apt-get utility, while distributions like Fedora Core tend to rely on RPM packages. These prepackaged
versions of software are really easy to install; unfortunately, they also limit the software configuration options available to you. If you already have MySQL and PHP installed in packaged form, feel free to proceed with those versions, and skip forward to the section called "Post-Installation Setup Tasks". If you encounter any problems, you can always return here to uninstall the packaged versions and reinstall PHP and MySQL by hand.

This section will assume that you have the Apache Web server installed on your machine already. If you don't, chances are that your distribution offers an easy way to install it (I have no objection to your using the packaged distributions of Apache). I recommend Apache 1.3 over Apache 2.0, as support for Apache 2.0 in PHP is still experimental, but I'll provide instructions for both versions here.

Removing Packaged Software

Since many Linux distributions will automatically install PHP and MySQL for you, your first step should be to remove any old packaged versions of PHP and MySQL from your system. If one exists, use your distribution's graphical software manager to remove all packages with php or mysql in their names.

If your distribution doesn't have a graphical software manager, or if you didn't install a graphical user interface for your server, you can remove these packages from the command prompt. You'll need to be logged in as the root user to issue the commands to do this. Note that in the following commands, shell# represents the shell prompt, and shouldn't be typed in.

In Fedora Core, RedHat, or Mandrake, you can use the rpm command-line utility:

shell#rpm -e mysql
shell#rpm -e php

In Debian, you can use apt-get to remove the relevant packages:

shell#apt-get remove mysql-server
shell#apt-get remove mysql-client
shell#apt-get remove php4
shell#apt-get remove php5

If any of these commands tell you that the package in question is not installed, don't worry about it unless you know for a fact that it is. In such cases, it will be necessary for you to remove the offending item by hand. Seek help from an experienced user if you don't know how.

If the command(s) for removing PHP completed successfully (i.e. no error message was displayed), then you have just removed PHP from your Web server, and you should check that you haven't broken it in the process. To make sure Apache is still in working order, you should restart it without the PHP plug-in:

shell#apachectl graceful

If Apache fails to start up, you'll need to have a look through its configuration file, which is usually called httpd.conf and may be found in /etc/apache or /etc/httpd. Look for leftover commands that may be trying to load the PHP plug-in that you have just removed from the system. The Apache error log files
may be of assistance in tracking these down if you can't find them. When you're finished, try restarting Apache again.

With everything neat and tidy, you're ready to download and install MySQL and PHP.

Installing MySQL

MySQL is freely available for Linux from http://dev.mysql.com/downloads/. Download the recommended stable release (4.0 as of this writing). You should grab the Standard version under Linux (x86, libc6) in the Linux downloads section.

Once you've downloaded the program (it was about 15MB as of this writing), you should make sure you're logged in as root before proceeding with the installation, unless you want to install MySQL only in your own home directory. To begin, move to /usr/local (unless you want to install MySQL elsewhere for
some reason) and unpack the downloaded file to create the MySQL directory (replace version with the full version of your MySQL download to match the downloaded file name on your system):

shell#cd /usr/local
shell#tar xfz mysql-version.tar.gz

Next, create a symbolic link to the mysql-version directory with the name mysql to make accessing the directory easier, then enter the directory:

shell#ln -s mysql-version mysql
shell#cd mysql

While you can run the server as the root user, or even as yourself (if, for example,you installed the server in your own home directory), the best idea is to set up on the system a special user whose sole purpose is to run the MySQL server. This will remove any possibility of someone using the MySQL server as a way to break into the rest of your system. To create a special MySQL user, you'll need to log
in as root and type the following commands:

shell#groupadd mysql
shell#useradd -g mysql mysql

MySQL is now installed, but before it can do anything useful, its database files need to be installed, too. In the new mysql directory, type the following command:

shell#scripts/mysql_install_db --user=mysql

By default, MySQL stores all database information in the data subdirectory of the directory to which it was installed. We want to ensure that nobody can access that directory except our new MySQL user. Assuming you installed MySQL to the /usr/local/mysql directory, you can use these commands:

shell#cd /usr/local/mysql
shell#chown -R root .
shell#chown -R mysql data
shell#chgrp -R mysql .

Now everything's set for you to launch the MySQL server for the first time. From the MySQL directory, type the following command:

shell#bin/mysqld_safe --user=mysql &

If you see the message mysql daemon ended, then the MySQL server was prevented from starting. The error message should have been written to a file called hostname.err (where hostname is your machine's host name) in MySQL's data directory. You'll usually find that this happens because another MySQL server
is already running on your computer.

If the MySQL server was launched without complaint, the server will run (just like your Web or FTP server) until your computer is shut down. To test that the server is running properly, type the following command:

shell#bin/mysqladmin -u root status

A little blurb with some statistics about the MySQL server should be displayed. If you receive an error message, something has gone wrong. Again, check the hostname.err file to see if the MySQL server output an error message while starting up. If you retrace your steps to make sure you followed the process described above, and this doesn't solve the problem, a post to the SitePoint Forums[11] will help you pin it down in no time.

If you want your MySQL server to run automatically whenever the system is running (just like your Web server probably does), you'll have to set it up to do so. In the support-files subdirectory of the MySQL directory, you'll find a script called mysql.server that can be added to your system startup routines to
do this. Let me show you how.

First of all, assuming you've set up a special MySQL user to run the MySQL server, you'll need to tell the MySQL server to start as that user by default. To do this, create in your system's /etc directory a file called my.cnf that contains these two lines:

[mysqld]
user=mysql

Now, when you run safe_mysqld or mysql.server to start the MySQL server, it will launch as user mysql automatically. You can test this by stopping MySQL, then running mysql.server with the start argument:

shell#bin/mysqladmin -u root shutdown
shell#support-files/mysql.server start

Request the server's status using mysqladmin as before, to make sure it's running correctly.

All that's left to do is to set up your system to run mysql.server automatically at startup (to launch the server) and at shutdown (to terminate the server). This is a highly operating system-dependant task. If you're not sure how to do it, you'd be best to ask someone who is. The following commands, however, will do the trick for most versions of Linux:

shell#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
shell#cd /etc/rc2.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc3.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc5.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc0.d
shell#ln -s ../init.d/mysql.server K01mysql

That's it! To test that this works, reboot your system and request the status of the server as before.
One final thing you might like to do for the sake of convenience is to place the MySQL client programs, which you'll use to administer your MySQL server later on, in the system path. To this end, you can place symbolic links to mysql, mysqladmin, and mysqldump in your /usr/local/bin directory:

shell#ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
shell#ln -s /usr/local/mysql/bin/mysqladmin
/usr/local/bin/mysqladmin
shell#ln -s /usr/local/mysql/bin/mysqldump
/usr/local/bin/mysqldump
Am now @ Chennai