On this page
- Which MySQL 8 release should you install?
- How do you install MySQL on Ubuntu?
- How do you install MySQL on Debian with the official repository?
- How do you install MySQL on Rocky Linux or AlmaLinux?
- How do you secure a fresh MySQL install?
- How do you create a database and an application user?
- How do you reach MySQL remotely without opening port 3306?
- Which settings should you tune first?
- How do you back up, upgrade and troubleshoot MySQL?
- Questions
Which MySQL 8 release should you install?
Pick the 8.4 LTS series for anything new, and stay on 8.0 only when an existing application or a distro package ties you to it. Upstream ended regular support for 8.0 in 2026, while 8.4 receives fixes for years and uses the same SQL dialect, so most applications move across without code changes.
What each system offers out of the box differs:
- Ubuntu 24.04 and 22.04: the archive carries
mysql-serverfrom the 8.0 series, patched by the Ubuntu security team. Check the exact candidate withapt policy mysql-server. - Debian 12 and 13: there is no Oracle MySQL package at all;
default-mysql-serverpulls in MariaDB. Use the MySQL APT repository instead. - Rocky Linux 9 and AlmaLinux 9: AppStream ships
mysql-server8.0, and the MySQL community repository offers 8.4 LTS.
Before you start, log in as your normal sudo user (see your first SSH session) and update the system. A database likes memory: a small shop or blog runs well on 2 GB, while busy sites want 4 GB or more so the buffer pool can hold the working set.
How do you install MySQL on Ubuntu?
Install the package from the Ubuntu archive; the service starts on its own and root logs in through the local socket without a password.
sudo apt update
sudo apt install -y mysql-server
sudo systemctl enable --now mysql
mysql --version
sudo systemctl status mysql --no-pagerIf you prefer 8.4 LTS on Ubuntu, follow the Debian steps below: the MySQL APT repository supports both distributions and replaces the archive packages cleanly on a fresh server. Do not mix the two sources on a server that already holds data without taking a dump first.
How do you install MySQL on Debian with the official repository?
Download the small mysql-apt-config package from the MySQL APT repository page, install it, choose the 8.4 LTS series in the menu, then install mysql-server. The file name carries a version number that changes over time, so copy the current name from the download page into the variable.
sudo apt update
sudo apt install -y wget lsb-release gnupg
CFG=mysql-apt-config_0.8.36-1_all.deb # use the current file name
wget https://dev.mysql.com/get/$CFG
sudo apt install -y ./$CFG
sudo apt update
sudo apt install -y mysql-server
mysql --versionThe installer asks for a root password. Pick a long one and store it in a password manager; with this package root uses password authentication rather than the socket method Ubuntu uses. To switch series later, run sudo dpkg-reconfigure mysql-apt-config.
How do you install MySQL on Rocky Linux or AlmaLinux?
For the distro build, install mysql-server from AppStream and enable the mysqld unit. For 8.4 LTS, add the MySQL community repository and install mysql-community-server.
AppStream (8.0)
sudo dnf install -y mysql-server
sudo systemctl enable --now mysqld
mysql --versionMySQL community repository (8.4 LTS)
sudo dnf install -y https://repo.mysql.com/mysql84-community-release-el9-1.noarch.rpm
sudo dnf install -y mysql-community-server
sudo systemctl enable --now mysqld
sudo grep 'temporary password' /var/log/mysqld.logThe community build creates a random temporary root password on first start and prints it in the log line shown above. You must change it before root can do anything else, which the next step handles.
How do you secure a fresh MySQL install?
Run mysql_secure_installation once: it sets or confirms the root password policy, removes anonymous accounts and the test database, and blocks remote root logins.
sudo mysql_secure_installationAnswer yes to removing anonymous users, disallowing remote root login, dropping the test database and reloading privileges. The password validation component is optional; enable it if several people create accounts on this server.
Next, confirm the listener. MySQL should only accept TCP connections on the loopback address unless you have a firm reason to open it.
sudo ss -ltnp | grep -E ':3306|:33060'If you see 0.0.0.0:3306 or *:3306, set the bind address. On Ubuntu and Debian edit /etc/mysql/mysql.conf.d/mysqld.cnf; on Rocky and Alma edit /etc/my.cnf.d/mysql-server.cnf (AppStream) or /etc/my.cnf (community build). Under [mysqld] add:
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1Restart with sudo systemctl restart mysql (or mysqld on Rocky and Alma). Your host firewall should not allow 3306 either; the firewall tutorial shows a default-deny rule set.
How do you create a database and an application user?
Give every application its own database and its own account limited to that database, so a leak in one app cannot read another.
sudo mysql
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'Long-Random-Passphrase-42';
GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'localhost';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'shop'@'localhost';
EXIT;Test the account the way the application will use it:
mysql -u shop -p -h 127.0.0.1 shop -e 'SELECT CURRENT_USER(), VERSION();'New accounts use the caching_sha2_password plugin. Current client libraries for PHP, Python, Node.js and Go all speak it. In 8.4 the old mysql_native_password plugin is off by default; turn it back on only for a legacy client you cannot update. Finer control over roles and password rules is covered in MySQL users and privileges.
How do you reach MySQL remotely without opening port 3306?
Forward a local port through SSH. The database keeps listening only on 127.0.0.1, and your desktop tool connects to a port on your own machine that SSH carries to the server.
# on your own computer
ssh -N -L 3307:127.0.0.1:3306 deploy@198.51.100.24
# in a second terminal, or point a GUI client at 127.0.0.1:3307
mysql -u shop -p -h 127.0.0.1 -P 3307 shopBecause the tunnel ends on the server itself, the MySQL account still only needs the localhost host part. SSH key login makes this smooth; set it up with SSH keys and hardening. A longer walkthrough, including persistent tunnels and GUI clients, lives in remote database access over SSH tunnels.
If an application on a second server needs the database, prefer the same tunnel approach with autossh or a WireGuard link between the two machines over exposing the port publicly.
Which settings should you tune first?
Size the InnoDB buffer pool to the memory you can spare, cap connections at a realistic number and switch on the slow query log; those three changes matter far more than any long list of tweaks.
Create a small file so package upgrades never overwrite your changes, for example /etc/mysql/mysql.conf.d/zz-tuning.cnf on Ubuntu and Debian or /etc/my.cnf.d/zz-tuning.cnf on Rocky and Alma:
[mysqld]
innodb_buffer_pool_size = 1G
max_connections = 100
slow_query_log = ON
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1As a rough guide, give the buffer pool about half of RAM on a server that also runs a web stack, and up to 70 percent on a database-only machine. On Rocky and Alma create /var/log/mysql owned by mysql or point the slow log at /var/log/mysqld-slow.log. After a restart, read the values back:
mysql -u root -p -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size'; SHOW VARIABLES LIKE 'slow_query%';"Use EXPLAIN on the queries that land in the slow log; keys and indexes in MySQL shows how to read its output.
How do you back up, upgrade and troubleshoot MySQL?
Dump each database on a schedule, upgrade within a series through the normal package manager, and read the error log first when the service will not start.
mysqldump --single-transaction --routines --triggers -u root -p shop | gzip > shop-$(date +%F).sql.gz
sudo apt update && sudo apt upgrade # Ubuntu, Debian
sudo dnf upgrade # Rocky, Alma
sudo journalctl -u mysql -n 50 --no-pager # unit is mysqld on Rocky and Alma
sudo tail -n 50 /var/log/mysql/error.log # /var/log/mysqld.log on Rocky and AlmaCommon causes of a failed start are a typo in a config file, a full disk and a buffer pool larger than free memory. Back up and restore MySQL goes deeper, and the optional daily backup add-on described in daily backups gives you a whole-disk copy on top of your dumps.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-2 | small site or test database | 1 vCPU, 2 GB RAM, 50 GB NVMe | $7.20/mo | $43.20 for 6 months |
| A2-4 | room for a larger buffer pool | 2 vCPU, 4 GB RAM, 100 GB NVMe | $14.40/mo | $43.20 for 3 months |
| A4-8 | busy shop or several databases | 4 vCPU, 8 GB RAM, 180 GB NVMe | $28.80/mo | $86.40 for 3 months |
Questions about Install MySQL 8
Does Debian have a MySQL package?
No. Debian packages MariaDB as its MySQL-compatible server, so apt install mysql-server does not give you Oracle MySQL there. Add the MySQL APT repository to get the real MySQL 8.4 packages.
Should I pick MySQL 8.0 or 8.4?
Choose 8.4 LTS for new projects. Stay on 8.0 only if a vendor application demands it, and plan the move, because the 8.0 series has reached the end of its regular support window.
Why can root log in without a password on Ubuntu?
Ubuntu configures root with the auth_socket plugin, so only the Linux root user (through sudo mysql) can log in as MySQL root. That is safer than a password and needs no change.
How much RAM does MySQL need?
It starts in a few hundred megabytes. For real use, 2 GB suits a small site and 4 GB or more lets the buffer pool cache a busier data set. If you outgrow the plan, deploy a larger one and move with this migration guide.
Can I open port 3306 to the internet?
You can, but an SSH tunnel or a private WireGuard link is the safer default. If you must open it, allow only your fixed source address in the firewall and require TLS for the account with REQUIRE SSL.
Where are the MySQL config files?
On Ubuntu and Debian under /etc/mysql/, mainly mysql.conf.d/mysqld.cnf. On Rocky and Alma in /etc/my.cnf and /etc/my.cnf.d/.
More in Databases
- Install MariaDB
- Install PostgreSQL
- Install MongoDB
- PostgreSQL JSON, arrays and special types
- PostgreSQL views, triggers and CTEs
- PostgreSQL full-text search and encryption
- MySQL keys, indexes and full-text search
- MySQL JSON, views and stored procedures
- Apache Solr search server
- Install Redis or Valkey
- Install Memcached
- Apache Cassandra cluster
- Install Apache CouchDB
- InfluxDB for time series
- Install Elasticsearch
- Back up and restore MySQL and MariaDB
- Back up and restore PostgreSQL
- MySQL users, roles and passwords
- PostgreSQL roles and permissions
- Repair a crashed MySQL table
- PostgreSQL streaming replication
- MongoDB replica set
- PostGIS for location data
- Database insight with PgHero
- Couchbase Server
- ArangoDB multi-model database
- Remote database access over SSH