Howto Install MySQL on Linux

by Ramesh Natarajan on July 6, 2008

MySQL Logo
Most of the Linux distro comes with MySQL.  If you want use MySQL, my recommendation is that you download the latest version of MySQL and install it yourself. Later you can upgrade it to the latest version when it becomes available. In this article, I will explain how to install the latest free community edition of MySQL on Linux platform.

1. Download the latest stable relase 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)”. Make sure to download MySQL Server, Client and “Headers and libraries” from the download page.

  • MySQL-client-community-5.1.25-0.rhel5.i386.rpm
  • MySQL-server-community-5.1.25-0.rhel5.i386.rpm
  • MySQL-devel-community-5.1.25-0.rhel5.i386.rpm

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

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.RHEL4.1

[local-host]# rpm -e mysql --nodeps
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
[local-host]# rpm -e mysqlclient10

3. Install the downloaded MySQL package

Install the MySQL Server and Client packages as shown below.

[local-host]# rpm -ivh MySQL-server-community-5.1.25-0.rhel5.i386.rpm MySQL-client-community-5.1.25-0.rhel5.i386.rpm
Preparing...                ########################################### [100%]
1:MySQL-client-community ########################################### [ 50%]
2:MySQL-server-community ########################################### [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

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

[local-host]# rpm -ivh MySQL-devel-community-5.1.25-0.rhel5.i386.rpm
Preparing...                ########################################### [100%]
1:MySQL-devel-community  ########################################### [100%]

Note: When I was compiling PHP with MySQL option from source on the Linux system, it failed with the following error. Installing the MySQL-devel-community package fixed this problem in installing PHP from source.

configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore!

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
[local-host]# /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.
You already have a root password set, so you can safely answer 'n'.
Change the 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!

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
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>

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  ]

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Tags:

{ 37 comments… read them below or add one }

1 narendra.s.v July 7, 2008 at 1:10 am

wow thanks a lot coz we are soon getting a Linux host :D

2 Siddharth July 12, 2008 at 10:44 pm

lotta thx for for this ;)
although I will install my sql on windows but I am sure this can help me :P

3 Gabriel October 28, 2008 at 11:17 am

Thanks, this guide is updated has been very usefull !!

4 Anjum January 9, 2009 at 3:03 am

Tahnkx for so much clear view of installing mysql on linux.
It answered me many quetion about installation.i have not tried but i near future um gonna do this and aill let u know about hte result

5 Swathi February 8, 2009 at 9:46 pm

Thank you so much for the detailed instructions. It would be helpful to add the expected exceptions and possible fixes too.
I had an issue with “Starting MySQL..Manager of pid-file quit without updating file.[Falure]”
I searched a lot in the internet and finally ended up at the solution.
starting “sudo /bin/sh /etc/init.d/mysql start” fixed the issue.
Hope this helps others who face the same issue on linux.

6 kenny April 2, 2009 at 10:09 pm

thanks Swathi. that helped me

7 Ravi September 30, 2009 at 3:41 am

Hi,
Its good to see the MySQL 5x install procedure using RPMs. But I have a specific requirement to be implemented and I can’t see how to do that.
Instead of the default “mysql” user and group we need to create a client specific user who owns the mysql installation. How can I specify that, assuming that I create the user and group before starting installation with the RPM.

Thanks

ravi

8 wessy December 10, 2009 at 4:20 am

great. thanks

9 Jan February 14, 2010 at 4:38 am

man i messed up the server vut you saved me, i’ve been looking for this solution for a couple of days.. thanks man

10 Ronald June 13, 2010 at 2:46 am

Hi I followed your guide I am having trouble enabling remote connection to mysql can you please add it to your guide? Thank you in advance.

11 suji September 28, 2010 at 5:28 am

How install php in linux ?

12 gautam November 18, 2010 at 12:14 am

thank you .
it was very helpful.

13 Anil December 12, 2010 at 1:08 am

Grt article!
very helpful

14 arturo garcia December 12, 2010 at 9:24 pm

thank you very much, this is the best guide I’ve found

15 martin January 12, 2011 at 6:32 pm

INSTALLING MYSQL WITHOUT ACCESS TO ROOT ACCOUNT:

1. Download MySQL Community Server 5.5.8 Linux – Generic Compressed TAR Archive
2. Unpack it. For example to: /home/martin/mysql
3. Create my.cnf file in your home directory. File contains:

[server]
user=martin
basedir=/home/martin/mysql
datadir=/home/martin/sql_data
socket=/home/martin/socket
port=3666

4. Go to /home/martin/mysql directory and execute:

./scripts/mysql_install_db –defaults-file=~/my.cnf –user=martin –basedir=/home/martin/mysql –datadir=/home/martin/sql_data –socket=/home/martin/socket

–defaults-file must be the first paremeter! Otherwise it won’t work. It’s mysql bug.

5. Your mysql server is ready. Start it with command:

./bin/mysqld_safe –defaults-file=~/my.cnf &

6. Connecting to server:

mysql -h 127.0.0.1 -P 3666 -u root -p (using tcp)
or
mysql –socket=/home/martin/socket -u root -p (using unix socket)

7. Shutting down server:

mysqladmin -h 127.0.0.1 -P 3666 -u root shutdown(using tcp)
or
mysqladmin –socket=/home/martin/socket -u root shutdown (using unix socket)

16 jen February 11, 2011 at 12:34 am

wow this article really helped but i did all my installation with , how to change it to mysql user

17 siros April 30, 2011 at 5:47 pm

Thank you so much great article.

my first source installation.

18 santosh May 22, 2011 at 8:02 am

thank u so much with helpful these document

19 Al-Imran Bin Al-Awad June 8, 2011 at 3:58 pm

Great article for a newbie like me. Thanks Sir right from UAE

20 Peri September 13, 2011 at 5:31 am

Hi,

I have successfully installed mysql in my linux machine but can any one know , why after installing all the 3 rpms (client/server/devel) the version is reflected with “unknown -linux” instead of red-hat
rpm’s used for my installation below.

MySQL-devel-community-5.1.58-1.rhel5.x86_64.rpm
MySQL-server-community-5.1.58-1.rhel5.x86_64.rpm
MySQL-client-community-5.1.58-1.rhel5.x86_64.rpm

[root@localhost peri]# mysql -V
mysql Ver 14.14 Distrib 5.1.58, for unknown-linux-gnu (x86_64) using readline 5.1

21 Avinash Bhageloe October 28, 2011 at 7:21 pm

Thanx a lot. This information helpt me alot :)

Regards,
Avinash Bhageloe

22 mustaq shah owaisi November 24, 2011 at 1:10 am

thanks for writing in easy format

23 marcos February 3, 2012 at 6:34 am

Very good…. ! I’m a newbie on MySQL.. and it helped me in installtion.

Thanks a TON

24 sharad May 23, 2012 at 11:01 am

I am trying to install a software which required MySQL-server, MySQL-client installed on the server so now I want to install MySQL-server and MySQL-client using rpm files “MySQL-server-5.5.17-1.rhel5.x86_64.rpm” and “MySQL-client-5.5.17-1.rhel5.x86_64.rpm”.

I have installed MySQL-client but when I try to run mysql-server

*rpm -i MySQL-server-5.5.17-1.rhel5.x86_64.rpm*

it gives me error:

*error: Failed dependencies:
MySQL conflict with mysql-5.0.77-4.el5_6.6.x86_64
MySQL conflict with mysql-5.0.77-4.el5_6.6.i386
MySQL conflict with mysql-server-5.0.77-4.el5_6.6.x86_64*

So how can i solve this problem?

25 zneteng July 11, 2012 at 4:10 pm

sharad,

think you can try rpm -e to remove old ones, or use “rpm -Uvh” to update the rpm

26 amit August 21, 2012 at 4:43 am

Thanks ,

This tutorial is very good and working……….
.One thing i would like to ask is it necessary to install
MySQL-client-community-5.1.58-1.rhel5.x86_64.rpm .

27 Manjunath September 10, 2012 at 11:58 pm

Thanks a lot.
It helped me a lot in installation of mysql on linux server.

28 Narender Reddy October 18, 2012 at 5:31 am

Hi,

Greate document,Detail explanation of installtion steps and configuration steps

Thank you very much..

29 neha October 20, 2012 at 9:21 am

in wndow 7 i install jomlla 1.5 in wamp server i change local host to ip address by now i am not able to open my page neither ip address nor local host anyone can help me.

hay i have red hat i want to install php my admin for my web server can u give me the installation manual step by step and what should be the other requirment pl give me manual i tried to install but i can not

30 Narendar January 22, 2013 at 2:03 am

Thank you so much great article

31 Mujeeb January 24, 2013 at 1:54 am

Thank you for this, detaled explanation of instalation step by step

32 Gaurav Khinchi April 4, 2013 at 4:45 am

I am having following problems while installing my sql server at linux…. can you pls help me out….

-bash-4.1$ rpm -ivh /login/sg216750/softwaresinstallables/MYSQLServer\ For\ Linux/MySQL-server-5.5.30-1.linux2.6.i386.rpm
error: Failed dependencies:
libaio.so.1 is needed by MySQL-server-5.5.30-1.linux2.6.i386
libaio.so.1(LIBAIO_0.1) is needed by MySQL-server-5.5.30-1.linux2.6.i386
libaio.so.1(LIBAIO_0.4) is needed by MySQL-server-5.5.30-1.linux2.6.i386
-bash-4.1$

33 Ashish June 25, 2013 at 1:03 am

When I install the mysql from “RPM” than I cant find the “my.cnf” file location…..

So where can I find it?

My OS is CentOS

I show in “/etc” but there wasnt…

Please guide me

34 gio August 16, 2013 at 9:08 am

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 spor, or if I uninstalle it, how do I go about installing it in the folder I want? Any help is greatly appreciated…thanks!!

35 Ben October 24, 2013 at 12:04 pm

Hello, Is it possible to install both 32bit and 64bit mysql on the same system and run them simultaneously? I have a linux system with 32bit Mysql installed. This is live and cannot be touched. I have a user that needs to import a database that is in excess of 30GB in size. I need to install a parallel installation of mysql in the 64bit version to handle the size of the database.

36 Chethan December 8, 2013 at 2:01 am

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)

–>I get this error when executing
# /usr/bin/mysql_secure_installation

–>and in /var/lib/mysql/mysql.sock, mysql.sock file is missing, could not find it anywhere under /
I’m using RHEL 6
Please Help!!

37 satyendra June 19, 2014 at 2:13 am

thanks for material guys.

Leave a Comment

Previous post:

Next post: