How To Install MySQL Server on Ubuntu 22.04/Ubuntu 20.04

MySQL community server is a free open-source, popular, and cross-platform database management system, which supports both SQL and NoSQL and has a pluggable storage engine architecture.

Additionally, it also comes with multiple database connectors for different programming languages, allowing you to develop applications using any of the well-known languages, and many other features.

It has many use cases under document storage, cloud, high availability systems, IoT (Internet of Things), hadoop, big data, data warehousing, LAMP or LEMP stack for supporting high-volume websites/apps, and much more.

In this article, we will explain a fresh installation of the MySQL 8.0 database system on Ubuntu 22.04, Ubuntu 20.04, and Ubuntu 18.04 releases. Before we move on to the actual installation steps, let’s look at a summary of:

What’s New in MySQL 8.0

  • The database now incorporates a transactional data dictionary.
  • Comes with Atomic DDL statement support.
  • Enhanced security and account management.
  • Improvements to resource management.
  • Several InnoDB enhancements.
  • A new type of backup lock.
  • The default character set has changed to utf8mb4 from latin1.
  • A couple of JSON enhancements.
  • Comes with regular expression support using International Components for Unicode (ICU).
  • New error logging which now uses the MySQL component architecture.
  • Enhancements to MySQL replication.
  • Supports common table expressions(both non-recursive and recursive).
  • Has an enhanced optimizer.
  • Additional window functions and more.

Step 1: Udating Ubuntu Server

Luckily, you can use the default APT package repository for installing the MySQL server, client, and other components. At the time of writing this article, the available MySQL version is 8.0.28.

To install it, you must update the package index on your Ubuntu server by running the following apt commands.

$ sudo apt update
$ sudo apt upgrade

Next, use the apt-cache command (queries the package cache) to search for MySQL server and client packages on your Ubuntu server.

$ apt-cache search mysql-server


mysql-server - MySQL database server (metapackage depending on the latest version)
mysql-server-8.0 - MySQL database server binaries and system database setup
mysql-server-core-8.0 - MySQL database server binaries
default-mysql-server - MySQL database server binaries and system database setup (metapackage)
default-mysql-server-core - MySQL database server binaries (metapackage)
mariadb-server-10.6 - MariaDB database server binaries
mariadb-server-core-10.6 - MariaDB database core server files

Step 2: Install MySQL on Ubuntu Server

Then run the following command to install packages for the MySQL community server, client, and the database common files.

$ sudo apt-get install mysql-server
Install MySQL 8.0 in Ubuntu 18.04
Install MySQL 8.0 in Ubuntu 18.04

The default MySQL configuration file and an error log file will be located at the following location, you can view them using the nano editor or tail command as shown.

$ sudo nano /etc/my.cnf
$ sudo tail -f /var/log/mysql/error.log

Step 3: Managing MySQL Server via Systemd

On Ubuntu, after installing the MySQL server package, its service(s) are usually started automatically once the package is configured. You can check if the MySQL server is up and running using the following systemctl command.

$ sudo systemctl status mysql.service
Check MySQL Server Status
Check MySQL Server Status

If for one reason or the other, it isn’t auto-started, use the commands below to start and enable it to start at system boot time, as follows.

$ sudo systemctl start mysql.service
$ sudo systemctl status mysql.service
$ sudo systemctl start mysql.service
$ sudo systemctl restart mysql.service

Step 4: Set MySQL Root Password

First, open up the MySQL prompt:

$ sudo mysql

Next, run the following ALTER USER command to set the MySQL root password using the mysql_native_password authentication method as shown.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR-STRONG-PASSWORD';

After setting the password, exit the MySQL prompt:

mysql> exit
Set MySQL Password in Ubuntu
Set MySQL Password in Ubuntu

Step 5: Secure MySQL Server Installation

By default, the MySQL installation is unsecure. To secure it, run the security script which comes with the binary package.

$ sudo mysql_secure_installation

You will be asked to enter the root password you just set above.

Then enter yes/y to the following security questions:

  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
  • Remove the test database and access to it? (Press y|Y for Yes, any other key for No) : y
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Secure MySQL in Ubuntu
Secure MySQL in Ubuntu

To further secure your MySQL server, read our article 12 MySQL Security Best Practices for Linux.

Step 6: Create MySQL Database with User

The MySQL installation comes with a root account, which you can use to manage the complete database, but for security reasons, I suggest you create a normal user account with limited privileges to databases as shown.

$ mysql -u root -p
mysql> CREATE DATABASE tecmintdb;
mysql> CREATE USER 'aaron'@'localhost' IDENTIFIED BY 'rig!43B#web1rdl0gn1';
mysql> GRANT ALL PRIVILEGES ON tecmintdb.* TO 'aaron'@'localhost';

Now to log in as your new MySQL user, use the following command.

$ mysql -u aaron -p
Create MySQL Database with User
Create MySQL Database with User

Step 7: Install Extra MySQL Products and Components

In addition, you can install extra MySQL components that you feel you need in order to work with the server, such as mysql-workbench-community, libmysqlclient18, and many others.

$ sudo apt-get update
$ sudo apt-get install mysql-workbench-community libmysqlclient18

Finally, to access the MySQL shell, issue the following command.

$ sudo mysql -u root -p
Connect to MySQL Server
Connect to MySQL Server

That’s It! In this article, we have explained how to install MySQL in Ubuntu 22.04 and Ubuntu 20.04. If you have any questions or thoughts to share, use the comment form below to reach us.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

17 thoughts on “How To Install MySQL Server on Ubuntu 22.04/Ubuntu 20.04”

  1. Hi

    Installation on Ubuntu 18.04 is failing like this

    dpkg: error processing package mysql-community-server (--configure): installed mysql-community-server package post-installation script subprocess returned error exit status 137dpkg: dependency problems prevent configuration of mysql-server: mysql-server depends on mysql-community-server (= 8.0.18-1ubuntu18.04); however: Package mysql-community-server is not configured yet.dpkg: error processing package mysql-server (--configure): dependency problems - leaving unconfiguredProcessing triggers for libc-bin (2.27-3ubuntu1) ...No apport report written because the error message indicates its a followup error from a previous failure.E: Sub-process /usr/bin/dpkg returned an error code (1)

    Do you have an idea regarding this?

    Reply
  2. mysql-server : Depends: mysql-community-server (= 8.0.18-1ubuntu18.04) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

    sudo add-apt-repository universe has not fixed the problem. Please help

    Reply
  3. hi i am from india and i’m getting follwing error can any one help me out
    The repository ‘http://repo.mysql.com/apt/ubuntu bionic InRelease’ is not signed.

    please help me asap.

    Reply
    • There is a new deb file provided by mysql that has the correct signature.

      # wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
      

      (notice the 8.13 instead of 8.10)

      Reply
  4. Hi! I’m from Argentina! I can’t install MySQL 8 and i don’t know why. I followed your steps, but when i see the “configuring” box, i don’t see Mysql8. In the box I see “Mysql 5.7” i and don’t have another option. Then, another problem, is that the system never asks for the password. Can you help me? Sorry for my bad English!

    Reply
  5. At the beginning of step 3, if you are getting something along the lines of: Table ‘mysql.user’ doesn’t exist:ERROR, be sure to run: “sudo mysql_upgrade -u root“.

    Reply
  6. Package libmysqlclient18 is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘libmysqlclient18‘ has no installation candidate

    May anyone help me to solve this? I am using Ubuntu 18.04 and followed the tutorial except “Secure MySQL Server Installation”, thanks!

    Reply
    • @Needhelp

      We are here to help!

      Try to install package libmysqlclient20 instead, using this command:

      $ sudo apt install libmysqlclient20
      
      Reply
  7. In some newer Ubuntu 18.04 installations, you will run into the following scenario:

    The following packages have unmet dependencies:
    mysql-server : Depends: mysql-community-server (= 8.0.12-1ubuntu18.04) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

    It is necessary to add the ‘universe’ repository with the following command:

    $ sudo add-apt-repository universe
    

    After that, attempt mysql-server installation and it will be fine.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.