My (SQL) WorkLog: Step-by-Step guide for Installing MySQL on RHEL5

Thursday, July 30, 2009

Step-by-Step guide for Installing MySQL on RHEL5

Installation of MySQL Server on Linux (RedHat Enterprise Linux 5)

The recommended way to install MySQL on RPM-based Linux distributions is by using the RPM packages. The RPMs that MySQL/Sun provide to the community should work on all versions of Linux that support RPM packages and use glibc 2.3. You can download it from http://mirrors.sunsite.dk/mysql/downloads/mysql/5.1.html

Checking if MySQL is already installed & would be uninstalling it to install latest MySQL version

[root@localhost mysql]# rpm -qa | grep -i '^mysql-'
MySQL-python-1.2.1-1
mysql-5.0.22-2.1.0.1
mysql-server-5.0.22-2.1.0.1
mysql-connector-odbc-3.51.12-2.2

Un-installing older version of MySQL

[root@localhost mysql]# rpm --nodeps -ev MySQL-python-1.2.1-1
[root@localhost mysql]# rpm --nodeps -ev mysql-5.0.22-2.1.0.1
[root@localhost mysql]# rpm --nodeps -ev mysql-server-5.0.22-2.1.0.1
warning: /var/log/mysqld.log saved as /var/log/mysqld.log.rpmsave
[root@localhost mysql]# rpm --nodeps -ev mysql-connector-odbc-3.51.12-2.2
[root@localhost mysql]#


First, using a web browser or wget utility, you should download the server RPM from the location: http://www.mysql.com/Downloads/MySQL-*.rpm
Once you download the packages, install it as shown below.


[root@localhost mysql]# ls -la
total 39572
drwxr-xr-x 2 root root 4096 Jul 26 17:26 .
drwxr-xr-x 3 root root 4096 Jul 26 16:40 ..
-rw-r--r-- 1 root root 6664128 Jun 11 14:07 MySQL-client-community-5.0.83-0.rhel5.i386.rpm
-rw-r--r-- 1 root root 10616677 Jun 11 14:17 MySQL-devel-community-5.0.83-0.rhel5.i386.rpm
-rw-r--r-- 1 root root 19613508 Jun 11 14:26 MySQL-server-community-5.0.83-0.rhel5.i386.rpm
-rw-r--r-- 1 root root 3532055 Jun 11 14:31 MySQL-shared-compat-5.0.83-0.rhel5.i386.rpm

Here is brief details of packages that we are going to install

MySQL-server-community-5.0.83-0.rhel5.i386.rpm: The MySQL server. You need this unless you only want to connect to a MySQL server running on another machine.
MySQL-client-community-5.0.83-0.rhel5.i386.rpm: The standard MySQL client programs. You probably always want to install this package.
MySQL-devel-community-5.0.83-0.rhel5.i386.rpm: The libraries and include files that are needed if you want to compile other MySQL clients, such as the Perl modules.
MySQL-shared-compat-5.0.83-0.rhel5.i386.rpm: This package includes the shared libraries for MySQL 3.23, 4.0, and so on, up to the current release. It contains single-threaded and thread-safe libraries. Install this package instead of
MySQL-shared if you have applications installed that are dynamically linked against older versions of MySQL but you want to upgrade to the current version without breaking the library dependencies.


To perform a standard minimal installation, you can only install the server and client RPMs and for complete installation you have to install all the packages.



[root@localhost mysql]# rpm -ivh MySQL-*.rpm
Preparing... ########################################### [100%]
1:MySQL-shared-compat ########################################### [ 25%]
2:MySQL-client-community ########################################### [ 50%]
3:MySQL-devel-community ########################################### [ 75%]
4:MySQL-server-community ########################################### [100%]
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'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
Starting MySQL..[ OK ]
Giving mysqld 2 seconds to start
[root@localhost mysql]#


The server RPM places data under the /var/lib/mysql directory. The RPM also creates a login account for a user named mysql (if one does not exist) to use for running the MySQL server, and creates the appropriate entries in /etc/init.d/ to start the server automatically at boot time.


If you want to install the MySQL RPM on older Linux distributions that do not support initialization scripts in /etc/init.d (directly or via a symlink), you should create a symbolic link that points to the location where your initialization scripts actually are installed. For example, if that location is /etc/rc.d/init.d, use these commands before installing the RPM to create /etc/init.d as a symbolic link that points there:
shell> cd /etc
shell> ln -s rc.d/init.d .

However, all current major Linux distributions should support the new directory layout that uses /etc/init.d, because it is required for LSB (Linux Standard Base) compliance.
If the RPM files that you install include MySQL-server, the mysqld server should be up and running after installation. You should be able to start using MySQL.



Securing the Server


[root@localhost mysql]# /usr/bin/mysql_secure_installation




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...



All done! If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


[root@localhost mysql]#




Test mysql connectivity

Type the following command to connect to MySQL server:


[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.0.83-community MySQL Community Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

17 comments:

Anonymous said...

hi umesh,

thank you for the excellent write-up. This was very helpful for me to install mysql on ly rhel

Anonymous said...

umesh ji the site you are mentioned is not showong any packages to dowmload
http://www.mysql.com/Downloads/MySQL-*.rpm

Umesh Shastry said...

Hi,

Please try from the below link(chose appropriate OS)

http://www.mysql.com/downloads/



Regards,
Umesh

Umesh Shastry said...

Select appropriate OS - http://www.mysql.com/downloads/mysql/#downloads

Anonymous said...

Hi
Thanks so much clear view of installing mysql on RHEL5. I just viewed it after my installation i will the result.

Anonymous said...

very helpful

Anonymous said...

Thank you so much , it was very helpful

vinay

Anonymous said...

Thanks, this guide was very helpful. I did run into a problem when I ran the mysql_secure_installation.

After I pressed return through the current password, I got this error:

ERROR 2002 (HY000):Can't connect to local MySql server through socket '/var/run/mysqld/mysqld.sock'

I was stuck in a loop here until I pressed CTRL-C. After that I did some snooping on the web and found a solution:

At root, type

# /usr/sbin/setenforce 0
# /etc/init.d/mysql start

After doing this, I run the mysql_secure_installation again and had no problems.

jaiforbyss said...

Thanks very much Umesh for the wonderful installation steps.. Jai

Anonymous said...

I tried your step by step guide and succeeded to install Mysql in my redhat machine. How do I locate mysql config file? cant find them in /etc/my.cnf /usr/etc/my.cnf

Unknown said...

Hello,
I installed mysql 5.6.11 (client and server) on my linux (Red Hat Linux 5). My issue is that the rpm installed everything on var/lib/mysql/dbname I want to install it under home/database/mysql/dbname.

how can I either move it to another spot, or if I uninstall it, how do I go about installing it in the folder I want? Any help is greatly appreciated...thanks!!

Umesh Shastry said...

MySQL rpm packages are not relocatable.. so --prefix is of no use..

As a workaround - you can install the rpm's with the parameter --noscripts option, which makes sure the post-installation scripts are not run.. later on, you need to manually run mysql_install_db, after making sure directories are created. Make sure the permission of the directories are correct.. something like

/bin/mysql_install_db --defaults-file=/path/to/my.cnf \
--basedir=/var/lib/mysql \
--datadir=/home/database/mysql/

(2) Try tar binaries and customize your environment

Anonymous said...

Hi Amesh,

When you say "you can install the rpm's with the parameter --noscripts option" do you mean on the step where I type "rpm -ivh MySQL-*.rpm", I will actually type "rpm -ivh MySQL-*.rpm --noscripts" ? Let me know if this is correct or if you meant something else.

Thanks for your feedback!

Anonymous said...

I tried to install the rpm with the parameter --noscripts and nothing works. when starting the server I get error"
Starting MySQL.The server quit without updating PID file (/var/lib/mysql/ppdb03.mysite.com.pid). "

Umesh Shastry said...

Pls try this..

cd /path_to_dir/MySQL-*.rpm
rpm -ivh --nodeps --noscripts MySQL-*.rpm

You need to manually run mysql_install_db, after making sure directories are created. Make sure the permission of the directories are correct.. something like

/mysql_install_db --defaults-file=/path/to/my.cnf \
--basedir=/var/lib/mysql \
--datadir=/db01/mysql --user=mysql

don't shave spare box to test this. let me know how it goes.. shall try to pass on exact steps from test box..

Unknown said...

Hi. I was finally able to get the database to be located under home/database/mysql/dbname by editing the /etc/init.d/mysql file. I stopped mysql, changed the file to have /home/database/mysql as the datadir and then copied all the files over to /home/database.

it works!
thanks for the tutorial.

Unknown said...

Hi Umesh Ji,

The steps were excellent and it worked on my local machine superbly.

But when i installed the same packages on a different server machine and tried the same steps and it does not work.

These are the errors:

[root@localhost ~]# mysql_secure_installation




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Enter current password for root (enter for none):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Enter current password for root (enter for none):


I press 'enter' no use, i goto my.cnf file and manually assign a password and try , still no use.
I am stuck.
And i did setenforce 0 as well, still not installing,

Its saying something to do with Not updating the PID file and localhost.localdomain.err.
I try removing this file and installing , still no use.

Thanks in advance..

Post a Comment