Last updated: 23 October 2026. Rewritten for current Ubuntu LTS releases.
This guide installs MySQL on Ubuntu 22.04, 24.04 or 26.04 LTS from Ubuntu's own packages, secures it, creates a database and a user for your app, and checks that everything works. It takes about 10 minutes. Once it's done, you can practise with my SQL Starter Kit, which uses the database you create here.
Contents
- Install MySQL
- Check it's running
- Secure the installation
- How root login works on Ubuntu
- Create a database and user for your app
- Allow remote connections (only if you need them)
- Start, stop and uninstall
- Troubleshooting
1. Install MySQL
Ubuntu's own repository includes MySQL, so you don't need to download anything from Oracle's site:
sudo apt update
sudo apt install mysql-server
That's it. You won't be asked for a password during installation; on Ubuntu, the root account uses your Linux login instead (see section 4).
Which version do I get? Whatever Ubuntu packages for your release, kept up to date with security fixes through sudo apt upgrade. That's the right choice for learning and most projects. Only use Oracle's MySQL APT repository if you specifically need a newer version than Ubuntu ships, and download its current config package from that page rather than copying an old version number from a tutorial.
2. Check it's running
sudo systemctl status mysql
Look for active (running) in green. Press q to exit. Then log in and check the version:
sudo mysql
SELECT VERSION();
EXIT;
3. Secure the installation
MySQL includes a script that removes insecure defaults:
sudo mysql_secure_installation
It asks a series of questions. Recommended answers for a development machine:
| Question | Answer | Why |
|---|---|---|
| Set up the VALIDATE PASSWORD component? | Y, then level 1 (MEDIUM) | Rejects weak passwords for new users |
| Remove anonymous users? | Y | Stops anyone logging in without a username |
| Disallow root login remotely? | Y | Root should only log in from this machine |
| Remove test database? | Y | It's accessible to everyone by default |
| Reload privilege tables now? | Y | Applies the changes immediately |
On Ubuntu, the script may say it's skipping the root password because root uses auth_socket. That's expected; see the next section.
4. How root login works on Ubuntu
Ubuntu sets up MySQL's root user to authenticate with your Linux account (the auth_socket plugin), not a password. That's why sudo mysql logs you straight in, and mysql -u root -p without sudo fails. This is more secure than a password, because only users who can already run sudo can log in as MySQL root.
Keep it this way. Older guides tell you to change root to mysql_native_password. Don't: that plugin is disabled by default in MySQL 8.4 and removed in MySQL 9. Instead, create a separate user for your apps and tools, as in the next step.
5. Create a database and user for your app
Your applications, phpMyAdmin or tools like DBeaver should never log in as root. Create a dedicated user with access to one database:
sudo mysql
CREATE DATABASE shop;
CREATE USER 'shopuser'@'localhost' IDENTIFIED BY 'Choose-A-Strong-Password-1';
GRANT ALL PRIVILEGES ON shop.* TO 'shopuser'@'localhost';
EXIT;
New users use MySQL's default caching_sha2_password authentication, which current drivers for Python, Node.js, PHP and Java all support. Test the new login:
mysql -u shopuser -p shop
SELECT DATABASE(), CURRENT_USER();
If you see shop and shopuser@localhost, you're done. Your app connects with host localhost, port 3306, database shop, and that username and password.
6. Allow remote connections (only if you need them)
By default, MySQL on Ubuntu only listens on 127.0.0.1, so only programs on the same machine can connect. That's what you want for local development. If another machine, such as an app server, must connect:
- Edit
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnfand changebind-address = 127.0.0.1to the server's private IP (or0.0.0.0for all interfaces). - Create a user for that specific client address, for example
'shopuser'@'10.0.0.5', rather than'%'(any host). - Open port 3306 only to that address:
sudo ufw allow from 10.0.0.5 to any port 3306. sudo systemctl restart mysql.
Never expose port 3306 to the whole internet; automated bots scan for it constantly.
7. Start, stop and uninstall
| Command | What it does |
|---|---|
sudo systemctl stop mysql | Stop MySQL |
sudo systemctl start mysql | Start it |
sudo systemctl restart mysql | Restart after config changes |
sudo systemctl disable mysql | Don't start at boot (saves RAM on a laptop) |
mysqldump -u shopuser -p shop > shop-backup.sql | Back up the shop database to a file |
sudo apt purge mysql-server mysql-common && sudo apt autoremove | Uninstall. Add sudo rm -rf /var/lib/mysql only if you also want to delete all data |
8. Troubleshooting
| Error | Fix |
|---|---|
ERROR 1698 (28000): Access denied for user 'root'@'localhost' | Normal on Ubuntu. Use sudo mysql, or log in as your own user (section 5). |
ERROR 1819: Your password does not satisfy the current policy requirements | The password validator is on. Use at least 8 characters with upper and lower case, a number and a symbol. |
Can't connect to local MySQL server through socket | MySQL isn't running: sudo systemctl start mysql, then check sudo journalctl -u mysql for the reason. |
| An app can't connect but the terminal can | Check the app uses localhost and port 3306, the right database name, and an up-to-date MySQL driver. |
| MySQL won't start after editing the config | Undo the last change; a typo in mysqld.cnf stops the server. The error is in sudo journalctl -u mysql. |
Next: load sample tables into your new shop database and learn the essential queries in my SQL Starter Kit. Prefer containers? See WordPress and MySQL with Docker.
