Both MySQL and MariaDB are powerful relational database management systems (RDBMS). They are popular choices for numerous web and software applications that require databases to store and manage data efficiently.

While MySQL has been around for decades and is known for its robustness and performance, MariaDB emerged as a fork of MySQL, promising to be an open-source and more community-driven solution. It's also intended to be a drop-in replacement for MySQL, ensuring compatibility between the two.

Ubuntu, being one of the most popular Linux distributions, has straightforward processes to install either database system. Depending on your requirements, you can choose to install MySQL or MariaDB, and the steps are quite similar.

Steps to install MySQL/MariaDB on Ubuntu:

  1. Launch terminal.
  2. Update apt's package list.
    $ sudo apt update
    [sudo] password for user: 
    Hit:1 http://ports.ubuntu.com/ubuntu-ports kinetic-security InRelease
    Hit:2 http://us.ports.ubuntu.com/ubuntu-ports kinetic InRelease
    Hit:3 http://us.ports.ubuntu.com/ubuntu-ports kinetic-updates InRelease
    Hit:4 http://us.ports.ubuntu.com/ubuntu-ports kinetic-backports InRelease
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    7 packages can be upgraded. Run 'apt list --upgradable' to see them.
  3. Install mysql-server or mariadb-server package using apt.
    $ sudo apt install --assume-yes mysql-server
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
      libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7a
      libevent-pthreads-2.1-7 libfcgi-bin libfcgi-perl libfcgi0ldbl
      libhtml-template-perl libmecab2 libprotobuf-lite23 mecab-ipadic
      mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0
      mysql-common mysql-server-8.0 mysql-server-core-8.0
    Suggested packages:
      libipc-sharedcache-perl mailx tinyca
    The following NEW packages will be installed:
      libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7a
      libevent-pthreads-2.1-7 libfcgi-bin libfcgi-perl libfcgi0ldbl
      libhtml-template-perl libmecab2 libprotobuf-lite23 mecab-ipadic
      mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0
      mysql-common mysql-server mysql-server-8.0 mysql-server-core-8.0
    0 upgraded, 20 newly installed, 0 to remove and 10 not upgraded.
    Need to get 28.0 MB of archives.
    After this operation, 239 MB of additional disk space will be used.
    ##### snipped

    Replace package name with mariadb-server to install MariaDB.

    Installed service is automatically started, and set to start during system boot.
    Related: How to manage MySQL or MariaDB service in Linux

  4. Set password for root user.

    Skipping this step will cause error in the mysql_secure_installation step below.

  5. Run mysql_secure_installation (recommended).
    $ sudo mysql_secure_installation
    
    Securing the MySQL server deployment.
    
    Enter password for user root: 
  6. Enable password validation. (recommended).
    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: y
    
  7. Set password level for validation.
    There are three levels of password validation policy:
    
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
    
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
  8. Don't change root password.
    Using existing password for root.
    
    Estimated strength of the password: 50 
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

    Use the same password as set prior to running mysql_secure_installation.

  9. Remove guest account.
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.
    
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    
  10. Allow root to only login locally.
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    
  11. Remove test database.
    By default, MySQL comes with a database named 'test' that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.
    
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  12. Reload privilege table.
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
  13. Try connecting to localhost to test.
    $ mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 27
    Server version: 8.0.32-0ubuntu0.22.10.2 (Ubuntu)
    
    Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 

This guide is tested on Ubuntu:

Version Code Name
22.04 LTS Jammy Jellyfish
23.10 Mantic Minotaur
24.04 LTS Noble Numbat
Discuss the article:

Comment anonymously. Login not required.