Tutorial: MySQL on Mac OS X via MacPorts

This is a little tutorial on customizing the previous MySQL install found here.

I prefer to have my DB living inside /Users directory, instead of /var or /opt. That's what we are going to achive. The goal is to make MySQL DBs drop here /Users/DB/mysql.

To accomplish this, we need to edit the MySQL configuration file which can be found here /opt/local/etc/mysql5/my.cnf. Edit the file, and find the line where datadir is set, replace it with the desired path. In my case

datadir = /Users/DB/mysql

Save the changes, open up our beloved terminal and let's go to the command line. We will need to create the directories for MySQL and set the right permissions.

  • $ sudo mkdir -p DB/mysql - Create directories and subdirectories
  • $ sudo chown mysql:mysql DB/mysql - Set user mysql and group mysql as owners of DB/mysql directory
$ cd /Users/DB
$ sudo mkdir -p DB/mysql
$ sudo chown mysql:mysql DB/mysql

Create the initial database layout,

$ sudo -u mysql mysql_install_db5
Start MySQL,
$ sudo mysqld_safe5 &

Set a password for root user,

$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'mysecretpassword'

MacPorts installs symbolic links into /opt/local/bin pointing to /opt/local/lib/mysql5/bin, the problem is that it appends a "5", meaning MySQL version to each link. I don't want that, so let's add /opt/local/lib/mysql5/bin to our path, and make it availabe for future use.

$ export PATH=$PATH:/opt/local/lib/mysql5/bin
$ echo 'export PATH=$PATH:/opt/local/lib/mysql5/bin' >> ~/.profile

Tightening our MySQL install, the -p is for asking a password, since we already set it. This step is optional, answer the questions to suite your needs.

$ sudo  mysql_secure_installation -p

Creating convenience alias for starting and stopping MySQL.

alias startmysql='sudo mysqld_safe &'
alias stopmysql='mysqladmin -u root -p shutdown'
echo "alias startmysql='sudo mysqld_safe &'" >> ~/.profile
echo "alias stopmysql='mysqladmin -u root -p shutdown'" >> ~/.profile
Now we can use startmysql and stopmysql. Feel free to suggest, criticize or correct. Have Fun!

Taxonomy upgrade extras: