Installing MySQL on Ubuntu
MySQL is one of the most widely used relational databases (a database that stores data in tables with rows and columns) in the world. If you run a WordPress site, a PHP app, or many web frameworks, there is a good chance MySQL is involved. On Ubuntu you can install it in a couple of minutes with apt, the package manager that ships with the system. This page walks you through installing the official MySQL server, the popular drop-in alternative MariaDB, starting the service, and connecting for the first time.
MySQL vs MariaDB — which should you install?
Before you install anything, it helps to know there are two closely related options. MariaDB is a community-built fork (a separate project that started from a copy of MySQL’s code) created by MySQL’s original developers after Oracle acquired MySQL. For most everyday use they behave almost identically — same SQL, same client commands, same default port (3306).
mysql-server | mariadb-server | |
|---|---|---|
| Maintained by | Oracle | MariaDB Foundation (community) |
| In Ubuntu’s default repos | Yes (recent versions) | Yes (default “mysql” on many Ubuntu installs) |
| Client command | mysql | mysql (same client) |
| Default port | 3306 | 3306 |
| License | Open-core (some paid features) | Fully open source (GPL) |
| Best when | You need exact Oracle MySQL compatibility, MySQL Shell, or features like the X Protocol | You want a fully free, fast, drop-in MySQL replacement |
When to use MariaDB: general web apps, self-hosted tools, and anything where you just want “a MySQL-compatible database.” It is the safer default on Ubuntu.
When to use Oracle’s MySQL: you need official MySQL behavior to match a production system, or you depend on a specific MySQL-only feature. Pick ONE — do not install both, since they both want port 3306 and the same data paths.
Installing MySQL server
First refresh your package lists so apt knows about the newest versions, then install the server package.
sudo apt update
sudo apt install -y mysql-server
Output:
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
mysql-server mysql-server-8.0 mysql-client-core-8.0 ...
Setting up mysql-server-8.0 (8.0.39-0ubuntu0.24.04.1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service ...
On Ubuntu 22.04 and 24.04 LTS this installs MySQL 8.0, which is started automatically after install.
The MariaDB alternative
If you prefer MariaDB, install its server package instead. The steps are identical otherwise.
sudo apt update
sudo apt install -y mariadb-server
Do NOT install
mysql-serverandmariadb-serveron the same machine. They conflict over the data directory (/var/lib/mysql) and the network port, and apt may refuse or leave you with a broken setup. Choose one.
Managing the service with systemd
Ubuntu manages the database as a background service (a program that keeps running on its own) using systemd, the standard Ubuntu service manager. The service is called mysql for Oracle’s MySQL and mariadb for MariaDB.
Check that it is running:
sudo systemctl status mysql
Output:
● mysql.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
Active: active (running) since Mon 2026-06-15 09:14:02 UTC; 30s ago
Main PID: 2417 (mysqld)
Status: "Server is operational"
The word active (running) means it is up, and enabled means it will start again automatically after a reboot. The common commands you will use:
sudo systemctl start mysql # start it now
sudo systemctl stop mysql # stop it now
sudo systemctl restart mysql # restart (e.g. after a config change)
sudo systemctl enable mysql # start on every boot
If you installed MariaDB, swap
mysqlformariadbin these commands. The two services are never both present, so use the one that matches your install.
Connecting with sudo mysql
On a fresh MySQL 8.0 install on Ubuntu, the root database user is configured to use auth_socket authentication. In plain terms: instead of a password, MySQL checks which Linux user you are. So the Linux root account (reached through sudo) is allowed straight in — no password needed yet.
Connect to the database shell:
sudo mysql
Output:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.39-0ubuntu0.24.04.1 (Ubuntu)
mysql>
You are now at the mysql> prompt, where you type SQL. Try listing the built-in databases, then quit:
SHOW DATABASES;
EXIT;
Output:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
Verifying the version
It is good practice to confirm exactly which version you got, both for the client and the running server.
mysql --version
Output:
mysql Ver 8.0.39-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))
To check the server version from inside the shell:
sudo mysql -e "SELECT VERSION();"
Output:
+-------------------------+
| VERSION() |
+-------------------------+
| 8.0.39-0ubuntu0.24.04.1 |
+-------------------------+
On a MariaDB install the version string will read something like mariadb Ver 15.1 Distrib 10.11.x-MariaDB, which is how you can tell at a glance which one you are running.
Where things live
Knowing the key paths helps when something goes wrong:
| Path | What it holds |
|---|---|
/var/lib/mysql | The actual data files for your databases |
/etc/mysql/ | Configuration (my.cnf and mysql.conf.d/) |
/var/log/mysql/error.log | The error log — check here first when the service won’t start |
Port 3306 | The network port MySQL/MariaDB listens on |
If the service fails to start, run sudo journalctl -u mysql -n 50 to see the recent systemd log lines, then check the error log above.
Best Practices
- Run
sudo apt updatebefore installing so you get the latest patched version from Ubuntu’s repos. - Pick MySQL or MariaDB and stick with one machine, one engine — never install both side by side.
- Right after installing, run
sudo mysql_secure_installationto set a strong password policy and remove insecure defaults (see the linked page below). - Keep
enableon so the database restarts automatically after a reboot. - By default MySQL listens only on
127.0.0.1(localhost). Leave it that way unless you genuinely need remote access, and useufw(Ubuntu’s firewall) to control port 3306 if you do. - Watch
/var/log/mysql/error.logandsystemctl statusas your first stop for any startup problem.