MacPorts - MySQL Installation

 

Most of the existing articles on installing MySQL on macports are out of date, due to a change in the ports. Thankfully installing MySQL is still pretty easy.

First, download and install Macports, be sure to read the whole article on installing it.

Several times in the article I mention using sudo, this will run the command succeeding it as root. The first time you use it, it will prompt you for your password, this is the same as your login password. You can of course do all of this as root, if you want, just be very careful as you can really screw your system up if you delete the wrong stuff. If you are working as root, you won't need to do sudo in front of anything.

  1. sudo su -

Next, install MySQL5; just open up terminal and do the following:

  1. sudo port install mysql5
  2. sudo port install mysql5-server

Create the initial MySQL databases.

  1. sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql

Copy the config file to the etc path.

  1. sudo cp /opt/local/share/mysql5/mysql/my-medium.cnf /opt/local/etc/mysql5/my.cnf

Add the path to your pid file, then save the file and quit TextEdit. You'll find socket keys in both the [client] and [mysqld] sections.

  1. socket = /opt/local/var/run/mysql5/mysql.pid

Next up, make sure the mysql user can access the path for that pid.

  1. sudo chown -R mysql /opt/local/var/run/mysql5/

Now, make sure the mysql user can get to the data directory, sometimes you get this error if you don't [ERROR] /opt/local/libexec/mysqld: Can't find file: './mysql/host.frm' (errno: 13).

  1. sudo chown -R mysql /opt/local/var/db/mysql5/mysql

Add a startup item for MySQL.

  1. sudo launchctl load -w /Library/LaunchDaemons org.macports.mysql5.plist

Another good idea recommended by 2TBSP is to create start and stop aliases. You don't need to use TextEdit here, you can use pico or vi if you know how, it doesn't really matter.

  1. /Applications/TextEdit.app/Contents/MacOS/TextEdit .profile

Add the following lines and close the file. Using mysqlstop will prompt you for your password, this is the database password which you setup in the mysql_install_db step, above.

  1. alias mysqlstart='sudo /opt/local/bin/mysqld_safe5 &'
  2. alias mysqlstop='/opt/local/bin/mysqladmin5 -u root -p shutdown'

That's pretty much it.

I did get some errors during my installation and have updated this guide to avoid those errors, if you have issues, the first thing to do is look at your log file, the name of the log file varies by the name of the machine. For instance, my computer's name is macpro, thus my error log is /opt/local/var/db/mysql5/macpro.local.err. To view the file, you can cat the file, then of course google the messages.

  1. cat /opt/local/var/db/mysql5/macpro.local.err

If you want to install PHP5, which you most likely will, you should copy a php.ini file and update it with the sockets.

  1. sudo port install php5
  2. sudo port install php5-mysql
  3. sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini
  4. sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /opt/local/etc/php5/php.ini

In the php.ini, you should search for mysql.default_socket, you'll find several instances they should all be set to the following:

  1. /opt/local/var/run/mysql5/mysql.pid

If you forget your root password, nixCraft has an article called Recover MySQL root password, which works great.