Overview
MySQL is one of the most widely used relational database systems in the world, and Ubuntu is one of the most common Linux distributions you’ll find it running on. Whether you’re setting up a new VPS SSD Hosting instance, deploying a web app, or migrating off a shared hosting plan, getting MySQL installed and secured correctly is a foundational step you don’t want to rush.
This guide covers MySQL installation on Ubuntu 20.04 (Focal), 22.04 (Jammy), and 24.04 (Noble). The steps are mostly the same across all three, and I’ll call out version-specific differences where they matter. Ubuntu’s default repositories ship MySQL 8.0 on 20.04 and 22.04, and MySQL 8.4 on 24.04 — both are covered here.
One thing beginners consistently run into: on Ubuntu, the MySQL root account uses socket-based authentication by default, not a password. That means mysql_secure_installation can fail or behave unexpectedly unless you know what you’re dealing with. I’ll walk through the fix in detail.
Prerequisites
- Ubuntu 20.04, 22.04, or 24.04 server (desktop installs work too, but these steps assume a server context)
- A user account with
sudoprivileges — root login works, but using a sudo user is better practice - SSH access to the server, or a local terminal session
- Basic familiarity with the Linux command line (running commands, editing files)
- A live internet connection on the server for package downloads
Step-by-Step: Installing MySQL on Ubuntu
Step 1: Update the Package Index
Before installing anything, refresh the package list so apt knows about the latest available versions. Skipping this step is one of the most common reasons installs pull outdated packages.
sudo apt update
Step 2: Install MySQL Server
Install the mysql-server package. This pulls in the MySQL server, client tools, and all required dependencies in one shot.
sudo apt install mysql-server -y
The installation runs without prompting you for a root password — that’s intentional on Ubuntu. Don’t worry, you’ll set one shortly.
📝 Note: On Ubuntu 24.04, this installs MySQL 8.4. On 20.04 and 22.04, you’ll get MySQL 8.0. You can verify after install with mysql --version.
Step 3: Verify MySQL Is Running
Once the install finishes, MySQL should start automatically. Confirm it’s active before going further.
sudo systemctl status mysql
You want to see active (running) in green. If it shows inactive or failed, start it manually:
sudo systemctl start mysql
sudo systemctl enable mysql
The enable command makes MySQL start automatically on reboot — something you almost certainly want on a production server.
Step 4: Understand the Root Auth Situation Before Proceeding
This is the part most guides gloss over, and it’s the source of a lot of frustration. On Ubuntu, the MySQL root user is configured to authenticate via the auth_socket plugin by default. That means it authenticates based on your Linux system user, not a MySQL password.
The practical effect: you can log in as root without a password if you’re running commands as the Linux root user or with sudo. But it also means mysql_secure_installation may prompt you for a root password that doesn’t exist yet, get confused, and either fail silently or error out.
To avoid that, switch root to password-based authentication first. Log into MySQL using sudo:
sudo mysql
Then run the following SQL. Replace YourStrongPassword with an actual secure password — and don’t leave the example value in place on any real server.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongPassword';
FLUSH PRIVILEGES;
EXIT;
📝 Note: On MySQL 8.4 (Ubuntu 24.04), mysql_native_password is disabled by default as a security measure. If you get an error about an unknown plugin, use caching_sha2_password instead: IDENTIFIED WITH caching_sha2_password BY 'YourStrongPassword'. This is more secure anyway.
⚠ Warning: Do not use weak or example passwords on any internet-facing server. If your VPS has a public IP — which it almost certainly does — a database with a weak root password is a serious security risk.
Step 5: Run the Security Script
Now that root has a password, run mysql_secure_installation. This script walks you through several important hardening steps: removing anonymous users, disabling remote root login, removing the test database, and reloading privilege tables.
sudo mysql_secure_installation
The script will ask a series of yes/no questions. Here’s what I’d recommend for a production server:
- Validate password component — Enable it. It enforces password complexity rules.
- Change the root password — You already set one, so say no unless you want to change it again.
- Remove anonymous users — Yes. There’s no reason to keep them.
- Disallow root login remotely — Yes. Root should never log in over the network.
- Remove test database — Yes. It’s a security best practice.
- Reload privilege tables — Yes.
Step 6: Log In and Confirm Access
Test that password authentication works correctly for root:
mysql -u root -p
Enter the password you set in Step 4. You should land at the MySQL shell prompt. Run a quick sanity check:
SHOW DATABASES;
SELECT user, host, plugin FROM mysql.user;
The plugin column in that second query will show you how each user authenticates. Root should now show mysql_native_password or caching_sha2_password, not auth_socket.
Step 7: Create a Dedicated Database User (Recommended)
Running your application as the MySQL root user is bad practice. Create a dedicated user with only the permissions it needs. Here’s an example for a web app database:
CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'AnotherStrongPassword';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace myapp_db, myapp_user, and the password with your actual values. Granting privileges only on a specific database means that if this account is ever compromised, the attacker can’t touch other databases on the same server.
📝 Note: If you’re hosting multiple sites on a single VPS — a common setup — create a separate database and user for each one. It’s a few extra minutes up front and it contains any potential damage.
Common Issues and Troubleshooting
mysql_secure_installation Keeps Asking for a Password That Doesn’t Work
This is almost always the socket authentication issue described in Step 4. If you skipped that step or it didn’t apply correctly, the script can’t authenticate and just loops or fails.
Go back and run the sudo mysql flow from Step 4, confirm the ALTER USER command ran without errors, and try the script again. If you’re on Ubuntu 24.04 and got a plugin error, re-run using caching_sha2_password.
ERROR 1045 (28000): Access Denied for User ‘root’@’localhost’
You’re getting this when trying to log in with mysql -u root -p. It usually means either the password is wrong, or root is still using socket auth and you’re not connecting via sudo. Try sudo mysql first to confirm you can get in at all, then revisit Step 4 to switch the auth method.
Can’t Connect to Local MySQL Server Through Socket ‘/var/run/mysqld/mysqld.sock’
The socket file is missing, which means MySQL isn’t running. Check the service status with sudo systemctl status mysql and look at the journal for the actual error:
sudo journalctl -u mysql --no-pager -n 50
Common causes are disk full (MySQL can’t write its data files), permissions issues on /var/lib/mysql, or a corrupted InnoDB tablespace from an unclean shutdown. The journal output will tell you which one.
MySQL Starts But Immediately Stops (Crash Loop)
Usually a configuration error or disk space issue. Check available disk space first:
df -h
If the disk is fine, look at the MySQL error log directly:
sudo tail -n 100 /var/log/mysql/error.log
On a VPS with limited RAM, MySQL may also be getting killed by the OOM killer. Check with dmesg | grep -i oom. If that’s the issue, tuning /etc/mysql/mysql.conf.d/mysqld.cnf to reduce innodb_buffer_pool_size usually resolves it.
Package ‘mysql-server’ Has No Installation Candidate
This means your apt sources are either outdated or misconfigured. Run sudo apt update and try again. If you’re on a minimal Ubuntu install (common on cloud VPS images), you may need to add the universe repository:
sudo add-apt-repository universe
sudo apt update
sudo apt install mysql-server -y
FAQ
Frequently Asked Questions
Which version of MySQL does Ubuntu install by default?
Ubuntu 20.04 and 22.04 install MySQL 8.0 from the default apt repositories. Ubuntu 24.04 ships MySQL 8.4. If you need a specific version that isn’t available in the default repos, you can add the official MySQL APT repository from dev.mysql.com and choose your version from there.
How do I allow remote connections to MySQL?
By default, MySQL on Ubuntu only listens on localhost (127.0.0.1). To allow remote connections, edit /etc/mysql/mysql.conf.d/mysqld.cnf and change the bind-address to 0.0.0.0 (or a specific IP), then restart MySQL with sudo systemctl restart mysql. You’ll also need to create a MySQL user with a host value other than ‘localhost’, and make sure your firewall allows port 3306. Be cautious — exposing MySQL directly to the internet without additional controls is a security risk.
How do I change the MySQL root password on Ubuntu?
Log in with sudo mysql (if socket auth is still active) or mysql -u root -p, then run: ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘NewPassword’; followed by FLUSH PRIVILEGES; If you’ve forgotten the root password entirely, you’ll need to restart MySQL in skip-grant-tables mode — look up the procedure for your specific MySQL version, as the steps differ between 8.0 and 8.4.
Is MySQL or MariaDB better for Ubuntu?
For most web hosting setups, either will work. MariaDB is a drop-in MySQL alternative with some performance differences and a slightly different feature set. If you’re following specific documentation for an app (like certain WordPress configurations or a framework with MySQL-specific queries), stick with MySQL to avoid compatibility surprises. MariaDB can be installed with sudo apt install mariadb-server and follows a similar setup process.
Do I need to install MySQL separately if I'm using managed WordPress hosting?
No. With Host & Tech managed WordPress hosting, MySQL is pre-installed, configured, and maintained as part of the stack — you don’t touch the database server directly. MySQL installation is something you’d handle yourself on a VPS or dedicated server where you control the full environment.