Autostart MySQL on Mac

See http://hivelogic.com/articles/2007/11/installing-mysql-on-mac-os-x

First install MySQL.

Auto-Starting MySQL#

Now that the install is done, you need to have MySQL auto-start every time you start or reboot your Mac. The easiest way to do this is using launchd.

I’ve prepared a launchd plist file that will manage MySQL, starting it at boot and stopping it cleanly at shutdown. Create a file named com.mysql.mysqld.plist using the text-editor of your choice, and save it to your Desktop. Enter the following text into the file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.mysql.mysqld</string>
    <key>Program</key>
    <string>/usr/local/mysql/bin/mysqld_safe</string>
    <key>RunAtLoad</key>
    <true/>
    <key>UserName</key>
    <string>mysql</string>
    <key>WorkingDirectory</key>
    <string>/usr/local/mysql</string>
</dict>
</plist>

Now we need to move the file into place and set the permissions on it. You may be prompted for your password again:

sudo mv ~/Desktop/com.mysql.mysqld.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist

With the file in place, the last step is to tell launchd to load and startup MySQL. You may be prompted for your password again:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

If things go well, you won’t see anything special happen, but MySQL will have started up. You can verify this, again back in Terminal:

mysql -uroot

This will initiate MySQL’s command-line monitor. If everything went well, you should see something like this:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql&gt;

If you see that, that’s it, you’re done! Type exit to quit the MySQL monitor.

If you see something else, verify that your paths are set correctly and try the command again. If things still don’t work, it’s likely that something didn’t work and the compile didn’t finish. Try going through the steps once more and see if you can catch any error messages.

Starting and Stopping MySQL Manually#

If you ever want to stop MySQL manually, use this command in Terminal, entering you password when prompted:

sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist

To start it manually, use this command in Terminal, entering you password when prompted:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

A Note about Security#

The easiest way to secure your MySQL installation without affecting the way you (or your applications) will need to communicate with it is to limit anything but local connections to your MySQL server. In other words, only you and the apps you run on your own Mac will be able to connect. You won’t need to enter passwords when interacting with MySQL locally, and won’t need to tweak the default database.yml files that Rails creates, for example.

We can limit access by creating (or editing) the /etc/my.cnf file. If you have TextMate installed, you can enter the following command to create (or edit) the file:

mate /etc/my.cnf

The handy bit about using TextMate (or BBEdit) for this task is that it will handle authentication and setting permissions for you.

Enter the following text into the file save it and close it, authenticating as needed:

[mysqld]
bind-address = 127.0.0.1

Baking-In the MySQL Bindings#

You can gain some bigtime Rails-to-MySQL speed improvements by building the MySQL C bindings for Ruby.

If you have an Intel Mac, just run the following command (entering your password when prompted):

sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

You’ll see a prompt asking you which gem to install:

Select which gem to install for your platform (universal-darwin9.0)
 1. mysql 2.7.3 (mswin32)
 2. mysql 2.7.1 (mswin32)
 3. mysql 2.7 (ruby)
 4. mysql 2.6 (ruby)
 5. Skip this gem
 6. Cancel installation

Pick the option closest to the top that ends in “(ruby)”. In the example above, we’d want to select option 3.

Uninstalling MySQL#

In case you one day decide that you’d like to remove MySQL, it’s easy to do when building from source:

cd ~/src/mysql-5.0.45
sudo make uninstall
sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist
sudo rm /Library/LaunchDaemons/com.mysql.mysqld.plist

That’s It.


MySQL - Mac - Fixme