How to install MySQL on Linux

Akademily
4 min readSep 28, 2020
How to install MySQL on Linux

Most Linux distributions come with MySQL.If you want to use MySQL, my recommendation is that you download the latest version of MySQL and install it yourself. Later you can update it to the latest version when it becomes available. In this article, I will show you how to install the latest version of free MySQL on a Linux platform.

1. Download the latest stable version of MySQL

Download MySql from mysql.com. Please download the MySQL release for the corresponding Linux platform. I downloaded “Red Hat Enterprise Linux 5 RPM (x86)”. Make sure to download MySQL server, client, and “Headsers 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 an existing default MySQL that comes with the Linux distribution

Do not do this on a system where a MySQL database is needed by some applications.

[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 client and MySQL server 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 show the next 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
The databases and anonymous users created by default. This is
We are 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 “Headsers and libraries”, which are part of MySQL 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 the MySQL option from a source on a Linux system it failed with an error. Installing the MySQL-devel-community package eliminated this problem by installing PHP from the source.

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

4. Perform post-installation security actions on MySQL.

At a minimum, you should set a password for user root as shown below:

[local-user]# /usr/bin/mysqladmin -u root password ‘My2Secure$Password’.

The best option is to run the mysql_secure_installation script, which will take care of the security of the linked elements in MySQL as shown below. Let’s set the following points at a high level:

  • Changing the password
  • Delete an anonymous user
  • Denies root entry from remote machines
  • Delete the default test database of the sample

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 authorization.
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. Check if MySQL is installed correctly:

You can check MySQL which version of MySQL is installed by running -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 a MySQL database using the root user and make sure the connection is successful.

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

Follow these steps 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 ]

--

--

Akademily

We conduct reviews, guides and comparative tests of gaming laptops, monitors, graphics cards, keyboards, mouses, headsets and chairs to help you buy the best ga