CentOS - Installing MySQL (with Rails and PHP options)

Installing MySQL on CentOS is very simple using the 'yum' package manager.

This article will go through the install process and also look at what other packages are needed for MySQL to integrate with Ruby on Rails and with a standard PHP install.


MySQL Install

To begin with, a simple MySQL install:

sudo yum install mysql-server mysql mysql-devel

Note that we have installed the development libs and headers with the 'mysql-devel' package. This package is required by the mysql Rubygems package which we install later in this article.

Start

We need to start the MySQL server to intiliaze the installation of the base MySQL databases.

sudo /etc/init.d/mysqld start

During the process, you will be presented with instructions to install a password for the root MySQL user:

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'

Setting the MySQL Root password is a recommended step in setting up your Slice. However, it is, of course, up to you.

Should you decide that protecting your production database is a good idea, then simply enter your chosen password as directed.

MySQL Password Confirmation

Let's confirm that our password is working as expected.

mysql -uroot -ppassword

Replace 'password' with the one you set in the previous step.

The result should be a successful login to MySQL:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.45 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

OK and exit MySQL.

mysql> quit;

Done. It really is as simple as that.

MySQL and Ruby on Rails

I am assuming you have Ruby on Rails installed at this stage (if not, please see the CentOS - Ruby on Rails article).

All we need to do is install one package as follows:

sudo gem install mysql -- --with-mysql-config=/usr/lib64/mysql/mysql_config

By default the install process looks for the mysql_config file in '/usr/lib'. However, as we have installed the 64bit version of MySQL, we need to specify the correct location as shown above.

Seems suspiciously easy. Let's conduct a quick test to see if all is well:

irb
#irb(main):001:0> 
require 'rubygems'
=> true
#irb(main):002:0> 
require 'mysql'
=> true

That seems fine to me.

MySQL and PHP

If you installed Apache and PHP using the CentOS - Apache and PHP article, you will already have the PHP MySQL module installed (which is 'php5-mysql').

This simple process also installs the PHP 'mysqli' (MySQL improved extension) module. This allows for much more powerful MySQL usage within PHP - please see the PHP mysqli page for more details.

A standard phpinfo page shows the following information for the mysql and mysqli modules:

phpinfo mysql and mysqli information

As you can see, installation is very easy.

Summary

Installing MySQL via the 'yum' package manager is very simple and ensures you have any security updates if and when they are released.

Installing the necessary packages for MySQL to integrate with Ruby on Rails and PHP is a suitably easy process - allowing you to concentrate on the site design rather than messing with the installation itself.

Article Comments:

Charlie commented Mon Apr 13 15:20:36 UTC 2009:

If you start off with the MySQL / PHP tutorial, you will have installed only mysql and mysql-server (because that's all that's required for the php installation) - not mysql-devel. However, when I tried to install Rails, using the command, above:

sudo gem install mysql -- --with-mysql-config=/usr/lib64/mysql/mysql_config

it failed. I think mysql-devel needs to be installed for the above command to work.

Mark commented Fri Apr 17 16:05:31 UTC 2009:

That is correct Charlie. It is noted in the article as well.

"Note that we have installed the development libs and headers with the 'mysql-devel' package. This package is required by the mysql Rubygems package which we install later in this article."

Ed Ford commented Thu Apr 23 07:07:50 UTC 2009:

I tried the guide above and am getting the same error on the gem installation.

I have got mysql-devel on the slice.

The error is saying:

ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb --with-mysql-config=/usr/lib64/mysql/mysql_config checking for mysqlsslset()... no checking for mysql.h... no checking for mysql/mysql.h... no * extconf.rb failed * Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

Have I missed something?

Dave commented Wed Apr 29 01:31:26 UTC 2009:

How do I install and configure mysqli? The article acts like it told me, but it doesn't at all. "This simple process also..." - what simple process?!?

Jared commented Mon May 25 18:40:06 UTC 2009:

You may want to make sure that MySQL starts automatically when the server is rebooted.

chkconfig --level 2345 mysqld on

Henrik Solberg commented Tue May 26 10:19:41 UTC 2009:

I too wants to know how to install and configure mysqli. I don't find the process simple at all. Any smart heads that could explain it?

paul commented Tue May 26 15:18:04 UTC 2009:

For security, in addition to this:

/usr/bin/mysqladmin -u root password 'new-password'

You might also want to log into mysql server: mysql -u root -p

and then run the following:


use mysql;

update user set password = password ('yournewpassword') where host = '127.0.0.1' and user = 'root';

update user set password = password ('yournewpassword') where host = 'localhost' and user = 'root';

update user set password = password ('yournewpassword') where host = 'yourhostnameeg_server.example.com' and user = 'root';


You can check the correct fields are updated by entering:

select * from user;

and finally enter:

flush privileges;

Ther's a good article on securing mysql server here: http://www.securityfocus.com/infocus/1726

Brendan Dean commented Sun Jul 26 00:01:18 UTC 2009:

Paul, thanks for the link and the extra advice on securing users.

I just wanted to point out that the middle section can be accomplished in two fewer commands:

use mysql; update user set password = password ('yournewpassword') where user = 'root';

navjeet Singh commented Sat Oct 31 01:32:52 UTC 2009:

@Ed Ford: Make sure you remove your old mysql mysql-server and mysql-devel

yum remove mysql mysql-server mysql-devel then install again: yum install mysql mysql-server mysql-devel

Belciu commented Sat Oct 31 23:18:40 UTC 2009:

Hello, Im trying to set up mysql on VPS, im connecting with vps from my IP 83.22~ . I cant change the password for root when I started the mysql first time because im not on localhost.. what should I do now?

dave commented Wed Jan 13 21:58:19 UTC 2010:

hello,

i followed the instructions for installing ruby+rails on centos (the prerequisite for this turorial), but it looks like the version of ruby that i got is too old for the mysql gems. do i have to compile ruby from source to get around this?

ERROR: Error installing mysql: mysql requires Ruby version >= 1.8.6

Justin commented Sat Jan 30 07:05:57 UTC 2010:

@dave,

Having the same issue:

ERROR: Error installing mysql: mysql requires Ruby version >= 1.8.6

What's the fix?

Chris Davidowski commented Mon Feb 01 18:03:50 UTC 2010:

Version 2.8 of the mysql gem is incompatible with ruby version < 1.8.6. You can run the following command to force version 2.7 of the mysql gem to install.

gem install mysql -v '2.7'

Yëco commented Wed May 12 08:54:18 UTC 2010:

You could also need to install ruby-devel package if you have this error:

ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb can't find header files for ruby

Pinhead commented Fri May 21 07:36:35 UTC 2010:

to get mysql working with php 5.3, i first started off by following the steps in this tutorial for installing mysql. then once i set the mysql root password, i unistalled mysql using

yum remove mysql mysql-server mysql-devel

then proceeded to reinstall the newer version:

wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm wget http://rpms.famillecollet.com/el5.i386/remi-release-5-7.el5.remi.noarch.rpm rpm -Uvh remi-release-5.rpm epel-release-5.rpm

REINSTALL MYSQL

yum --enablerepo=remi install mysql mysql-server mysql-devel

INSTALL PHP

yum --enablerepo=remi install php-common yum --enablerepo=remi install php yum install gd gd-devel yum --enablerepo=remi install php-mcrypt php-xml php-xml php-devel php-imap php-soap php-mbstring php-mysql yum --enablerepo=remi install php-mhash php-simplexml php-dom php-gd php-pear php-pecl-imagick php-magickwand

jmm commented Tue Oct 30 19:36:40 UTC 2012:

sudo yum install ruby-mysql before installing the gem mysql corrected the above mentioned error for me.

I also changed to the non-standard port in: /usr/lib64/mysql/mysql_config

nurse networking commented Wed Apr 24 06:34:17 UTC 2013:

Nurses in the neonatal intensive care unit (NICU) are amongst the highly trained nurses you could ever find. In the long-term a nurse within entitled for higher repayment when they complete specialty instruction or an advance course. To become a certified nursing assistant may give you a salary advantage greater than that of typical nursing assistants.

Also visit my web blog ... [nurse networking](http://RECLAIMINGTHESKIES.

COM/?p=43 "nurse networking")

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)