How to Install MySQL in Linux

The MySQL database is the most popular database in the open source world, because of ease of use, performance and high reliability. MySQL installation and configuration is very simple. Many of the world’s largest organizations rely on MySQL to save time and money power of their sites at high-volume and software.

This how-to will explain how to install mysql on linux, services status, secure post-install activities of MySQL, installation verification and how to connect with MySQL.

Installing MySQL Service

[root@linuxpathfinder ~]# yum install mysql-server mysql-client

MySQL-devel – Libraries and header files

[root@linuxpathfinder ~]# yum install mysql-devel

Starting mysql services

[root@linuxpathfinder ~]# /etc/init.d/mysqld status
mysqld is stopped

[root@linuxpathfinder ~]# /etc/init.d/mysqld start

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

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

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

Starting mysqld:                                           [  OK  ]

Secure post-install Activities on MySQL

[root@linuxpathfinder ~]# /usr/bin/mysqladmin -u root password ‘mypassword’

mysql_secure_installation script is the best option to run because of typical security related items on the MySQL as shown below. It does on a high level items as follows:

  • Change the root password
  • Remove the anonymous user
  • Disallow root login from remote machines
  • Remove the default sample test database
[root@linuxpathfinder ~]# 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!

Verify Installation:

[root@linuxpathfinder ~]# mysql -V
mysql  Ver 14.14 Distrib 5.1.69, for redhat-linux-gnu (i386) using readline 5.1

Login into MySQL Prompt:

Enter this command and you will be prompted to enter the password for the MySQL user

[root@linuxpathfinder ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.1.69 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

MySQL Status

To know the status, enable and disable the MySQL service.

[root@linuxpathfinder ~]# /etc/init.d/mysqld status
mysqld (pid  13708) is running…

[root@linuxpathfinder ~]# /etc/init.d/mysqld stop
Stopping mysqld:                                           [  OK  ]

[root@linuxpathfinder ~]# /etc/init.d/mysqld start
Starting mysqld:                                            [  OK  ]

[root@linuxpathfinder ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                             [  OK  ]

[root@linuxpathfinder ~]# service mysqld status
mysqld (pid  14169) is running…

[root@linuxpathfinder ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

[root@linuxpathfinder ~]# service mysqld start
Starting mysqld:                                             [  OK  ]

Installation of mysql in linux guide is finished here. Hope this guide will be invaluable for you.

Avatar photo

Asif Khan

Responsible and proactive professional with more than 13 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.