Installing MySQL on Mac OS X
This page is intended to document the installatin of MySQL 4.0.x on Mac OS X 10.3 (Panther). The steps for installation are documented in serveral places on the Internet with the best being Apple's devloper site at: http://developer.apple.com/internet/
However, as we all know, things never go exactly as planned and there are always a few minor differences between what someone has documented and what actually works. :-) So I'm writing up this tip to help me if I have have to install and configure MySQL again.
Getting the Software
The MySQL server software can be downloaded from http://www.mysql.com. For the binary installer, select the Installer package from the download page. This will download a disk image you can mount and run the install from.
In addition to the server software, MySQL AB (the developer) has been working on graphical administration tool for MySQL. They have Linux and Windows versions and just recently, someone has offered Mac OS X builds. As of this writing, you can get the Mac version of MySQL Control Center from http://www.pogma.com/fink/MySQLControlCenter-0.9.4.dmg. Be aware that this link could change.
The disk image includes a readme with details about the install and where files will be installed. Mount the disk image and run the installer. By default everything is installed in the
/usr/local/mysql directory. Once you have everthing installed, you need to perform some configuration steps to get everything working.
Setting up a MySQL User
The first step in setting up the software is to setup a special OS X user for the MySQL server to run as. With Panther, a user and a group for MySQL has already been created. So you don't need to do anything.
In previous versions of OS X you had to create this user manually. For the sake of completeness are the set of commands to create a MySQL user and group from the command line.sudo bash niutil -create / /groups/mysql niutil -createprop / /groups/mysql gid 401 niutil -create / /users/mysql niutil -createprop / /users/mysql gid 401 niutil -createprop / /users/mysql uid 401 exit
You can also create the account using the NetInfo Manager utility. The user and group ID do not have to be the same. For more details see the article on Apple's Internet Developer web site: http://developer.apple.com/internet/macosx/osdb.html
Next, you can set the password for the mysql account. To do this, become super user and run the
passwd command on the mysql account.
sudo bash passwd mysql exit
Set the Permissions for the MySQL Data Directory
By default, the data directory for MySQL is:
/usr/local/mysql/data. However, the installer assigns ownership to the
root user and the
wheel group. So you must login again as
root and reassign the ownership of this directory. Here are the commands:
sudo bash chgrp -R mysql /usr/local/mysql/data chown -R mysql /usr/local/mysql/data exit
Starting the Server
Whew! That's a lot of stuff to be in a position just to start a server.
Start the server using the
sudo command. You can either run the command from a prompt or put it in a shell script. This command will start the server in the background as the
sudo -u mysql -b /usr/local/mysql/bin/safe_mysqld
Setting the Root Password
Now that the server is up, you need to setup some security for the MySQL server. By default, MySQL sets up 2 users,
root with no password and a blank user account which allows you to access MySQL without any password. The first order of business is to set the
root password. To do this type:
mysqladmin -u root password 'password here'
The command line assumes that
mysqladmin is in your path which is probably not the case. You can precede the command with the PATH like this:
/usr/local/mysql/bin/mysqladmin. Or, add the
/usr/local/mysql/bin directory to your PATH environment variable. I would recommend doing the latter to save yourself some typing.
Testing to Make Sure Everything is Working
Now log into MySQL as
root. Enter the following command:
mysql -u root -p Enter Password:
Enter the password you set in the previous step. This puts you at a
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 to server version: 4.0.17-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
Note: Each MySQL command must be end with a semi-colon (;). Do not forget this as you enter in commands.
Next, make sure everything is working. To get a listing of all the databases in MySQL type the following:
mysql> show databases; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.00 sec) mysql>
You should see two databases as shown above.
Next, delete the blank user account so only the
root user, and any other users you add, can access MySQL. To do this type the following command at the MySQL prompt:
mysql> use mysql; Database changed mysql> delete from user where User=''; Query OK, 2 rows affected (0.34 sec) mysql> exit Bye
Once blank account is deleted, you must reload the server for the change to take effect. Type the following at a shell prompt to reload the server:
mysqladmin -u root -p reload Enter password:
Now when an unauthenticated user tries to access MySQL, the following response is returned:
$ mysql ERROR 1045: Access denied for user: 'user@localhost' (Using password: NO)
That is pretty much it. You should be ready to use your MySQL server and databases at this point.
Stopping the Server
One final point to cover is stopping the server. To do this enter the following command at a shell prompt:
mysqladmin -u root -p shutdown Enter password: $ 040215 21:18:37 mysqld ended
This stops the server and closes the databases.