MySQL is one of the most popular database servers, available for all flavors of Linux, Windows, and now Mac OS X. Due to its immense popularity, you'll often see Web hosts and ISPs offering it as one of the default features for new accounts. It's a good database to get your feet wet with.
One of the drawbacks to its popularity, however, is the number of ways you can install it. There are various double-clickable packages available, as well as various ways to install and compile from source. We'll be focusing on the two I found most arousing:
Before we go much further, we have to create a MySQL user. This is the account that our MySQL server will run itself as. (Whereas you may be the
morbus user, and the administrative account would be the
root user, our MySQL server will run as the
Creating this account is rather simple:
- Click on your Apple menu.
- Choose System Preferences.
- Click the Users preference panel.
- Click New User.
- For Name, enter "MySQL User".
- For Short Name, enter "mysql".
The Login Picture and Password can be anything you wish. Once you've got all the information filled in, click the OK button, and you'll see the "MySQL User" entry in the User list. This step is required for both the double-clickable install and compiling the source code--don't skip it.
Installing the MySQL package
|Ah, so many things to discuss. . . . One of the topics we're interested in is what you would like to see next in this series.|
We're ready to move on with our double-clickable install, so download the MySQL 3.23.49 package from Marc's site. Once the download is complete, the install is much like any other OS X package; you'll need an administrative password, and a few button clicks later, the installer will be finished.
That's the extent of installing MySQL in package form--nothing fancy, really. There are a few more steps to configuring a properly working MySQL database, but since they're needed for both the packaged and compiled versions, we'll get to them after the next section. You can skim on down to "Post-installation wrap up."
Compiling MySQL from source
Compiling MySQL from source is relatively easy. In the next few steps, we'll create and install the MySQL database with the same configuration as the one available in the prepackaged form above. To compile MySQL, you'll need access to your Terminal and an administrative password. Also, the latest OS X Developer Tools should be installed.
Note: If you've installed the packaged version of MySQL (above), you do not need to do anything in this section.
The first thing, obviously, is to get the source code itself. You can find the closest mirror on the MySQL site. Once the download is complete, get into your Terminal and create and move into the
cd /usr/local/ mkdir src cd src
When you're installing something new on a Linux-like system (such as Mac OS X), you really should keep most of your efforts and work environment centered around the
/usr/local/ directory. This helps give a distinctive separation between software installed by the operating system and software installed by you. When you install the MySQL package (above), for example, it installs everything into
/usr/local/mysql-3.23.49/, including an alias (called a "symlink" in Linux terms) from
Next, we decompress our downloaded file (your path and filename will be different):
tar -zxvf /Users/morbus/Desktop/mysql.gz
We then move into the new directory:
At this point, you're going to use three commands that are very common when compiling source code. The first command is
configure and, funnily enough, it creates a configuration file that is used by the other two commands. In most cases, this is where you choose how you want your program to act, where you want it installed, etc.
configure line for MySQL is simply:
./configure --mandir=/usr/local/share/man --with-innodb
This line gives us an example of two things we can do with a
configure statement. The first option,
--mandir=/usr/local/share/man, shows an example of how you can override a built-in default. In this case, we're saying, "No, MySQL, don't install the documentation in the normal directory; install it over in this one instead."
The second option,
--with-innodb, is an example of turning a feature on--one that normally is not. In this case, InnoDB is a way of adding foreign keys, transactions, and more to MySQL. (These are some of the things that purportedly make a "man's database," as mentioned in our introduction.) It's outside the scope of this article to get into what all this really means; if you're interested, you can check out InnoDB.com for more information.
After you run the above command line, you'll see a decent amount of output, most of which probably won't make sense. That's OK, though;
configure scripts often check your "build environment" to make sure they know everything they need to do before you actually compile the source code. In essence, they're taking all the guesswork out of the eventual compilation. As long as there are no glaring errors (there shouldn't be), you can move on.
The next step is the actual compilation phase. This is where you take the source code you've
configure'd, and turn it into an executable program for OS X. To do this, simply enter the following:
make will take a look at the configuration you created (using that
configure command) and go about creating a custom installation based on your whims and desires. Often, this can take minutes; it can also take seconds. (On my Dual 450 G4, it took a good eight or nine minutes, with three or four other programs open.) Either way, you're going to see a lot of stuff saunter by on your screen. You don't have to worry about reading or understanding it all . . . this is the art of a compile-in-progress.
Be careful that you don't get confused by the concepts of "compiling" and "installing." Just because we're compiling our source code with
make, there's no guarantee that we can use it to conquer the world. Our last command in our trio-of-temerity handles that aspect:
sudo make install
This command simply takes all of the compiled code from our
make and installs it in the places we've requested (said places being overridable using the
configure command, if you recall). After you run
make install, the code you've compiled is ready for your use. You can either begin using the program right away, or you can continue tweaking extra settings.
In MySQL's case, there are a few more commands we need to run--basic steps that ensure a properly running MySQL. Read on, stalwart traveler!
Post-installation wrap up
Depending on how you installed MySQL (either as a package or by compiling the source code), certain files will be in different places under the
/usr/local/ directory. This is normal and is covered in the install documentation, which I've excerpted below.
In the case of the package installation, your directory layout is shown below, with
/usr/local/mysql/ being a symlink to
/usr/local/mysql-3.23.49/. (Note, however, that the client programs and server are also installed in
*Directory* *Contents of directory* `/usr/local/mysql-3.23.49/bin' Client programs and the server `/usr/local/mysql-3.23.49/data' Log files, databases `/usr/local/mysql-3.23.49/include' Include (header) files `/usr/local/mysql-3.23.49/lib' Libraries `/usr/local/mysql-3.23.49/scripts' `mysql_install_db' `/usr/local/mysql-3.23.49/share/mysql' Error message files `/usr/local/mysql-3.23.49/sql-bench' Benchmarks
If you compiled from source, your directory structure becomes:
*Directory* *Contents of directory* `/usr/local/bin' Client programs and scripts `/usr/local/include/mysql' Include (header) files `/usr/local/info' Documentation in Info format `/usr/local/lib/mysql' Libraries `/usr/local/libexec' The `mysqld' server `/usr/local/share/mysql' Error message files `/usr/local/sql-bench' Benchmarks and `crash-me' test `/usr/local/var' Databases and log files
With the above hierarchy, your final steps are within walking distance:
# for package installations cd /usr/local/mysql/ sudo ./scripts/mysql_install_db sudo chown -R mysql /usr/local/mysql sudo ./bin/safe_mysqld --user=mysql & # for source installations cd /usr/local/ sudo ./bin/mysql_install_db sudo chown -R mysql /usr/local/var sudo ./bin/safe_mysqld --user=mysql &
The above orders will initialize the core MySQL database (which takes care of access control), as well as start the MySQL server in the background. If everything goes smoothly, you should see something similar to this (the pathname is based on which install you chose earlier):
Starting mysqld daemon with databases from /usr/local/var