Upgrade to a new version of MySQL on Linux for Ruby on Rails
back
I had a pretty unpleasant experience upgrading to a new version of MySQL on my Linux box, so I am simply listing the steps here. It is firstly a journal of what I need to do the next time it comes up. The second point is hopefully it becomes useful to someone else too.
For my Linux box, the database sits in /var/lib/mysql. The new MySQL source code base will by default install the database in /usr/local/var, the configuration in /usr/local/share/mysql, and the binaries in /usr/local/bin. This is if you simply unpack the source code from the SRPM or the tar-gzip file. By default, the steps to compile up and deploy everything is the familiar 3 steps. You first chdir into the directory where the source code has been unpacked into, then do:
- ./configure
- make
- sudo make install
This approach is much better than specifying the command line parameters to point to the current installation of your running database. It will allow you to switch to the new version of the binaries and leave the old binaries as a fallback, in case everything goes pear-shaped (believe me, they do!).
Once you have done the above staps successfully, remember to verify two things:
- /usr/local/bin/mysql_config should print out the include flag, linker flag, default socket, default port etc. Check that all these values are what you expect them to be. If not, manually edit this file to point to the correct values. This method is much quicker than modifying any config or script file. For example, on my Red Hat flavoured Linux box, the default socket is actually at /var/lib/mysql/mysql.sock, instead of the default generated parameter of /tmp/mysql.sock. This value is essential if you intend to run the command line client mysql or access your MySQL database via PHPMyAdmin web interface.
- The PATH of your environment is updated to look in /usr/local/bin first, so that the correct mysql_config is invoked. This script is essential to the operation of MySQL in general as it is used to obtain the default configuration parameters for the running MySQL server when no override is specified (which is the normally case).
At this point, you should edit the /etc/init.d/mysql script and change the basedir= directive to
basedir=/usr/local
Then restart the server by the command:
/etc/init.d/mysql restart
By doing this, you can experiment with the new version of the server. In the event things are not all working, you can always blank out the basedir directive again to switch back to the default version which came with the OS.
You should verify that both the socket and port access methods work, as the socket access only works locally. Try
mysql -u root -p mysql # to test the socket based access mysql -u root -p -h localhost -p 3306 mysql # to test port based access
Finally, we need to get the MySQL adapter for Ruby on Rails upgraded. If you do not do this, you might encounter a lot of different problems, like the connection is closed unexpectedly half way through the communication, or the error "Uninitialized constant Mysql", or the "StatementInvalid" error.
First of all, you install the MySQL adapter via gem by doing
sudo gem install mysql -- --with-mysql-config=/usr/local/bin/mysql_config
Once done, start up your Rails application to check if database access works. If, like me, you are unlucky enough to get those errors above after this stage, then do not despair. You need to do another few steps:
cd /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7 sudo ruby extconf.rb --with-mysql-dir=/usr/local/lib/mysql sudo make sudo make install
By this time, your Rails application should be able to access the MySQL database.
back
|