A Full-Stack Developer’s Guide: Installing MySQL on Ubuntu Linux

M. Ilham Margatama
5 min readApr 26, 2024
Photo by Caspar Camille Rubin on Unsplash

You might be wondering why I took the time to write this comprehensive guide on installing MySQL on Ubuntu Linux. Well, the truth is, I created this post not only to assist others but also to serve as a reference for myself. As a full-stack developer, I often find myself setting up local, development, and production environments for various projects. Having a detailed, step-by-step guide like this one ensures that I can quickly and efficiently install MySQL whenever I need to, without having to hunt down scattered documentation or rely on memory alone. So, consider this guide not just a resource for you, but also a handy tool for my own development journey.

Why MySQL?

Before we jump into the installation, let’s quickly talk about why MySQL is a solid choice for your database needs. MySQL is an open-source relational database management system that’s trusted by millions of developers worldwide. It’s known for its reliability, performance, and ease of use, making it a favorite among developers for building robust, scalable applications.

Step 1: Update Your System

First things first, let’s ensure that our Ubuntu system is up to date. Open up your terminal and run the following commands:

sudo apt update
sudo apt upgrade

This will update the package lists and upgrade any outdated packages on your system. It’s always a good idea to start with a clean slate before installing new software.

Output for sudo apt update:

Hit:1 http://us.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
...
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.

Output for sudo apt upgrade (if there are updates):

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
package1 package2 ...
...
0 upgraded, 0 newly installed, 0 to remove, and 0 not upgraded.

Step 2: Install MySQL

Now that our system is up to date, let’s proceed with installing MySQL. Ubuntu makes this process a breeze with its package manager. Run the following command to install the MySQL server package:

sudo apt install mysql-server

During the installation, you’ll be prompted to set a root password for MySQL. Make sure to choose a strong password and keep it safe. When the installation running you’ll see this in your terminal.

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
mysql-server-8.0 mysql-server-core-8.0 ...
...
Setting up mysql-server-8.0 (8.0.xx-0ubuntu0.20.04.1) ...
...

Once the installation is complete, MySQL should be up and running on your system. You can verify this by checking the status of the MySQL service:

sudo systemctl status mysql

Step 3: Secure MySQL

With MySQL installed, it’s crucial to secure it to prevent unauthorized access and potential security vulnerabilities. Thankfully, MySQL provides a built-in script to help with this. Run the following command to launch the MySQL secure installation wizard:

sudo mysql_secure_installation

The wizard will guide you through various security configurations, such as setting a root password, removing anonymous users, and disabling remote root login. Follow the prompts and make the appropriate selections based on your security preferences. You’ll see the output like this in the terminal.

Securing the MySQL server deployment.Connecting to MySQL using a blank password.VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: n

Step 4: Access MySQL

Now that MySQL is installed and secured, let’s test it out by accessing the MySQL shell. You can do this by running the following command:

mysql -u root -p

You’ll be prompted to enter the root password you set earlier. Once authenticated, you should be greeted with the MySQL shell prompt, indicating that you’ve successfully connected to the MySQL server.

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is xxx
Server version: 8.0.xx-0ubuntu0.20.04.1 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

Step 5: Create a Database

With MySQL up and running, you’re ready to start creating databases and tables for your applications. Let’s start by creating a simple database. Run the following commands within the MySQL shell:

CREATE DATABASE my_database;

This command creates a new database named my_database. You can replace my_database with whatever name you prefer for your database. The output for creating a database within the MySQL shell:

CREATE DATABASE my_database;
Query OK, 1 row affected (0.01 sec)

Step 6: Listing Databases

Now that you’ve created a database, you might want to verify that it’s there along with any other databases you might have. Listing databases in MySQL is a straightforward process. Simply execute the following command within the MySQL shell:

SHOW DATABASES;

This command will display a list of all databases on the MySQL server. Let’s say you’ve created a database named my_database. Running the SHOW DATABASES; command should output something like this:

+--------------------+
| Database |
+--------------------+
| information_schema |
| my_database |
| mysql |
| performance_schema |
| sys |
+--------------------+

In this example, my_database is listed along with other system databases like information_schema, mysql, performance_schema, and sys. This confirms that your database has been successfully created and is accessible within the MySQL server.

Listing databases is handy for checking the status of your databases and ensuring that your creations are where they should be.

Conclusion

Congratulations! You’ve successfully installed MySQL on your Ubuntu Linux server and are ready to start building awesome applications. In this guide, we covered the installation process, securing MySQL, accessing the MySQL shell, and creating a database. After installing all setup is complete then you want to create and manage MySQL users, grant and revoke privileges, please check the next post to deep dive into Managing MySQL Access.

--

--