---
title: "Install MySQL on Ubuntu: Get Your Database Server Up and Running"
description: "Want a robust, secure database for your projects? Discover how to install MySQL on Ubuntu and implement essential security measures with this guide."
canonical: "https://adamtheautomator.com/install-mysql-on-ubuntu/"
---

# Install MySQL on Ubuntu: Get Your Database Server Up and Running

> Want a robust, secure database for your projects? Discover how to install MySQL on Ubuntu and implement essential security measures with this guide.

Source: https://adamtheautomator.com/install-mysql-on-ubuntu/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Install MySQL on Ubuntu: Get Your Database Server Up and Running](https://adamtheautomator.com/wp-content/uploads/2022/04/How-to-Install-MySQL-on-Ubuntu.jpg)

# Install MySQL on Ubuntu: Get Your Database Server Up and Running

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)29 February 20248 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[MySQL](/tag/mysql/)[Ubuntu Linux](/tag/ubuntu-linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Install MySQL on Ubuntu (From the Repository)](#install-mysql-on-ubuntu-from-the-repository)
*   [Installing MySQL from the Official Repository](#installing-mysql-from-the-official-repository)
*   [Securing Your MySQL Server](#securing-your-mysql-server)
*   [Creating a New MySQL User and Granting Limited Permissions](#creating-a-new-mysql-user-and-granting-limited-permissions)
*   [Testing Your MySQL Server](#testing-your-mysql-server)
*   [Conclusion](#conclusion)

You’re the _system-slash-database admin_ and need to build a database management system (DBMS) for a new project. If you choose to install MySQL on Ubuntu, then you’re in luck. MySQL is fast, reliable, versatile, and free — a good choice!

This tutorial will walk you through installing a MySQL server on Ubuntu, make it secure, and create a dedicated user with limited permissions to avoid exposing the root account. By the end, you’ll have built a MySQL server that you can use in your project to host databases!

## **Prerequisites**

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have an Ubuntu Server 20.04 or later with a non-root user account with **[sudo privileges](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/).**

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

## Install MySQL on Ubuntu (From the Repository)

The Ubuntu APT package repository includes the MySQL package by default, making it convenient to install MySQL Ubuntu in a single command.

> _NOTE: The default Ubuntu APT repository may not always have the latest MySQL version._

To install MySQL Ubuntu, proceed as follows.

1\. Open your preferred [SSH](https://adamtheautomator.com/powershell-ssh/) client and log in to your Ubuntu server.

2\. Ensure that your package list is recent by running the below command. This command updates the list of available packages in the APT package repository.

```bash
sudo apt update -y
```

3\. Now, install the `mysql-server` package by executing the following command. Wait for the installation to finish.

```bash
sudo apt install mysql-server -y
```

As you can see below, the latest MySQL version from the APT repository is 8.0.28.

![Install MySQL Ubuntu package from the default APT repository](https://adamtheautomator.com/wp-content/uploads/2022/04/image-280.png)

Install MySQL Ubuntu package from the default APT repository

Related:[Learning Ubuntu Apt Get Through Examples](https://adamtheautomator.com/ubuntu-apt-get/)

4\. Lastly, run the below command to check and confirm whether the `mysql` service is now running.

```bash
sudo systemctl status mysql
```

![Verifying mysql service status](https://adamtheautomator.com/wp-content/uploads/2022/04/image-281.png)

Verifying mysql service status

## Installing MySQL from the Official Repository

If you don’t like waiting for your Linux distro repository to catch up, downloading and installing the DEB package is the best way to get the latest features and bug fixes.

The MySQL developers provide their software repository to install the latest version of MySQL, but Ubuntu doesn’t know where to find that repository (yet). Follow the below steps to download and install MySQL from the official repository.

1\. Open the web browser on your computer and navigate to the [**MySQL APT Repository**](https://dev.mysql.com/downloads/repo/apt/) page.

2\. Once on the download page, click the **Download** button, as you can see below.

![Clicking the download link](https://adamtheautomator.com/wp-content/uploads/2022/04/image-282.png)

Clicking the download link

3\. On the next page, right-click the _**No thanks, just start my download**_ link and click _**Copy link address**_.

![Copying the MySQL download link](https://adamtheautomator.com/wp-content/uploads/2022/04/image-283.png)

Copying the MySQL download link

4\. Go back to your SSH terminal session and download the MySQL package to the `/tmp` directory home directory by running the `curl` command below. Make sure to change the URL value after the `-OL` parameter with the URL you copied in the previous step.

```bash
cd /tmp && curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb
```

![Downloading the MySQL DEB package](https://adamtheautomator.com/wp-content/uploads/2022/04/image-284.png)

Downloading the MySQL DEB package

5\. After downloading the package, run the [`dpkg`](https://man7.org/linux/man-pages/man1/dpkg.1.html) command below to install the MySQL package file.

```bash
sudo dpkg -i mysql-apt-config*
```

6\. On the configuration page, that shows up asking **`Which MySQL product do you wish to configure?`** – Press the down arrow until you select **`Ok`**, and press Enter.

![Selecting the MySQL product to configure](https://adamtheautomator.com/wp-content/uploads/2022/04/image-285.png)

Selecting the MySQL product to configure

7\. Now, run the below command to update your package list so that APT can find the new MySQL packages.

```bash
sudo apt update -y
```

8\. Now that APT knows where to find the latest MySQL version, run the below command to install the `mysql-server` package.

```bash
sudo apt install mysql-server -y
```

9\. Type in the new strong password for the MySQL database _root_ account on the configuration prompt and press Enter.

![Creating the MySQL root password](https://adamtheautomator.com/wp-content/uploads/2022/04/image-286.png)

Creating the MySQL root password

Re-enter the password and press Enter to confirm.

![Confirming the new password](https://adamtheautomator.com/wp-content/uploads/2022/04/image-287.png)

Confirming the new password

10\. Select `Use Strong Password Encryption (RECOMMENDED)`, and press Enter on the default authentication plugin selection.

![Selecting the default authentication method](https://adamtheautomator.com/wp-content/uploads/2022/04/image-288.png)

Selecting the default authentication method

Wait for the installation to complete. The screenshot below shows the MySQL server version you installed. In this example, the latest version is 8.0.28.

![Install MySQL Ubuntu package from the official repository](https://adamtheautomator.com/wp-content/uploads/2022/04/image-289.png)

Install MySQL Ubuntu package from the official repository

11\. Run the `systemctl` command below to verify that the MySQL service is running.

```bash
sudo systemctl status mysql
```

You will see the following output. This output shows that the service is active (running) and enabled.

![Verifying MySQL service status](https://adamtheautomator.com/wp-content/uploads/2022/04/image-290.png)

Verifying MySQL service status

Related:[Controlling Systemd services with Ubuntu systemctl](https://adamtheautomator.com/ubuntu-systemctl/)

## Securing Your MySQL Server

The MySQL server default installation is not secured out-of-the-box, and you need to perform some additional steps to secure your MySQL server. Luckily, the MySQL developers have created a configuration script that does most of the work for you.

The security script is called `mysql_secure_installation`, which you can find under the `/usr/bin` directory. The script modifies less safe defaults, such as remote root logins and sample users.

1\. Run the following command below to start the security script on the terminal. This script will ask you several Yes/No questions to configure the MySQL server security.

```bash
sudo mysql_secure_installation
```

> _If you installed MySQL from the default Ubuntu repository, you’d see the following message saying that you’re connecting to MySQL with a blank password! Don’t worry; you’ll be fixing that issue in the succeeding steps._

![Connecting to MySQL without a password.](https://adamtheautomator.com/wp-content/uploads/2022/04/image-291.png)

Connecting to MySQL without a password.

2\. At the next prompt asking to set up the **VALIDATE PASSWORD COMPONENT**, type `y` and press Enter. This component validates whether MySQL user passwords comply with the complexity requirement.

![Setting up password validation](https://adamtheautomator.com/wp-content/uploads/2022/04/image-292.png)

Setting up password validation

3\. Next, select the password validation policy level to implement. The options are:

*   `LOW` – requires a password with eight characters minimum.
    
*   `MEDIUM` – requires a password with eight or more characters, including numbers, mixed case, and special characters.
    
*   `STRONG` – same as `MEDIUM` but checks the password against a dictionary file for validation.
    

In this example, type 2 to select STRONG and press Enter.

![Selecting the password validation policy level](https://adamtheautomator.com/wp-content/uploads/2022/04/image-293.png)

Selecting the password validation policy level

> _MySQL does not have a default dictionary file. As such, you’ll need to create a dictionary file and modify the validate\_password\_dictionary\_file system variable value to the dictionary file path. Creating and configuring the_ [_password dictionary file_](https://dev.mysql.com/doc/mysql-security-excerpt/5.6/en/validate-password-options-variables.html#sysvar_validate_password_dictionary_file) _is not in the scope of this tutorial._

4\. To ensure that the root password complies with the password complexity requirements, set the root password again and re-enter. The script will then show you the password strength score. Press `y` to confirm saving the password and press Enter.

![Setting the strong root password](https://adamtheautomator.com/wp-content/uploads/2022/04/image-294.png)

Setting the strong root password

5\. Next, the script asks if it should remove the _anonymous_ user that the MySQL installation created. This user is safe to remove, so type `y` and press Enter to remove the anonymous user.

![Deleting the MySQL anonymous user](https://adamtheautomator.com/wp-content/uploads/2022/04/image-295.png)

Deleting the MySQL anonymous user

6\. To further lock down MySQL, type `y` and press Enter to disallow remote login for root. After this step, the root account can only log in to MySQL from the server and not from remote clients.

![Disabling remote root login](https://adamtheautomator.com/wp-content/uploads/2022/04/image-296.png)

Disabling remote root login

7\. Next, remove the `test` database. This database is only for testing and should not go into production. To remove (drop) the test database, type `y`, and press Enter.

![Removing the test database](https://adamtheautomator.com/wp-content/uploads/2022/04/image-297.png)

Removing the test database

8\. Finally, reload the privilege tables for all the changes to take effect immediately. Type `y` and press Enter to reload.

![Reloading the MySQL privilege table](https://adamtheautomator.com/wp-content/uploads/2022/04/image-298.png)

Reloading the MySQL privilege table

You have now successfully secured your MySQL server.

## Creating a New MySQL User and Granting Limited Permissions

At this point, there’s only one user account in your MySQL server, which is the _root_ user. As the name implies, the root account has full access to the MySQL server and every resource within.

Constantly using the root user account is not a best practice and can be a security risk. You should reserve the root account for administrative tasks that need its permissions. Otherwise, you should be using an account with limited privileges, and below are the instructions for creating one.

1\. Log in to MySQL as the root user on the terminal by executing the below command.

```bash
sudo mysql
```

![Logging in to the MySQL prompt](https://adamtheautomator.com/wp-content/uploads/2022/04/image-299.png)

Logging in to the MySQL prompt

2\. Next, run the `CREATE USER` statement to create a new user. The below example creates a new user account named `ata` which can only log in from the `localhost`. Make sure to provide a strong `password` that complies with the password policy you configured earlier.

```sql
CREATE USER 'ata'@'localhost' IDENTIFIED BY 'password';
```

Suppose you create a user who can log in from a specific remote computer, run the below command instead. This command replaces localhost with the remote computer’s ip\_address.

```sql
CREATE USER 'ata'@'ip_address' IDENTIFIED BY 'password';
```

To create a user that can log in to MySQL from anywhere, replace the localhost or ip\_address with % instead.

```bash
CREATE USER 'ata'@'%' IDENTIFIED BY 'password';
```

> _Always end your MySQL statements with a semi-colon (;). Also, enclosing the values in single quotes (‘) helps prevent SQL syntax errors._

The result below shows the successful user creation

![Creating a new MySQL user account](https://adamtheautomator.com/wp-content/uploads/2022/04/image-300.png)

Creating a new MySQL user account

3\. Now that you’ve created the user run the `GRANT` statement next to grant permissions. There are many MySQL privileges that you can assign to the user. But in this example, the command will grant the following privileges to the user `'ata'@'localhost`’ to all (`*.*`) databases and tables.

*   `ALTER` – gives the user permission to alter the structures of tables.
    
*   `SELECT` – gives the user access to the `SELECT` statement, which allows reading the database contents.
    
*   `CREATE` – gives the user permission to create databases and tables.
    
*   `INSERT` – allows the user to insert rows into the table.
    
*   `DELETE` – allows the user to delete rows from the table.
    
*   `UPDATE` – the user can update rows on the table.
    
*   `REFERENCES` – enable foreign key creation.
    
*   `RELOAD` – the user can run the [`FLUSH`](https://dev.mysql.com/doc/refman/8.0/en/flush.html) statement and commands.
    

```sql
GRANT CREATE, ALTER, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO ‘ata'@'localhost’;
```

> _Refer to the_ [_Privileges Provided by MySQL_](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html) _documentation for a complete list of all privileges. You may add, remove, combine permissions as you wish._

The result below shows granting permissions was successful.

![Granting limited MySQL permissions to the new user](https://adamtheautomator.com/wp-content/uploads/2022/04/image-301.png)

Granting limited MySQL permissions to the new user

4\. Next, run the below command to reload the privilege tables. Doing so will ensure that all the changes you have made so far immediately take effect.

```sql
FLUSH PRIVILEGES;
```

5\. Lastly, log out from MySQL.

```sql
exit
```

## Testing Your MySQL Server

So you’ve installed and secured your MySQL server and configured a new limited user account. Now’s the time to test whether everything works as you expected.

1\. Log in to MySQL as the `ata` user. This way, you can test whether this limited user account works as planned. The `-u` flag specifies the account to log in, while the `-p` flag invokes the password prompt.

> If you do not append the -p flag, the mysql account will try to log in without a password — which will fail since the login account has a password.

```bash
mysql -u ata -p
```

![Logging in to MySQL as the new user account](https://adamtheautomator.com/wp-content/uploads/2022/04/image-302.png)

Logging in to MySQL as the new user account

2\. Next, test the user privilege by running the below `CREATE` statement to create a new database named `ata_db`.

```sql
CREATE DATABASE ata_db;
```

You should see the Query OK message, indicating a successful database creation.

![Creating a new database](https://adamtheautomator.com/wp-content/uploads/2022/04/image-303.png)

Creating a new database

3\. Run the `show databases;` statement to see a list of all the databases that are available to use.

```bash
show databases;
```

You should see the ata\_db database you created earlier, as shown below.

![Listing all MySQL databases](https://adamtheautomator.com/wp-content/uploads/2022/04/image-304.png)

Listing all MySQL databases

## Conclusion

In this article, you have learned how to install the MySQL Ubuntu server and configure the server security settings. You also learned how to create a new user account with limited privileges and test the new user’s permissions by creating a new database.

At this point, you should have a fully functional MySQL Ubuntu server that you can use for your development and production needs. And if you prefer managing MySQL with a GUI, perhaps you could install a web front-end next?

Related:[How to Install phpMyAdmin Securely on Linux](https://adamtheautomator.com/install-phpmyadmin/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-mysql-on-ubuntu%2F&text=Install%20MySQL%20on%20Ubuntu%3A%20Get%20Your%20Database%20Server%20Up%20and%20Running)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Finstall-mysql-on-ubuntu%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-mysql-on-ubuntu%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Getting-Started-With-Mysql-Workbench-on-Ubuntu-Linux.jpg)

### [Getting Started With Mysql Workbench on Ubuntu Linux](/mysql-workbench-on-ubuntu/)

Learn how to get started with and use MySQL Workbench on Ubuntu Linux for connecting to MySQL databases in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/06/Fundamentals-of-ProxySQL-High-Availability-Galera-MySQL.jpg)

### [Fundamentals of ProxySQL, High Availability, & Galera MySQL](/proxysql/)

Learn how to create a highly available MySQL installation with ProxySQL and a Galera Cluster in this ATA Learning tutorial.

![](https://adamtheautomator.com/wp-content/uploads/2024/03/openldap-4.jpg)

### [How to Install and Configure an OpenLDAP Ubuntu Server](/openldap/)

Unlock the power of OpenLDAP on Ubuntu for centralized user authentication, seamless access control management, and enhanced directory services!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
