Easy Steps to Install MySQL on Ubuntu 16.04 and Debian OS

MySQL is the 2nd most popular database management system commonly used by web applications as backend. It integrates into Web development frameworks like LAMP (Linux, Apache, MySQL, PHP) to enable database and SQL (Structured Query Language) support. Here we are outlining the steps to install MySQL on Ubuntu 16.04 OS.

MySQL support on Ubuntu is unparallel. It is fast and reliable. Also, Ubuntu 16.04 is an LTS version which means it would get regular updates until five years from the date of release. And that’s why we recommend an LTS version for installing and managing servers like MySQL and Apache TomCat.

Debian users who want to install MySQL on Debian OS can also follow this tutorial as the commands and syntax are almost the same. However, do let us know for any variation and we’ll address it.

Prior installing MySQL on Ubuntu, you’ll have to decide the version of MySQL package for the installation. Its latest version is v5.7, but the older versions like v5.5 and v5.6 are also quite popular. In this post, we’ll use all these versions to install MySQL on Ubuntu.

Table of Index.

How to Install MySQL on Ubuntu 16.04 and Debian OS

Install MySQL on Ubuntu and Debian.

Pre-requisites.

  • A Virtual machine or physical system running Ubuntu 16.04 OS
  • Sudo or Root access to install MySQL
  • Fast and stable internet connection

How to install MySQL on Ubuntu 16.04?

Did you check if the system which you are using to install MySQL has the hostname set? It is a best practice to use a name for accessing the database servers. Make sure the hostname length should not exceed the maximum of 15 characters. You can check the hostname with any of the below commands.

With the commands given here, you can install all versions of MySQL supported by Ubuntu 16.04.

Installing the latest version – MySQL v5.7

To install MySQL v5.7, you can use the version included in Ubuntu’s APT repository. For Ubuntu 16.04, it currently points to the latest MySQL v5.7.

At the onset, we’ll first update the APT repo and upgrade the software already installed on our Ubuntu system.

💡 Fact – The “apt-get update” command synchronizes the local package (APT) index with the latest changes whereas the “apt-get upgrade” command updates the already installed packages on the system.

Next, run the following commands to install both MySQL server and client packages.

During the installation, the system will prompt you to set the database root user password. Make sure the password is hard to guess for others but the one which you can easily recall when needed.

Installing the older versions – MySQL v5.5 and v5.6

To install an older version of MySQL on Ubuntu, the process is somewhat similar. You just need to suffix the version identifier in the package names. Check out the below commands to carry out the installation.

First of all, update the package index and the system.

Install MySQL 5.5 server and client.

Install MySQL 5.6 server and client.

Securing the MySQL installation.

The installation comes with a security script to control the default settings for root logins and sample users. Run this script with <sudo> privileges.

Upon execution, the script will prompt you to enter the database root password you set during MySQL installation. Further, it’ll ask some questions which you need to respond with <ENTER> key to accept the default setting. Remember to ignore one question which asks you modify the root password.

Enabling remote database access.

Now, you are all set to start working with MySQL. However, if you like to access the database instance remotely via any client software, then you might run the following commands to allow remote access.

💡 Fact – If you are tired of entering <sudo> password for every command run, then use the <sudo -s> command to save you from any such hassle.

Initializing MySQL database.

To initialize the MySQL version earlier than 5.7, run the following command.

For versions, 5.7 and above, you should be using the following command.

In case the, database directory is pre-initialized, the command will issue the following error message.

Verifying MySQL installation.

It is easy to track if the MySQL software is successfully installed or not.

As the name suggests, the command will give you the version installed.

Now, to check if the MySQL service daemon is running or not, issue the following command.

It will return the following status. Please note that the value of process-id will change.

However, if the MySQL server is not running, then you can spawn it by using the below commands.

In addition to the above commands, you can test it further by logging into the MySQL database.

The above command will verify the successful MySQL installation on Ubuntu.

How to configure MySQL on Ubuntu?

Edit MySQL server settings.

The system permits you to alter the default MySQL configuration. If you want to change the default port, IP address or any other simple setting, then edit the “/etc/mysql/my.cnf” file.

However, modify the “/etc/mysql/mysql.conf.d/mysqld.cnf” file to update any advance database server option.

Once you make these changes, you’ll need to restart the MySQL services. You can run any of the below commands.

Managing MySQL database and users.

Connecting to MySQL instance.

For connecting to the installed MySQL instance, you need to run the below command.

After entering the database root user password, the above command will open the MySQL command prompt.

At the MySQL shell, you can execute SQL queries.

View Users in MySQL.

For viewing the existing database users, run the below query into the MySQL shell.

Create a new MySQL user.

You can create standard database users who can connect to the MySQL server and perform database related tasks.

You can also login with the newly created user.

Create a new MySQL database.

With everything in place, you might now want to create a new database as well. Use the below command to create a fresh MySQL DB.

You can verify if the new database is available for use. Run the below query.

Next, to update the database, you’ll first of all need to use the database.

Let’s now create a dummy table in the newly created database.

You can check if the table gets created successfully or not.

Performance Tuning Your MySQL Server.

You can install MySQL tuner package which comes with a Perl script to optimize the performance and tighten the security of a running MySQL instance.

Run the following commands to install and setup MySQL tuner.

Once you get the MySQL tuner installed, issue the below command to start the performance tuner. But, please make sure that the MySQL server has run at least 24 hours or longer before starting the performance tuner.

By default, the tuner will apply the default recommendations which you can change if needed.

Summary – How to Install MySQL on Ubuntu 16.04 OS

We tried to keep all the instructions simple and directed to install MySQL on Ubuntu. And we wish that you would now be able to use them on your systems.

If you still have any open question about MySQL, then please let us know through your comments.

Best,
TechBeamers

2 Comments

  1. Adriano Scheffer Reply
    • Meenakshi Agarwal Reply

Leave a Reply


*