Step 1: Install the Package
sudo apt update
sudo apt install mysql-server
This installs the MySQL server, the mysql client, and their dependencies from Ubuntu's own repositories. To see which version your release offers before you install, run apt policy mysql-server. The steps are the same on Ubuntu 24.04, which ships MySQL 8.0, and on 26.04, which ships MySQL 8.4 LTS.
You need a user with sudo rights; see how to create a sudo user on Ubuntu.
Step 2: Check the Service
The service starts by itself and is enabled at boot:
sudo systemctl status mysql
mysql --version
If it is not active, read the error log, /var/log/mysql/error.log, with tail, and the service messages with sudo journalctl -u mysql. A frequent cause on small servers is too little memory.
Step 3: Run the Security Script
sudo mysql_secure_installation
The script asks a series of questions. Answer yes to each removal:
- Password validation: offers a component that rejects weak passwords for new accounts. Optional; if you enable it, pick a level that your password generator meets.
- Root password: on Ubuntu, the database root account logs in through the system's root user, and Ubuntu's script says it skips setting a password for it. Leave it that way.
- Anonymous users: removes accounts without a name.
- Remote root login: limits root to connections from the server itself.
- Test database: removes the
testdatabase and the rights that let any user open databases namedtest_something. - Reload privileges: applies the changes at once.
Step 4: How root Logs In
sudo mysql
No password is asked. The root account uses the auth_socket plugin: MySQL checks that the Linux user running the client is root. This is safer than a password, because only users with sudo rights get in. Keep it that way and use root for administration only, never in an application.
Step 5: Create a Database and an Application User
Inside sudo mysql:
CREATE DATABASE app CHARACTER SET utf8mb4;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON app.* TO 'app_user'@'localhost';
EXIT;
Replace STRONG_PASSWORD with a long random password and store it in the application's configuration. The account can work in the app database only and can connect only from the server itself. Test it, letting the client ask for the password:
mysql -u app_user -p app
Never write the password after -p on the command line. To review accounts later, see how to list users in MySQL.
Step 6: Keep MySQL Local
Ubuntu's packaged configuration, /etc/mysql/mysql.conf.d/mysqld.cnf, sets bind-address and mysqlx-bind-address to 127.0.0.1. MySQL then accepts connections from the server itself only, through the loopback address explained in what is localhost, and through its local socket. Check it:
sudo ss -tlnp | grep mysqld
The output should show 127.0.0.1 for port 3306, and for 33060, the port of the X Protocol. An application on the same server needs nothing more.
Step 7: Firewall and Remote Access
Leave port 3306 closed in ufw. A database open to the internet receives password guessing all day, and a single weak account exposes all its data. To work on the database from your own computer, use an SSH tunnel instead:
ssh -L 3307:127.0.0.1:3306 user@your_server_ip
While the session is open, a database tool on your computer connects to 127.0.0.1 port 3307 and reaches MySQL through SSH. If another server must connect, use a private network between the two, an account for that host only, and a firewall rule for its address only.
Next Steps
- Back up: schedule
mysqldumpfor each database and copy the dumps off the server. - Update: MySQL updates with the system; see sudo apt update.
- Learn the basics: how to list databases in MySQL and how to check the MySQL version.