Sunday, January 17, 2021
Home Linux How to Install MySQL on Ubuntu Linux

How to Install MySQL on Ubuntu Linux

MySQL is considered the most popular database management system in the world. It is an open-source database management system which is usually installed as part of LAMP stack. For managing the data, it uses a relational database and structured query language (SQL). Here I am going to describe the steps of how to install MySQL on an Ubuntu server.


Before going to install MySQL on Ubuntu, it is advised to have the latest version of Ubuntu server with the necessary setup. This setup includes a non-root user having sudo privileges and a firewall.

Installation Process

  • MySQL Installation

In the latest versions of Ubuntu, MySQL is available by default in the APT package repository. For a successful installation process, updating the package index on the server is essential with “apt.”

sudo apt update

After that complete the installation of the default package.

sudo apt-get install mysql-server

By this, MySQL will be installed. But, setting password and configuration modification is not allowed due to security purposes.

  • MySQL Configuring

During a fresh installation, if you run the included security script, it will change some default options of less security for the purposes like sample users and remote root logins. In the latest versions of MySQL, the data directory is initialized automatically whereas older versions needed a manual initiation.

sudo mysql_secure_installation

It will drive you through a sequence where you can bring necessary changes to the security options of MySQL installation. The first prompt will inquire that whether you want to set up any password plugin to examine the strength of MySQL password given by you. The second prompt will ask you to set up a password dedicated to the MySQL root user. Confirm the selected password from here.

Then, pressing “Y” and “ENTER” will accept the defaults for all situations. Anonymous users and test database will be removed by this. This will also disable remote root login and activate the new rules changed by you for immediate action by MySQL.

For initializing the MySQL data directory, use “mysql_install_db” command for previous versions of 5.7.6 and for the versions after that use “mysqld –initialize.” If MySQL is installed from the Debian distribution, data directory is initialized by default. No additional action required.

It is to be mentioned that, setting a password for the root MySQL user does not ensure that the user has authentication with a password for connecting to MySQL shell. If required, following step 3 you can adjust this setting.

  • Adjustment of User Authentication and Privileges (Optional)

No password but a default plugin “auth_socket” is used to authenticate the root MySQL user in Ubuntu systems running the latest versions of MySQL (5.7 and later). Though it permits for some greater security and ease of use, in some cases like allowing an external program to access the user, it may create a complicated scenario.

For using a password to as root to MySQL, it is needed to change the authentication method. This change of authentication is form “auth_socket” to “mysql_native_password.” From the terminal, open up MySQL prompt to perform this action.

sudo mysql

After that, with the following command check the user authentication method of your MySQL user account.

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

Adjustment of User Authentication and Privileges

In the above example, it is seen that using the “auth_socket” plugin; the root user complete the authentication process. For configuring the root account to authenticate using a password, the following “ALTER USER” command is to run. Select a strong password and mind it that the root password set up in step 2 will be changed with this command.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run the following command to reload the grant tables and putting new changes into effect.


Check the authentication methods assigned by each user to be assured that root does not authenticate using “auth_socket” plugin.

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

authentication methods assigned by each user

In this example, you can see that the authentication is done by root MySQL user by using a password. Once confirmed on the server, you can exit the shell.

mysql> exit

Now, is the time to test the MySQL installation.

  • Testing the MySQL Installation

No matter how you have installed MySQL, it should have initiated functioning automatically. With the following command, check its status.

sudo systemctl status mysql.service

Testing the MySQL Installation

You can initiate with:

sudo systemctl start mysql

If MySQL does not run. You can try in another way of connecting to the database utilizing “mysqladmin” tool for an additional check. It is a client which allows run the administrative commands.

sudo mysqladmin -p -u root version

It indicates that MySQL is up and running.

Concluding Words

Now, MySQL is installed on your Ubuntu server. For ease of use, you can now modify it. Besides, adding measures is also possible for enhancing security. Is this tutorial on how to install MySQL on Ubuntu helpful? If you like it, please take a moment to share this tutorial on your social network. And don’t forget to leave your suggestion and experience in the comment section.


Please enter your comment!
Please enter your name here

Latest Post

How to Install and Configure Squid Proxy on Linux System

Squid proxy server is an open-source proxy server for Linux distributions. You can install the Squid proxy server on...

How to Install and Use Xrdp Server (Remote Desktop) on Linux System

The Xrdp server is the RDP tool (Microsoft Remote Desktop Protocol) for Linux distributions. You can install it on...

How to Install and Use FFmpeg on Linux Distros | Beginner’s Guide

The FFmpeg is a popular media library function that you can install individually or alongside a media player on...

How to Install and Configure Windows Subsystem for Linux

There was a time when Windows was completely separate and different from Linux. Windows used to think of Linux...

Most Talked Post

Most Stable Linux Distros: 5 versions of Linux We Recommend

At the very beginning, I would like to mention why the term ‘Stable’ comes about Linux OS or distro...

Linux or Windows: 25 Things You Must Know While Choosing The Best Platform

Choosing the best platform - Linux or Windows is complicated. Because both the system is versatile and capable of...

Linux Mint vs Ubuntu: 15 Facts To Know Before Choosing The Best One

Ubuntu and Linux Mint are two popular Linux distros available in the Linux community. Ubuntu is a derivative of...

Best Things To Do After Installing Linux Mint 20 “Ulyana”

Linux Mint is one of the best Linux distros for newcomers, especially those who come from other Operating Systems...