Installing mySQL under Linux

mySQL is a robust Relational Database Management System. Currently it runs under Linux, Windows, OS2, and several other operating systems. Aside from the fact that it is free (yes, you heard right), mySQL has become the database management system of choice for many small and large solutions (Yahoo has been known to use mySQL).
This tutorial teaches how to install mySQL under Linux.

Installing mySQL under Linux

For those of you not yet familiar with mySQL, it is a robust Relational Database Management System. Currently it runs under Linux, Windows, OS2, and several other operating systems. Aside from the fact that it is free (yes, you heard right), mySQL has become the database management system of choice for many small and large solutions (Yahoo has been known to use mySQL).

Oh, one more thing. If the Linux box you have access to is not your own, do not attempt to follow this tutorial. You will need to have root access to the machine in order to install mySQL. Be forewarned trying to install mySQL on a machine that you do not have permission to do so on MAY get you in to some trouble with some administrators. I doubt you want to do that.

Otherwise, have fun. If you want just the down and dirty install commands, without all of my witty comments and explanations, scroll to the bottom.

Step 1 - Ingredients

First of all you need to download a copy of mySQL, it can be found at:

www.mysql.com/downloads/

I would recommend downloading whichever version of MySQL is the "Stable Release". Typically it says "Stable Release (Recommended)" right next to the version number. How convenient!

On the download page there are several different download options. Let me make them easier for you. Look underneath the "Standard binary (tarball) distributions" list. At this point I am going to assume that you are running some flavor of Linux, it doesn't matter which one.

Find "Linux (Intel libc6 systems) [pc-linux-gnu-i686]". This is the file that you want to download. If you are running an Alpha or Sparc system, you will need to get the version of the software for that specific chip. Most of us will have Intel-compatible chips (AMD, Intel, IBM, etc...).

Click on the proper version of Linux and download it into your home directory.

Step 2 - Unpacking

Now you have a file called:

mysql-3.xx.xx-pc-linux-gnu-i686.tar.gz

(note that xx in the filename generally stands for any number, since I don't know when you are reading this and what version you have downloaded.)

This file is tar/gzipped. This means that a group of files was first gathered into a .tar file (tar files are not compressed). Someone then used the gzip program to further compress this tar file to make it smaller to download. Tar and Gzip should be on your machine by default.

To unpack this file into a folder, generally the tar will make it's own folder, use the following command in your shell:

tar -xvzf mysql-3.xx.xx-pc-linux-gnu-i686.tar.gz

Your filename may vary. This should unpack mySQL into a folder called something like "mysql-3.xx.xx".

Step 3 - Adding a User for mySQL

In order for mysql to run properly, the mysql daemon (mysqld) has to have it's own user and group settings.

Here are the commands that you need to execute as root:

"groupadd mysql"
"useradd -g mysql mysql"

The first command "groupadd mysql" creates a new group on the machine called "mysql". The second command "useradd -g mysql mysql" creates a user "mysql" in the group "mysql". The -g mysql paramater tells the useradd function in which group to add the new user.

Step 4 - Configuring mySQL for your machine

This step may sound hard, but it really isn't. mySQL has a set of requirements that it must have or be able to find in order to run on your system. Typically every program that you install under linux will include a configure script. This script scans your system for the required files and makes sure that you will be able to compile the program properly.

Okay, go back to your shell and go into the mysql folder you just created in step 2:

"cd mysql-3.xx.xx"

You will need to now figure out where you want to install mySQL. The default location is /usr/local/mysql You can put it wherever you want, but you need to specify that information (called the prefix) when you execute your configure script.

To run the configure program for mySQL type:

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

After executing the configure script, a whole bunch of jibberish should appear on the screen. As long as there are no errors, you should proceed to the next step. If there are any errors, you should be able to figure out what your system is missing or needs upgraded by the output on the screen.

Step 5 - Compiling mySQL

After you have configured mySQL for your system, you are all ready to compile the program. To do this, we use the make command. Make uses the makefile that was created with the configure script to compile all of the source code for mySQL into a program that we can use. Once we have compiled the program, we tell make to install it also.

First you want to execute the command:

"make"

within your mySQL folder. When make has completed without any errors, you should then run:

"make install"

to go ahead and install mySQL. You are not done, however. You still need to install your grant tables and configure ownership of your mySQL files

Step 6 - Installing Default Databases (Grant Tables)

Grant tables are what mySQL uses to give and restrict users access to specific databases. This step will create these tables for you with a script.

From your mySQL folder, execute:

"scripts/mysql_install_db"

Once you have installed your grant table databases, it is time to give mysql access to it's files

Step 6 - mySQL Owns Itself

In order for mySQL to make changes to the folders that were created for it when you installed, you must give the proper permissions to the mysql user and group. You also want to make sure that root is the only user who owns the mysql directory.

Using the prefix that you used in step 4, execute the following commands. I am using the default prefix:

"chown -R root /usr/local/mysql"
"chown -R mysql /usr/local/mysql/var"
"chgrp -R mysql /usr/local/mysql"

The first command changed ownership of /usr/local/mysql and all subfolders and files (hence the -R for recursive).

The second command gave the mysql user ownership of /usr/local/mysql/var and all of it's subfolders and files (-R again). Var is where mySQL stores all of your database information.

The last command gave the mysql group, of which the mysql user is a part of, permission to access all files within the /usr/local/mysql folder.

Now we can finish our installation and make mySQL a permanent part of our Linux system!

Step 7 - Starting up mySQL

To start up mySQL you can type the command:

"mysql.server start"

To stop the mySQL server, you type the command:

"mysql.server stop"

Mysql.server is a script, so you may have to find it on your system. You can do this with the following command:

"cd \"
"find -iname mysql.server"

That should tell you where the file is.

This is all well and good, but only if we want to start the server every time we turn our computer on. Like most Linux users, you probably don't do that very often. I can tell you from experience that it is no fun to spend 3 hours troubleshooting your script problems when the problem stems from the fact that your SQL server didn't startup on boot.

Step 8 - Adding mySQL to System Startup

In order to start up the server automatically, you need to copy the file 'mysql.server' out of the 'support-files' folder of your mysql directory.

This file needs to go in the folder where your system keeps its startup files.

Your mileage may vary, and you may need to ask around to find out where that location is on your specific machine. Once you do, I am sure that you will appreciate the beauty of mySQL as so many of us have already.

The last step will just be an overview of the commands that need to be run in order to install mySQL, for all of those people too lazy to read this tutorial.

Step 9 - Lazy People Look Here

  1. Download mysql from http://www.mysql.com/downloads/ Get whatever stable release is available for your system.
  2. Open a shell window, and su to root.
  3. Go to the folder that has your mysql-3.xx.xx.*.tar.gz
  4. Execute the following commands.
    "groupadd mysql"
    "useradd -g mysql mysql"
    "tar -xvzf mysql-3.xx.xx.*.tar.gz" (whatever your file is named)
    "cd mysql-3.xx.xx" (whatever your folder is named)
    "./configure --prefix=/usr/local/mysql"
    "make"
    "make install"
    "scripts/mysql_install_db"
    "chown -R root /usr/local/mysql"
    "chown -R mysql /usr/local/mysql/var"
    "chgrp -R mysql /usr/local/mysql"

    That will install mySQL. For information on starting and stopping the mySQL daemon, look in steps 8 and 9.

Thank you for taking the time to go through this tutorial. I hope that it helped you out. If you have any reasonable questions, or if you have found any mistakes, please email me at scott@ruttencutter.com

Thanks,

-Scott

Comments

How about Winn2000 users?

December 19, 2001 by Helle M

I really don't know much about MySQL, but would like to also read about how you do this using Win2000 ?

Helle:-)

RE: How about Winn2000 users?

March 18, 2002 by Plamen Jelezov

Well, actually you will have no problems with the MySQL itself, but there are few bugs in the W2K OS. It took me about 2 weeks to make it work. Now it's perfect. Works very well with the OS and the PWS. I hate heavy things and that'a why removed the IIS. If you decide to start working with it, just mail me back and I'll send you full dscription, how to do it!

pj - pj@buldata.com

RE: RE: How about Winn2000 users?

March 20, 2002 by Stefan Gomez

I am extremely new to php and mysql. I am used to plain old access databases. My web host only accepts mysql databases. After going through the endless pages of setting up mysql, I have yet to find information on how to create mysql databases. If I got pass this, I'm sure I'd be using php with a mysql database in no time. I've managed to create a database on my host, and I've downloaded something to edit it. But I'm still lost on how to create the database. And, it'd be much easier for me to just convert my access databases to mysql. How would I do this, and how would I transfer it to the server? the only thing I can see so far is that I would have to re enter everything into the mysql server? Can't it just be as easy as uploading my database, I wish it could be. If anyone would be willing to help, I'd be very grateful,

Thanks,

Stefan

RE: How about Winn2000 users?

April 17, 2002 by Cheryl Floyed

I only read this last night but it may help... I read that you can export to MySQL from Access. I also read that you can use a free utility called DataTools... Again, don't hold me to this because I've only read it but it is a starting place.

See all 7 Comments

You must me logged in to write a comment.