Skip to content
Log In Sign Up info@muvcloud.com

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 test database and the rights that let any user open databases named test_ 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

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations