How to Install MySQL on Ubuntu and Other Linux Distributions

How to Install MySQL on Ubuntu and Other Linux Distributions

Reading time1 min
#mysql#database#ubuntu#linux#rhel

How to Install MySQL on Ubuntu and Other Linux Distributions

Installing MySQL takes one command. What differs between distros is the version you get, the service name, and how the root account logs in. This post covers Ubuntu first, then Debian and the RHEL family, then the first database, remote access and the usual errors.

Which version you get

DistroDefault MySQL
Ubuntu 22.04, 24.048.0
Ubuntu 26.048.4 LTS
RHEL, AlmaLinux, Rocky 98.0
RHEL, AlmaLinux, Rocky 108.4 LTS
Debiannone, default-mysql-server installs MariaDB

Oracle ended support for MySQL 8.0 in April 2026. For a new server, prefer a release that ships 8.4 LTS (Oracle also lists 9.7 LTS as an upgrade target), or install 8.4 from Oracle's own APT or Yum repository (download the mysql-apt-config or mysql84-community-release package from dev.mysql.com). On an existing 8.0 server, plan the upgrade to 8.4.

Install on Ubuntu

sudo apt update
sudo apt install mysql-server
systemctl status mysql
mysql --version

The service is called mysql and is started and enabled during install. By default it listens only on 127.0.0.1:3306.

Secure it

sudo mysql_secure_installation

The script offers the password validation component, removes anonymous users and the test database, and disables remote root login. On Ubuntu it skips the root password step: root@localhost uses the auth_socket plugin, which means only the system root user can log in as MySQL root, without a password. That is a sensible default, so leave it.

Log in as root:

sudo mysql

Install on other distros

RHEL, AlmaLinux, Rocky Linux:

sudo dnf install mysql-server               # EL9: MySQL 8.0
sudo dnf module install mysql:8.4/server     # EL9: MySQL 8.4 instead
sudo dnf install mysql8.4-server             # EL10
sudo systemctl enable --now mysqld
sudo mysql_secure_installation

The service is mysqld, and RHEL packages do not start it on install. The distro package initializes root with an empty password, and mysql_secure_installation sets one. If you install from Oracle's Yum repository instead, a temporary root password is generated on first start:

sudo grep 'temporary password' /var/log/mysqld.log

MySQL and MariaDB packages conflict, so a host runs one or the other.

Debian: mysql-server is not in any Debian stable release. Either install MariaDB (sudo apt install mariadb-server), which most applications accept as a drop-in, or add Oracle's APT repository for real MySQL.

Create a database and user

Never let an application connect as root. Create a dedicated user with rights on one database:

CREATE DATABASE myapp;
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'use-a-long-random-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';

FLUSH PRIVILEGES is not needed after CREATE USER and GRANT; it only matters when someone edits the grant tables directly. The default character set in 8.x is already utf8mb4.

Test the new account:

mysql -u myapp -p myapp

Allow remote connections

Only do this if the application runs on another host. On Ubuntu edit /etc/mysql/mysql.conf.d/mysqld.cnf, on RHEL a file in /etc/my.cnf.d/:

[mysqld]
bind-address = 10.0.0.5

Use the server's private address rather than 0.0.0.0 when you can. Then create a user for the client's address, restart, and open the port only to the application network:

CREATE USER 'myapp'@'10.0.0.%' IDENTIFIED BY 'use-a-long-random-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'10.0.0.%';
sudo systemctl restart mysql
sudo ufw allow from 10.0.0.0/24 to any port 3306 proto tcp
# RHEL: sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" service name="mysql" accept'
#       sudo firewall-cmd --reload

Never expose 3306 to the whole internet. Use a VPN, an SSH tunnel or a private network.

Start, stop and enable

sudo systemctl start mysql        # mysqld on RHEL
sudo systemctl stop mysql
sudo systemctl restart mysql
sudo systemctl enable mysql       # start at boot

Troubleshooting

ERROR 1698 (28000): Access denied for user 'root'@'localhost' on Ubuntu. You ran mysql -u root -p as a normal user, but root authenticates through auth_socket. Use sudo mysql. If you need a password-based admin for tools, create a separate account instead of changing root:

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'use-a-long-random-password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;

ERROR 2002 (HY000): Can't connect to local MySQL server through socket. The server is not running. Check why:

systemctl status mysql --no-pager
sudo journalctl -u mysql -n 50
sudo tail -n 50 /var/log/mysql/error.log     # /var/log/mysqld.log on RHEL

Service fails to start. The common causes are a full disk, port 3306 already taken (sudo ss -ltnp 'sport = :3306'), or a data directory moved without updating AppArmor (Ubuntu) or SELinux labels (RHEL). Both security layers only allow MySQL to use its default paths unless told otherwise.

Access denied for the application user from another host. MySQL matches users by name and host. 'myapp'@'localhost' and 'myapp'@'10.0.0.%' are two different accounts with separate passwords and grants. List them with SELECT user, host FROM mysql.user;.

Quick reference

sudo apt install mysql-server              # Ubuntu, service: mysql
sudo dnf install mysql8.4-server           # EL10 (EL9: mysql-server), service: mysqld
sudo systemctl enable --now mysqld         # RHEL does not autostart
sudo mysql_secure_installation
sudo mysql                                 # root via auth_socket on Ubuntu
mysql -u myapp -p myapp                    # app user
sudo journalctl -u mysql -n 50             # why it did not start