Installing MySQL on Mac OS X Leopard using MacPorts

There comes a time in every Rails developers life that they need to work with MySQL. Often this means that your development comes to a screeching halt as you wade through the various options for getting MySQL installed on your Mac.

This guide won’t help you install it anywhere else… because I am a zealot and I don’t care how to install it anywhere else.

Install MySQL using macports, go get a coffee while this runs its course.

1
sudo port install mysql5 +server

Next up, create the initial databases used by MySQL.

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

Configure MySQL to start at system start up.

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

Start MySQL.

1
sudo /opt/local/bin/mysqld_safe5 &

You can always stop the service manually as well:

1
mysqladmin5 -u root -p shutdown

Confirm that that MySQL is running.

1
mysqladmin5 -u root -p ping

Just hit enter at the password prompt, as it is blank. We are about to change that.

Secure your server.

1
mysqladmin5 -u root password [your password goes here]

At this point you should have your MySQL server up and running. However, your Rails project may complain about not being able to find the socket file. You can find that using the ‘status’ command.

First log into MySQL.

1
mysql5 -u root -p

At the prompt, run the command.

1
status;

In the output you will see an entry listing the socket being used by MySQL. You can use this in the database.yml file.

And finally, I really don’t like typing that ‘5’ after all the commands… I can’t explain it, it just upsets me. So I like to create a symbolic link for the mysql5 and mysqladmin5 commands.

1
2
sudo ln -s /opt/local/bin/mysql5 /opt/local/bin/mysql
sudo ln -s /opt/local/bin/mysqladmin5 /opt/local/bin/mysqladmin

That should get you coding again against a MySQL database!

Running Ruby on Rails with Mongrel Clustering and Apache Proxy

Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we’ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy.

Here’s the deal, I am going to install all the necessary applications to get this up and running, so if you have some of these installed on your server already you may just skip ahead, however, if you have any issues with this you may want to read over those sections you skipped and understand why I installed a specific version or what pitfalls I encountered that may be affecting your setup. Also, I am running Ubuntu, so if you are running a different distribution, then the paths to specific configuration files on your system may be different. Let’s get started!

Installing MySQL

Install MySQL using the package manager in Ubuntu:

1
sudo apt-get install mysql-server

After the installation you should be able to run the command mysql or mysqladmin. If you cannot run these then you may have to create symbolic links to these commands or add them to your path.

By default, MySQL only listens on localhost, in order to change that you need to comment out the line which reads: bind-address = 127.0.0.1

Change this by editing the following file on your system:

1
sudo vi /etc/mysql/my.cnf

You can search for text in vi using a forward slash.

You should restart MySQL after making a change to the config file:

1
/etc/init.d/mysql restart

Now verify that MySQL is listening to the network. The output of the following command should show an entry for MySQL:

1
netstat -tap

You need to set a password for the root account of MySQL, otherwise anyone can access it!

1
2
mysqladmin -u root password your_password
mysqladmin -h server_name -u root password your_password

In case it is not clear, your_password should be replaced with a strong password of your choosing.

Installing Ruby

1
apt-get install ruby ri irb

Install Ruby Gems

1
2
3
4
5
cd /tmp
wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar -zxvf rubygems-0.9.2.tgz
cd /rubygems-0.9.2.tgz
ruby setup.rb

Update rubygems

1
2
3
gem update --system
gem update
gem cleanup

Why the two calls to gem update? –system will update the RubyGems itself and without that parameter you will only be updating the gems.

Install Rails

Install Rails using Rubygems:

1
gem install rails

Now let’s test the rails installation:

1
2
3
rails /tmp/railstest
cd /tmp/railstest
./script/server

This will start the default WeBrick web server, so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen. Once you are finished staring at your success, press CTRL + d to kill the web server.

Install Mongrel

Install the build tools:

1
apt-get install build-essential ruby1.8-dev

Install Mongrel:

1
gem install mongrel mongrel_cluster --include-dependencies

Now let’s test the Mongrel installation:

1
2
cd /tmp/railstest
mongrel_rails start

This will start the Mongrel web server (now default), so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen… again. Now kill the web server.

Configure Mongrel Rails cluster

Add a user for our mongrel cluster:

1
useradd mongrel

Add the group for our mongrel user:

1
groupadd mongrel

Configure the directory security for your application:

1
chown -R mongrel:mongrel /tmp/railstest

Now we need to configure the Rails cluster for the test application. This command will write a YAML file to the config directory in your Rails application so you will only have to enter all these parameters once. After that, simply firing the command mongrel_rails cluster::start will use this configuration:

1
2
3
cd /tmp/railstest
mongrel_rails cluster::configure -e production 
-p 8000 -N 8 --user mongrel --group mongrel

You can reconfigure by running this command and using different parameters, you can also safely delete the YAML file from your config directory to blow these settings away.

Time to start the cluster:

1
2
cd /tmp/railstest
mongrel_rails cluster::start

You should be able to test your application by going to http://localhost:8000. You could also hit any of the 8 ports you have configured.

Install Apache

We have come to the last part of this lengthy installation. Apache. Now, it must be understood that you MUST install Apache 2.2 or higher. The reason for this is the Apache Proxy Balance Module. This module is required to glue all this together and is ONLY available in Apache 2.2 or higher. I am going to build from source on this one because the current version available to me in the apt-get library is not the sufficient version required.

Download Apache, unpack it, and then change to the directory which contains the unpacked files.

1
2
3
wget http://archive.apache.org/dist/httpd/httpd-2.2.3.tar.gz
tar xvfz httpd-2.2.3.tar.gz
cd httpd-2.2.3

Configure the source tree for compiling Apache on your system:

1
2
3
4
5
6
./configure --prefix=/usr/local/apache2 
--enable-mods-shared=all 
--disable-deflate 
--enable-proxy 
--enable-proxy-balancer 
--enable-proxy-http

Build and install using the following commands:

1
2
make
sudo make install

Start Apache and navigate to your server through a browser. You should see the default Apache homepage.

1
sudo /usr/local/apache2/bin/apachectl start

There are several options for configuring and securing Apache that should be considered before taking this setup into production.

In order to configure Apache to serve your Mongrel cluster you will need to add the following to the Apache conf file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
BalancerMember http://127.0.0.1:8003
BalancerMember http://127.0.0.1:8004
BalancerMember http://127.0.0.1:8005
BalancerMember http://127.0.0.1:8006
BalancerMember http://127.0.0.1:8007
</Proxy>
 
<VirtualHost yourdomain.com:80>
ServerAdmin yourname@yourdomain.com
ServerName  www.yourdomain.com
ServerAlias yourdomain.com
 
# Indexes + Directory Root.
DirectoryIndex test.html
DocumentRoot /tmp/railstest/public/
 
<Directory "/tmp/railstest/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
 
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://www. yourdomain.com$1 [R=301,L]
 
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
 
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
 
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
 
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
 
# Logfiles
ErrorLog  /tmp/railstest/log/error.log
CustomLog /tmp/railstest/access.log combined
</VirtualHost>
 
NameVirtualHost yourdomain.com:80

Now, there are 3 pieces to take note of here. First, you need to create a Proxy group for the mongrel cluster we created. Since we created 8 nodes, there are 8 BalancerMembers. Second, you will need a VirtualHost entry for your Rails site (in this case the railstest site we created earlier). And Finally, you have to add the NameVirtualHost entry for your VirtualHost (I can’t tell you how many times I forget that).

That should get you going with a Mongrel Cluster behind Apache Proxy. If you want to setup more than one site, then just create 3 more entries in your Apache conf file. You may want to think about separating the declarations from the main httpd.conf file, but for this example I just wanted to get you going. Have Fun!