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
2. Install the MySQL server package:
$ sudo apt install mysql-server -y
3. Verify the installed version:
$ mysql --version
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
2. Check the service status:
$ sudo systemctl status mysql
3. Stop or restart the service when needed:
$ sudo systemctl stop mysql
$ sudo systemctl restart mysql
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
2. Open the MySQL configuration file:
$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add the following line under [mysqld]:
mysql_native_password=ON
3. Restart MySQL to apply the change:
$ sudo systemctl restart mysql
4. Set the root password in the MySQL shell:
$ sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
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
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;
Create a Sample Table
Log in as the new user to verify database access and permissions.
$ mysql -u myapp_user -p myapp_db
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;
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
mysqldumpor Percona XtraBackup
For the full guide with additional tips, visit the original article on Vultr Docs.
Top comments (0)