Wednesday 5 June 2013

How To Install MySQL in Linux

Most of the Linux flavors comes with MySQL.  If you want use MySQL, my recommendation is that you download the latest and stable version of MySQL and install it yourself. Later you can upgrade it to the latest version when it becomes available. In this post, I will discuss how I installed MySQL on Red hat or Oracle Linux platform.

1. Download the latest stable release of MySQL :

Download MySQL from mysql.com. Please download the community edition of MySQL for your appropriate Linux platform. I downloaded the “Red Hat Enterprise Linux 5 RPM (x86_64bit)”. Make sure to download MySQL Server, Client and “Headers and libraries” from the download page.

  • MySQL-client-5.5.32-1.rhel5.x86_64.rpm
  • MySQL-devel-5.5.32-1.rhel5.x86_64.rpm
  • MySQL-server-5.5.32-1.rhel5.x86_64.rpm

2. Remove the existing default MySQL that came with the Linux distribution.

Do not perform this on an system where the MySQL database is getting used by some application.

[local-host]# rpm -qa | grep -i mysql
mysql-5.0.22-2.1.0.1
mysqlclient10-3.23.58-4.RHEL5.4

[local-host]# rpm -e mysql --nodeps
No Mysql is running on the host.

3. Install the downloaded MySQL package

Install the MySQL Server and Client packages as shown below.

[local-host]# rpm -ivh MySQL-server-5.5.32-1.rhel5.x86_64.rpm MySQL-client-5.5.32-1.rhel5.x86_64.rpm

## if it will not work shows any message like below : use force option to install MYSQL-server :

# rpm -ivh --force MySQL-server-5.6.16-1.el6.x86_64.rpm


file /usr/share/mysql/czech/errmsg.sys from install of MySQL-server-5.6.16-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.52-1.el6_0.1.x86_64
        file /usr/share/mysql/danish/errmsg.sys from install of MySQL-server-5.6.16-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.52-1.el6_0.1.x86_64
        file /usr/share/mysql/dutch/errmsg.sys from install of MySQL-server-5.6.16-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.52-1.el6_0.1.x86_64
Preparing...   ########################################### [100%]
1:MySQL-client ########################################### [ 50%]
2:MySQL-server ########################################### [100%]
This will also display the following output and start the MySQL daemon automatically.
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 medica2 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 at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/

Starting MySQL.[  OK  ]
Giving mysqld 2 seconds to start
** If it is not get started automatically find below how to start 

Install the “Header and Libraries” that are part of the MySQL-devel packages.

[local-host]# rpm -ivh MySQL-devel-5.5.32-0.rhel5.x86_64.rpm
Preparing...   ########################################### [100%]
1:MySQL-devel  ########################################### [100%]

4.  Perform post-install security activities on MySQL.

At a bare minimum you should set a password for the root user as shown below:
[local-user]# /usr/bin/mysqladmin -u root password 'My2Secure$Password'
The best option is to run the mysql_secure_installation script that will take care of all the typical security related items on the MySQL as shown below. On a high level this does the following items:
  • Change the root password
  • Remove the anonymous user
  • Disallow root login from remote machines
  • Remove the default sample test database

5.  Verify the MySQL installation:

You can check the MySQL installed version by performing mysql -V as shown below:
[local-host]# mysql -V
mysql  Ver 14.14 Distrib 5.1.25-rc, for redhat-linux-gnu (i686) using readline 5.1
Connect to the MySQL database using the root user and make sure the connection is successfull.
[local-host]# mysql -u root -p  /** if it is not connect start service first **/
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

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

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

Follows the steps below to stop and start MySQL
[local-host]# service mysql status
MySQL running (12588)                                      [  OK  ]
[local-host]# service mysql stop
Shutting down MySQL.                                       [  OK  ]
[local-host]# service mysql start
Starting MySQL.                                            [  OK  ]
Issues I faced: 
1) When you are not able to connect MySQL, start the mysql service first.
  #service mysql start.
2) When you are not able to connect from the remote host give the permission as     below.
   [local-host]# mysql -u root -p
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root@'%' IDENTIFIED BY 'PASSWORD' WITH    GRANT OPTION;
If you want to give a permission to connect Mysql to a perticular remote machine give that IP instead of '%'.
Here '%' represents all Remote Hosts.