MySQL and the Macintosh
18 December, 2008 | Filed under “Apache on the Macintosh”
Note: The procedure herein described installs a MySQL 5.1.30 database on a 64-bit PowerPC Macintosh client running Mac OS X 10.5.5 (Leopard). The procedure may, however, be followed for different configurations. My main interest is to use the database in conjunction with a core version of the ExpressionEngine content management system (CMS) from EllisLab, both running locally on one of my own computers. MySQL was the natural “next step” in building a local Apache server and this is the third in a series of articles; the first article took care of the actual Apache installation while the second article set the Apache to work with PHP. CMSs generally rely on PHP and MySQL to build their web applications and ExpressionEngine specifically requires both.
MySQL – and yours, too
1. From Sun Microsystems’ MySQL downloads page, choose the “MySQL Community Server” download link. As of this writing, the latest stable version of MySQL is 5.1.30.
2. Download the correct version for your hardware and software. You are given a choice of “package format” and “TAR packages”. The former (which I prefer) is delivered as a dmg file; familiar to everyone who has previously installed software on Mac OS X. In the discussion below, I will presume that you opt for this more convenient package format.
Note: If you are uncertain of your system configuration, choose “About This Mac” under the Apple in Finder’s menu to see which version of Mac OS X you are running. Choose “More Info…” if you need information on your make of Macintosh. 32-bit? 64-bit? Still uncertain or in a nostalgic mood? Consult Ian Page’s Mactracker application and chime away! Know, however, that the PowerMac G5 was the first 64-bit consumer-level desktop computer ever sold. It was introduced in June of 2003. So, if you have a G4 computer, it is 32-bit. If you have a G3 computer, it is blue.
Important: If your system already has a MySQL instance up and running, shut it down before you proceed with a new installation. At this point it may be a good idea to read Sun’s article Installing MySQL on Mac OS X.
3. Double-click the dmg file icon to open the package.
4. Read the ReadMe.txt file in the package. The instructions more or less repeat what you (should have) read on Sun’s web site.
5. Double-click the pkg file for your system (e.g., mysql-5.1.30-osx10.5-powerpc-64bit.pkg) to start the installation. You will be guided through the necessary steps – only make sure you choose the correct install location should you have more than one operating system available.
6. To automate your system, follow this instruction from the ReadMe-file: “If you want MySQL to start automatically during system startup, you also need to install the MySQL Startup Item. It is part of the Mac OS X installation disk images as a separate installation package. Simply double-click the MySQLStartupItem.pkg icon and follow the instructions to install it. The Startup Item need be installed only once. There is no need to install it each time you upgrade the MySQL package later.”
Note: The startup item installation adds the variable MYSQLCOM=-YES- to the hostconfig system configuration file. Please observe that Apple has stamped this file with a This file is going away warning. Up ahead, we can expect to see some changes in this area.
7. If you are comfortable with the Terminal window, you will probably start and stop MySQL with sudo commands. For the rest of us, the world became a better place when Sun gave us a preferences pane. Once and for all, install MySQL.prefPane, from the package, here:
X > Library > PreferencePanes> MySQL.prefPane
(where X represents the name of your main hard disk/partition). Whenever you need to start or stop the database, do so from the Finder menu:
Apple > System Preferences… > Other > MySQL

The MySQL preferences pane.
Note: That said and taken care of, it might, however, come in handy at some time to know how to start and stop the database server from the Terminal. If you ever need information on this, you will find it tucked away at the end of this article.
8. Back to work: Unmount the installation package by throwing its drive icon to the trash, then delete the original dmg file which is no longer needed.
Database passwords
9. With PHP installed and the MySQL server operational, it is important to issue passwords to users. Out of the box, the initial MySQL user accounts lack passwords until assigned one. In the following you will assign a root password for MySQL.
Important: ‘root’ as used in this setting, should not to be confused with UNIX root.
Now, believe me: I shy away from the Terminal and UNIX command lines whenever I can, telling myself it is a normal, human reaction. After all, Apple gave us the graphical user interface for a reason. But at some point I have to bow down before the king. For what we are about to do, the Terminal rules.
9a. Fire up the Terminal application and enter the following:
sudo /usr/local/mysql/bin/mysql -u root
9b. When promted for a password, enter your Mac OS X administrator password.
9c. At the mysql> prompt, enter:
UPDATE mysql.user SET Password = PASSWORD('NewPassword') WHERE User = 'root';
where in place of NewPassword you supply your own password.
9d. At the next mysql> prompt, enter:
FLUSH PRIVILEGES;
9e. At the third mysql> prompt, disconnect from MySQL with:
quit
Preparing MySQL for PHP passwords
10. The default installation of MySQL should now be configured to recognize the format for PHP-compatible passwords. According to Kevin Yank in his book Build your own database driven website using PHP & MySQL, all it takes is a plain text document with the following two lines of code:
[mysqld]
old-passwords
Save the document temporarily to, e.g., the Desktop with the name:
my.cnf
The cnf suffix is important here! This file stores global options (aka configurations) for MySQL programs.
But why temporarily? Because the file is expected to reside in one of those folders which are normally hidden from the casual user. In Finder:
Go > Go to Folder…
and enter the following destination:
/etc/
Now, drag the cnf file into this folder, let go of the mouse, and authenticate with your administrator password when prompted.
Note: If you read Kevin Yank’s book, you may be tempted to copy the configuration file to the data folder of the MySQL installation – unless you read the errata information later released on the publisher’s web site. It so happens that the MySQL team now specify a different path for my.cnf.
11. Restart the computer and that’s it!
Digression
The Internet is both a blessing and a curse. You start off looking for information on a specific subject – and end up reading about something quite off the track. Or close to it. Or parallel to it. Or … whatever. If you do not care a fiddler’s fart about password hashing, do not read James McGlinn’s article.
Want (eh, need) to start or stop MySQL from the Terminal?
The reference manual for the MySQL database system, mysql.info, is brimming with command examples. But take heed: The text document is a hefty 10 MB and may take some time and horsepower to open. You will find it (hidden away) here:
/usr/local/mysql/docs/
Let us, however, restrict this to simply starting and stopping the MySQL server.
To start the server
a. Fire up the Terminal.
b. Enter the following:
sudo /usr/local/mysql/bin/mysqld_safe
c. Type in your system administrator password (not the database password) if prompted for Password. And, by the way, you will not see the password as you type.
d. Suspend MySQL by hitting Ctrl-Z.
e. Switch MySQL to background execution with the following command:
bg
To shut the server down
a. Fire up the Terminal.
b. Enter the following:
sudo /usr/local/mysql/bin/mysqladmin -u root -p shutdown
Beware of the differences in c) and d):
c. If prompted for Password, type in your system administrator password.
d. Type in your database password when prompted for Enter password.
Important: You may have observerd a couple of “ifs” in the steps above – and wondered why you are not always required to enter an administrator password. This has everything to do with sudo. Whenever you enter a command starting with sudo, the system grants you a 5 minutes temporary access as a system root user. And as a root user you can do pretty much whatever you want – no passwords asked – and may wreak havoc if you do not take care. The Apple support article Enabling and using the “root” user in Mac OS X might give some insight into this.