Ubuntu Feisty - MySQL and RoR

In the previous Ubuntu Feisty articles (page 1 and page 2) we secured, updated and personalised the install ready to add some working software.

As such, we can now install MySQL and Ruby on Rails with subversion and Postfix support.


MySQL install

We'll start by installing MySQL and then optimise it for use with a Slice.

Some optimisation is necessary as the default configuration uses a fair bit of memory and there is no need to 'waste' memory like that.

sudo aptitude install mysql-server mysql-client libmysqlclient15-dev libmysql-ruby -y

Password

MySQL installs with a blank password for the root user (don't confuse this 'root' user with your Slice root user - this is the MySQL user that has administration rights to the databases, not to your system).

Let's add a password:

mysqladmin -u root password YOURMYSQLPASSWORD

Optimisation

It's difficult to get into optimisation in this article but, for those who want to know more, there is a good article here: mysql performance.

So now let's optimise mysql for your Slice. Open the mysql configuration file:

sudo nano /etc/mysql/my.cnf

We're going to add two lines in the basic setting section. Find the first two lines shown below and add the two lines after that:

language  = /usr/share/mysql/english
skip-external-locking
skip-locking
skip-innodb

NOTE: If you intend to use innodb databases then don't include the skip-innodb line!

Next, find the Fine Tuning section and adjust/add the settings as shown below:

# * Fine Tuning
#
key_buffer    = 16M
max_allowed_packet  = 1M
thread_stack    = 64K
thread_cache_size = 4
sort_buffer=64K
net_buffer_length=2K

Memory check

Before we restart mysql, let's have a quick look at how much memory is being used with the original configuration. Enter the command 'top':

top

Then holding the 'Shift' button down, press 'm'. This will arrange the output of top in order of memory usage. You will see something like this:

PID   USER   PR  NI  VIRT  RES  SHR  S  %CPU %MEM    TIME+  COMMAND
3490 mysql  16   0   ----    ----    ----  --      0        9.0     0:00.14   mysqld

On this 256MB test slice, mysql is using 9% of the memory (I've removed some of the other figures so the output is easier to interpret).

Restart MySQL

Exit top (press 'q') and restart mysql:

sudo /etc/init.d/mysql restart

Now look at top again. My output is now:

PID   USER   PR  NI  VIRT  RES  SHR  S  %CPU %MEM    TIME+  COMMAND
3673 mysql  15   0   ----    ----   ----   --      0         2.2     0:00.02   mysqld

Memory use is now just 2.2%. When memory is a factor, such as when using a small Slice, optimising any install can be a very good idea.

The small optimisation shown above does more than just reduce initial memory use, it increases the efficiency of MySQL as well. More articles on MySQL soon...

Ruby on Rails install

Now we'll install ruby from the apt repositories and then installing rubygems via source code:

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby -y

Symlink

We need to create some symlinks from the install to locations every programme would look. Each line below is a separate command:

sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

Rubygems

Let's go ahead and download the source code into our sources directory. If you haven't got a sources directory, you can simply create one:

mkdir ~/sources
cd ~/sources

Then unpack it and move into the newly created folder:

wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
tar xzvf rubygems-1.0.1.tgz
cd  rubygems-1.0.1

Now we can go through the simple process of compiling it and creating a symlink:

sudo ruby setup.rb
...
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

Version

Once done, we can check the gem version with a:

gem -v
...
1.0.1

Excellent. Up to date.

Update

Whenever you need to update the installation just enter the following:

sudo gem update
sudo gem update --system

Rails

Now we can install rails with the command:

sudo gem install rails --include-dependencies

The basic Ruby on Rails install is now finished. Let's extend the install with imagemagick and a quick ruby test as follows:

sudo aptitude install imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core -y

Testing

Now we can do a test to see if the install works (thanks to brainspl.at for this idea).

irb
#irb(main):001:0>
require 'RMagick'
#=> true
#irb(main):002:0>
require 'mysql'
#=> true
#irb(main):003:0>
exit

Obviously, if the output does not return 'true' for each 'require' command, then something has gone wrong. If the result is correct and returns 'true' then we have a working ruby install.

Postfix and subversion

Finally, we need to install postfix (if we don't do this now, other programmes will install exim and all sorts of unneeded email help). Once done we can install subversion so we can 'check-out' plugins and other goodies for our Rails applications:

sudo aptitude install postfix subversion -y

Answer the postfix questions as you see fit but the defaults usually suffice for our purposes as we are only using it to send emails and not for receiving mail.

That's it

You now have a MySQL, Ruby on Rails and postfix/subversion stack setup and ready to roll.

Now you are ready to install the server of your choice whether that be Litespeed, Nginx or Apache.

PickledOnion.

Article Comments:

David Laing commented Thu Nov 01 11:47:21 UTC 2007:

Following these instructions threw an error with the test:

require 'mysql'

I had to apt-get install libmysql-ruby

to install the ruby mysql bindings.

PickledOnion commented Thu Nov 01 12:00:46 UTC 2007:

Hi David,

Not sure I understand, the command to install libmysql-ruby is the first thing you do on this page (see the first code block).

Did it not install for you? Have you updated the sources.list?

PickledOnion.

George Anderson commented Wed Nov 07 05:23:23 UTC 2007:

Will these instructions work for Gutsy too?

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)