DEV Community

Cover image for Installing MySQL on Ubuntu 26.04
Sanskriti Harmukh for Vultr

Posted on • Originally published at docs.vultr.com

Installing MySQL on Ubuntu 26.04

Ubuntu 26.04 includes MySQL 8.4 in its default APT repository with no third-party sources required. This guide covers installation, service configuration, security hardening with mysql_secure_installation, and creating a database with a scoped user ready for application use.


Install MySQL

MySQL 8.4 is available directly from Ubuntu 26.04's default APT repository.

1. Update the APT package index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install the MySQL server package:

$ sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

3. Verify the installed version:

$ mysql --version
Enter fullscreen mode Exit fullscreen mode

Manage the MySQL Service

Enable MySQL as a systemd service so it starts automatically on every boot.

1. Enable and start the service:

$ sudo systemctl enable mysql
$ sudo systemctl start mysql
Enter fullscreen mode Exit fullscreen mode

2. Check the service status:

$ sudo systemctl status mysql
Enter fullscreen mode Exit fullscreen mode

3. Stop or restart the service when needed:

$ sudo systemctl stop mysql
$ sudo systemctl restart mysql
Enter fullscreen mode Exit fullscreen mode

Secure MySQL

The mysql_secure_installation script removes default configuration risks including anonymous users, remote root login, and the test database.

1. Run the security script:

$ sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

2. Open the MySQL configuration file:

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Enter fullscreen mode Exit fullscreen mode

Add the following line under [mysqld]:

mysql_native_password=ON
Enter fullscreen mode Exit fullscreen mode

3. Restart MySQL to apply the change:

$ sudo systemctl restart mysql
Enter fullscreen mode Exit fullscreen mode

4. Set the root password in the MySQL shell:

$ sudo mysql
Enter fullscreen mode Exit fullscreen mode
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
Enter fullscreen mode Exit fullscreen mode

Create a Database and User

Log in as root and create a dedicated database with a user scoped to that database only.

$ mysql -u root -p
Enter fullscreen mode Exit fullscreen mode
mysql> CREATE DATABASE myapp_db;
mysql> CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'user_password';
mysql> GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
Enter fullscreen mode Exit fullscreen mode

Create a Sample Table

Log in as the new user to verify database access and permissions.

$ mysql -u myapp_user -p myapp_db
Enter fullscreen mode Exit fullscreen mode
mysql> CREATE TABLE services (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    status VARCHAR(50)
);
mysql> INSERT INTO services (name, status) VALUES
    ('web', 'active'),
    ('db', 'active'),
    ('cache', 'idle');
mysql> SELECT * FROM services;
mysql> EXIT;
Enter fullscreen mode Exit fullscreen mode

All three rows in the query output confirm the database, user, and table are working correctly.


Next Steps

MySQL is now installed and accepting connections. From here you can:

  • Integrate MySQL with an Apache and PHP stack to serve dynamic web applications
  • Configure replication for read scaling and high availability
  • Automate backups with mysqldump or Percona XtraBackup

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)