MySQL is the database behind a large share of web apps, from WordPress to custom PHP and Node.js projects. This guide shows the two ways to install it on Ubuntu and Debian, how to lock down the default install, and how root logs in on a fresh server.
Before you start
- A Linux VPS running Ubuntu 22.04 or 24.04, or Debian 12.
- A user with
sudorights, connected over SSH. Your login details are in your VPS provider's control panel. - No other database server on port 3306. If MariaDB is already installed, do not install MySQL alongside it.
Choose where to install from
You have two sources:
- Ubuntu's own packages. Ubuntu 22.04 and 24.04 ship MySQL 8.0 as
mysql-server. It is the simplest option and gets security updates through normalapt upgrade. It is not 8.4. - The official MySQL APT repository. Oracle publishes this repository so you can pick a release series, including MySQL 8.4 LTS. Use it when you want 8.4 on Ubuntu, or MySQL at all on Debian 12.
Note: Debian 12 does not ship MySQL. Its default-mysql-server package installs MariaDB. If MariaDB suits your app, read MySQL vs MariaDB before you decide.
Option 1: install from Ubuntu packages
sudo apt update
sudo apt install mysql-serverThe service starts on its own. Skip to Secure the install.
Option 2: install MySQL 8.4 from the MySQL APT repository
- Open the MySQL APT repository download page at
dev.mysql.com/downloads/repo/apt/in your browser and copy the link to the currentmysql-apt-config.debfile. The file name includes a version number that changes over time, so always use the current link. - Download and install it on the server, replacing the URL with the one you copied:
cd /tmp
wget https://dev.mysql.com/get/mysql-apt-config_X.Y.Z-1_all.deb
sudo apt install ./mysql-apt-config_X.Y.Z-1_all.deb- A text menu opens. Select MySQL Server & Cluster, choose the mysql-8.4-lts series, then choose Ok to save.
- Install the server:
sudo apt update
sudo apt install mysql-server- The installer asks for a root password. Choose your own long, unique password and keep it in a password manager.
Check the version you got:
mysql --versionThe output should mention 8.4. If it shows 8.0, apt picked Ubuntu's package; run apt policy mysql-server to see which repository won.
Tip: To change the series later, run sudo dpkg-reconfigure mysql-apt-config, then sudo apt update. Read the MySQL upgrade notes and take a backup first.
Secure the install
MySQL ships a script that removes test data and tightens defaults:
sudo mysql_secure_installationAnswer the prompts:
- VALIDATE PASSWORD component: optional. It rejects weak passwords for new accounts. Turn it on if several people create users.
- Remove anonymous users: yes.
- Disallow root login remotely: yes.
- Remove test database: yes.
- Reload privilege tables: yes.
How root logs in
How root authenticates depends on where MySQL came from.
- Ubuntu packages:
root@localhostuses theauth_socketplugin. It does not use a password at all. Instead, MySQL checks that the Linux user running the client isroot. You log in with:
sudo mysqlThis is secure by design: only someone with sudo on the server can get in as MySQL root.
- MySQL APT repository: root uses the password you chose during install:
mysql -u root -pCheck which plugin root uses:
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';auth_socket means sudo mysql; caching_sha2_password means a password login.
Warning: Do not switch root to password login just to suit an app. Create a dedicated user for each app instead, as shown in create a MySQL user and grant privileges.
Check it works
Confirm the service is running and enabled at boot:
systemctl status mysql
sudo systemctl is-enabled mysqlYou should see active (running) and enabled. Then check MySQL only listens locally:
sudo ss -ltnp | grep 3306A line showing 127.0.0.1:3306 means only local apps can connect. Finally, run a query:
sudo mysql -e "SELECT VERSION();"Troubleshooting
ERROR 1698 (28000): Access denied for user 'root'@'localhost': root usesauth_socket. Runsudo mysqlinstead ofmysql -u root -p.ERROR 1045 Access deniedwith a password: the password is wrong or you are logging in as the wrong account. Usesudo mysqlif root usesauth_socket.- Service fails to start: read
sudo journalctl -u mysql -n 50and/var/log/mysql/error.log. A leftover MariaDB install or a bad config line is the usual cause. - apt installs 8.0 instead of 8.4: the MySQL repository was not added or not selected. Rerun
sudo dpkg-reconfigure mysql-apt-configandsudo apt update. - Out-of-memory restarts on a small VPS: add a swap file or move to a larger plan.
Next steps
- Create the database your app will use: Create a database and user for a web app.
- Start backups on day one: Back up and restore MySQL with mysqldump.
