How to install MariaDB or MySQL server on Ubuntu

Installing MariaDB or MySQL from the Ubuntu repositories gives applications a local SQL server that follows the operating system's package, service, and security-update model. Choose the server family before installing packages, because the two families share client names, configuration paths, and service-management expectations.

Ubuntu ships native apt packages for both server families. On Ubuntu 26.04 LTS, mariadb-server installs MariaDB 11.8 from main and mysql-server installs MySQL 8.4 from the Ubuntu security and main repositories. Older supported releases can install different major versions or repository components, so check the package candidate before changing a production host.

Only one server family should remain installed on the same host, because the server packages replace overlapping client and server components. A completed install should leave the selected systemd unit enabled and active, run the matching secure-installation script, and create a dedicated application user instead of reusing the administrative root account.

Steps to install MariaDB or MySQL server on Ubuntu:

  1. Open a terminal with an account that can use sudo.
  2. Refresh the apt package index before installing the server package.
    $ sudo apt update
    Hit:1 http://archive.ubuntu.com/ubuntu resolute InRelease
    Hit:2 http://archive.ubuntu.com/ubuntu resolute-updates InRelease
    Hit:3 http://security.ubuntu.com/ubuntu resolute-security InRelease
    Reading package lists... Done
  3. Check the package candidates for the current Ubuntu release.
    $ apt-cache policy mariadb-server mysql-server
    mariadb-server:
      Installed: (none)
      Candidate: 1:11.8.6-5
    ##### snipped #####
    mysql-server:
      Installed: (none)
      Candidate: 8.4.9-0ubuntu0.26.04.1
    ##### snipped #####

    Ubuntu 26.04 LTS publishes MariaDB 11.8 and MySQL 8.4 through the native repositories. If an older LTS host cannot find mariadb-server, enable the standard universe component for that release before retrying.

  4. Decide which server family should stay installed on the host.

    Install only one server family at a time. Current MariaDB and MySQL packages replace overlapping client libraries, helper packages, and service assumptions, so switching families is cleaner after removing the previously installed database server first.

  5. Install MariaDB from the Ubuntu repositories.
    $ sudo apt install --assume-yes mariadb-server
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following additional packages will be installed:
      galera-4 mariadb-client mariadb-client-core mariadb-common
      mariadb-server-core
    ##### snipped #####
    Setting up mariadb-server (1:11.8.6-5) ...

    Install MySQL instead with

    $ sudo apt install --assume-yes mysql-server
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following additional packages will be installed:
      mysql-client mysql-common mysql-server-core
    ##### snipped #####
    Setting up mysql-server (8.4.9-0ubuntu0.26.04.1) ...
  6. Enable the selected database service at boot and start it now.
    $ sudo systemctl enable --now mariadb

    Use mysql instead of mariadb when mysql-server was installed.

  7. Confirm the database service is enabled at boot.
    $ sudo systemctl is-enabled mariadb
    enabled

    Use

    $ sudo systemctl is-enabled mysql

    when MySQL was installed.

  8. Confirm the database service is currently running.
    $ sudo systemctl is-active mariadb
    active

    Use

    $ sudo systemctl is-active mysql

    when MySQL was installed. If the service is not active, inspect

    $ sudo journalctl -u mariadb

    or

    $ sudo journalctl -u mysql

    before retrying the start command.

  9. Run the secure-installation utility for the installed server family.
    $ sudo mariadb-secure-installation
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    Enter current password for root (enter for none):
    ##### snipped #####
    Remove anonymous users? [Y/n] Y
    Disallow root login remotely? [Y/n] Y
    Remove test database and access to it? [Y/n] Y
    Reload privilege tables now? [Y/n] Y

    MariaDB on current Ubuntu releases uses mariadb-secure-installation. Use

    $ sudo mysql_secure_installation

    only when mysql-server was installed; do not rely on that command being present after installing MariaDB.

    MariaDB 10.4 and later on Debian and Ubuntu normally use unix_socket authentication for root by default, so pressing Enter at the current-password prompt is expected on a fresh install. MySQL can instead prompt to install the validate_password component and to set or change the root password.

  10. Apply the common safe answers during the secure-installation prompts.

    Keep anonymous users removed, disallow remote root login, remove the test database, and reload privileges. If local console administration should remain passwordless on MariaDB, leave root on unix_socket authentication and use sudo mariadb for local maintenance.

  11. Open a local administrative SQL session.
    $ sudo mariadb
    MariaDB [(none)]> SELECT VERSION();
    +------------------------------+
    | VERSION()                    |
    +------------------------------+
    | 11.8.6-MariaDB-5 from Ubuntu |
    +------------------------------+
    1 row in set (0.000 sec)

    For MySQL, use

    $ sudo mysql

    when the root account uses socket-based authentication, or

    $ mysql -u root -p

    when a root password was set during installation or by mysql_secure_installation. Related: How to reset MySQL or MariaDB root password

  12. Create an application database and a dedicated database user.
    MariaDB [(none)]> CREATE DATABASE appdb;
    Query OK, 1 row affected (0.001 sec)
    
    MariaDB [(none)]> CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPasswordHere!';
    Query OK, 0 rows affected (0.001 sec)
    
    MariaDB [(none)]> GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
    Query OK, 0 rows affected (0.001 sec)

    Application credentials should stay separate from root. If the application connects from another host, replace localhost with that specific client host or subnet instead of using a broad wildcard when possible.

  13. Exit the SQL prompt and verify that the new account can connect.
    MariaDB [(none)]> exit
    Bye
    
    $ mysql -u appuser -p appdb
    Enter password:
    mysql> SELECT CURRENT_USER();
    +-------------------+
    | CURRENT_USER()    |
    +-------------------+
    | appuser@localhost |
    +-------------------+
    1 row in set (0.00 sec)

    The same connection test works against MariaDB or MySQL because both server families speak the MySQL client protocol.