« Just a quick Technorati intermezzo | Home | Setting up a Ruby on Rails server - Part 4 - Litespeed a web server »

Setting up a Ruby on Rails server - Part 3 - Installing MySQL

By Nick | March 30, 2007

Today, I’ll explain how you can install MySQL on a CentOS box. You can use the MySQL version that is already installed on CentOS, but it is old. On my system it was 4.1.20, but I wanted to use the 5 series. So first step is to remove the old one:

yum remove mysql

Add some a mysql user and group to run MySQL and make sure the mysql can’t log in:

groupadd mysql (probably already exists)
useradd -s /sbin/nologin -g mysql mysql

Get the MySQL source from the MySQL website and downloaded the latest version. That is probably the biggest issues with installing packages from source, you tend not to upgrade them once they are up and running and your site is in production. If it works, don’t fix it!

Install MySQL and change some file settings:

tar -zxvf mysql-standard-5.0.xx-linux-i686.tar.gz
mv mysql-standard-5.0.xx-linux-i686 /usr/local/mysql
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
chgrp -R mysql .

Check your MySQL installation:
1. /usr/local/mysql/bin/mysqladmin version
2. /usr/local/mysql/bin/mysql - start the mysql command utility

How to secure MySQL:

cp mysql-dir/support-files/my-large.cnf /usr/local/mysql/data/my.cnf
vi data/my.cnf

You have two possibilities:
1. bind-address = 127.0.0.1 #MySQL wont be accessible from the outside
2. uncomment skip-networking #only connections from localhost are allowed

Both options make sure that nobody from outside your machine is able to connect to your MySQL. You are still able to connect to it using SSH tunneling, I’ll probably explain this very handy option in one of my next posts.

Other security measures are:

You will probably have to restart your server at some point, so it would be handy if MySQL restarted automatically. Here is how you do it:

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --level 345 mysql on

Now the only thing left to do is to make Ruby talk to MySQL by installingen the Ruby MySQL bindings:

gem install mysql — –with-mysql-include=/usr/local/mysql/include/
–with-mysql-lib=/usr/local/mysql/lib/
–with-mysql-config=/usr/local/mysql/bin/mysql_config

I had to modify modify /usr/local/mysql/bin/mysql_config and add “-lmygcc” to the end of the libraries to link. I added it to the end of this line in the script:

libs=" $ldflags -L$pkglibdir -lmysqlclient -lz -lcrypt -lnsl -lm -lmygcc "

Otherwise I got the error ‘unresolved symbol __pure_virtual exists‘. Of course if you don’t get this error you don’t have to change the file.

You can test the Ruby MySQL binding with:

irb
require 'rubygems'
require 'mysql'

This should return true. That’s it, know you should be able to connect your Rails program with MySQL.

Topics: Uncategorized |

Comments