Open Source

Learn how to set up the versatile MySQL database on Linux

Use the steps in this tutorial to help you build a successful installation of the MySQL database server on Linux.


MySQL is a fast, robust, and freely available open source database platform that runs on almost any current operating system, including both Linux and Windows. In this article, we will go over a simple installation of MySQL 3.23.xx on Red Hat Linux 7.1.

Rather than use RPMs for a canned installation, we will build the MySQL package for maximum configurability. Although there are MySQL 4.0 packages available for download, this version is still in alpha testing. The most current stable release is 3.23.43, which we'll install for this article. I'll assume that you have installed Linux and have a stable platform with networking support enabled and no previous installations of MySQL to deal with. I'll also assume that you have a working compiler on the system and that you have installed the development tools (gcc, etc.).

Downloading the package
Before anything can be installed, it must be downloaded. To download MySQL 3.23.43, issue the following commands:
su <type root password>
cd /usr/local
ftp ftp.mysql.com
<username = anonymous>8
<password = your email address>
cd MySQL-3.23
bin
get mysql-3.23.43.tar.gz

Expanding and configuring the package
To unpack and expand the MySQL package, you will need to be a superuser, which you became when you downloaded the file in the previous step. When you used FTP to download the file in the previous step, you saved it in the /usr/local directory on your Linux server. To expand the file, do the following:
cd /usr/local
gunzip -dc mysql-3.23.43.tar.gz | tar xvf

Now that the MySQL distribution has been expanded, you need to run the configuration script, which details what portions of MySQL will be compiled, configured, and installed. First, switch to the mysql-3.23.43.tar.gz directory in /usr/local:
cd /usr/local/mysql-3.23.43

Configuration of the build files for MySQL can be either very simple or quite complex, depending on the options that you want to enable or disable. Table A details some of the common options that are available during the configuration phase of the installation. This is not a complete list of the available options.

Table A
Option Explanation Default
—bindir=</path/to/dir> Specifies the path in which to place user executables /usr/local/bin
—help Prints a list of all configuration options Not supplied
—libexecdir=</path/to/dir> Specifies the path in which to place the MySQL program executables /usr/local/libexec
—localstatedir=</path/to/dir> Preserves the default MySQL installation path but modifies the database storage path /usr/local/var
—prefix=</path/to/dir> Changes the installation path to a different location /usr/local
—quiet Disables informational messages that MySQL provides during the configuration phase Verbose
—sbindir=</path/to/dir> Specifies the path in which to place administrative executables /usr/local/sbin
—silent Same as —quiet  
—with-debug Enables debugging code Disabled
—with-mysqld-user=<user> Specifies which user account should be used to run the MySQL daemon Current user
—with-tcp-port=<tcp port> Allows an administrator to supply an alternate port for a MySQL installation; useful to change for security purposes 3306

The MySQL installation we will go through next will modify only the installation location of the program and databases. I prefer to install the program into a specific location to make it easier to find it and its associated databases later on.

I also prefer to run the MySQL daemon as a specific user rather than root for security purposes. For this installation, we will run the daemon as the mysql user, which we will create later.

To run the configuration utility for MySQL and to install the product into the /usr/local/mysql directory, type the following at the command prompt (this assumes that you are still in the /usr/local/mysql-3.23.43 directory):
./configure —prefix=/usr/local/mysql —with-mysqld-user=mysql

Once you press [Enter], you will see a number of “checking” messages flow down the screen. These messages test to ensure that your system is ready for MySQL. Don’t worry if you see some indicators that say “no.” This is normal and expected. When the configuration utility has finished, you simply will be brought back to a command prompt.

Installing MySQL
You now have a system that is ready for MySQL. In the previous step, you built a configuration script that specified all of the options you desired and that set up the files that you will compile in this step. To begin the compilation of MySQL (again, from the /usr/local/mysql-3.23.43 directory), simply type:
make

Please note that this step will take some time because it will have to compile and build every program used by MySQL. When this step is complete, you have built the executables. To place the files in their proper locations, issue this command:
make install

This process won't take long. The final step in the MySQL database installation is the setup of the initial databases and the assigning of user permissions. All of this is accomplished by running a single script from within the MySQL source distribution’s scripts directory. Do this by running the following two commands:
cd /usr/local/mysql-3.23.43/scripts
./mysql_install_db

Congratulations! You have now completed the installation of the files required to run MySQL.

Running MySQL
Before you can begin to use MySQL for your database work, you need to set up a few more things and make sure that MySQL is working properly.

In the configuration phase, you set up MySQL to run as the user mysql rather than root or the current user. This is an important security measure; it allows you to properly restrict what MySQL has access to do and allows you to protect the file system in the unlikely event of a security breach that uses the mysql account.

To create the mysql account, type the following at the command line:
/usr/sbin/useradd mysql

Now that the user has been created, you need to assign ownership of the database directory to this user so that there are no access problems. To do this, issue these two commands:
cd /usr/local/mysql/var
chown -R mysql:mysql .    [do not forget the trailing period]

Now, when you do a directory listing, you should get something like this:
drwx———    4 mysql    mysql        4096 Oc2t 13 15:04 .
drwxrwxr-x   12 root     root         4096 Oct 13 15:04 ..
drwx———    2 mysql    mysql        4096 Oct 13 15:04 mysql
drwx———    2 mysql    mysql        4096 Oct 13 15:04 test

All of the files in the directory and all subdirectories are now owned by the mysql user and group.

At this point, you can start the MySQL server and begin to serve up database functionality. Again, MySQL comes with a script that will do it all for you. To run this script, issue the following commands:
cd /usr/local/mysql/bin
./safe_mysqld &

You will now get a message indicating that MySQL is starting. You can verify that it has indeed started by typing:
/usr/local/mysql/bin/mysqladmin ping

The server will respond with the line mysqld is alive if the MySQL daemon is up and running.

Finally, you need to set up a password to protect the administrative interface from being misused. You do this by typing:
/usr/local/mysql/bin/mysqladmin -u root password pw

Replace pw with a password of your choice and one that is consistent with your organization’s security policy.

To start the MySQL administration program, type the following at the command line:
/usr/local/mysql/bin/mysql -uroot –ppw

You are now presented with a MySQL command line and are ready to begin creating databases.

What’s next?
You have just completed your setup of the MySQL database platform. In my next article, we will discuss how to make MySQL interact with Web sites by using a combination of PHP4 and Apache.

Have a comment or a question?
We look forward to getting your input and hearing about your experiences regarding this topic. Post a comment or a question about this article.

 

Editor's Picks

Free Newsletters, In your Inbox