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

50 Simple and Useful dmidecode Commands for Linux

The dmidecode command in Linux allows users to retrieve sensitive hardware-related information directly from the command line. This way,...

The 10 Best IP Address Management Tools for Linux

IP address management takes a large part in the life of modern network admins. Linux admins are expected to...

25 Must Know Nginx Commands for Developers and Admins

Nginx is one of the most popular web servers among Linux and BSD users due to its rich feature...

40 Useful Linux Server Commands for Beginners in 2020

Most of the virtual world is powered by Linux today. Admins and network owners like to take control of...

Most Talked Post

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

Most Stable Linux Distros: 5 versions of Linux We Recommend

At the very beginning, I would like to mention why the term ‘Stable’ comes in relation to Linux OS...

15 Best Things To Do After Installing Linux Mint 19 “Tara”

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

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

Editors' Pick

Best LaTeX Editor: 33 LaTeX Editors Reviewed for Linux Nerds

In the arena of Linux, LaTeX is considered as...

Open Source Torrent Client for Linux: Top 10 Reviewed for Linux Nerds

Torrents are an excellent source to download large documents...

Top 20 Best Bioinformatics Tools for Linux: An Ultimate Collection

There are far-ranges of Linux bioinformatics tools available that...

Redhat vs Ubuntu: 15 Fundamental Facts You Must Know

There are hundreds of Linux distributions out there. It's...

How To Speed Up Ubuntu Linux: 12 Must Follow Tips

Ubuntu is very powerful and polished Debian based Linux...