How to Install MySQL on Ubuntu 22.04 LTS
How to Install MySQL on Ubuntu 22.04 LTS
Ubuntu 22.04 LTS (Jammy Jellyfish) continues the tradition of providing a stable and reliable platform for server deployments, making it an excellent choice for hosting MySQL databases. This tutorial will walk you through installing MySQL Community Server on an Ubuntu 22.04 LTS system. We will cover updating your package sources, installing the MySQL server, running the essential security script, and managing the MySQL service. For comprehensive server administration and optimization, including Linux Server Support, consider exploring professional services.
MySQL is a leading open-source relational database management system (RDBMS), integral to countless web applications and data-driven services. By following this guide, you will establish a secure and functional MySQL installation on your server.
Prerequisites
- An Ubuntu 22.04 LTS server.
- A non-root user with
sudoprivileges. - Access to a terminal or command-line interface.
Before starting, ensure your system is fully updated and you have a sudo-enabled user. Standard Ubuntu initial server setup guides can assist with these preliminary steps.
Step 1 — Updating Package Index
It is always a best practice to update your server’s package list before installing new software. This ensures that you are installing the latest available versions and that all dependencies are correctly resolved.
sudo apt update
Step 2 — Installing MySQL
MySQL packages are available in Ubuntu 22.04’s default APT repositories. You can install the mysql-server package, which typically provides mysql-community-server and its necessary dependencies.
sudo apt install mysql-server
During the installation on Ubuntu 22.04, MySQL 8.0 (or a newer stable version available in the repositories) is typically configured to use the auth_socket plugin for the root user by default. This plugin allows authentication by verifying the operating system user connecting via a Unix socket, meaning you can connect as root using sudo mysql without a separate MySQL root password initially.
Once the installation completes, the MySQL service should start automatically. You can verify its operational status:
sudo systemctl status mysql
The output should confirm that the service is active (running).
Step 3 — Securing MySQL
MySQL provides a security script, mysql_secure_installation, which should be executed post-installation to enhance the security of your MySQL server. This script helps you configure options such as the VALIDATE PASSWORD component (to enforce password strength), set or change the root password (especially if you intend to switch from auth_socket to password-based authentication for root), remove anonymous users, restrict remote root login, and remove the test database.
Run the script using sudo:
sudo mysql_secure_installation
The script will guide you through several prompts:
- VALIDATE PASSWORD COMPONENT: The script will first ask if you want to enable the VALIDATE PASSWORD component. Enabling this is recommended for production environments as it enforces password complexity. If you choose to enable it, you will select a password validation level (0 for LOW, 1 for MEDIUM, 2 for STRONG).
- Set Root Password: You will be prompted to set a password for the MySQL
rootuser.- Note on
auth_socket: If you plan to continue usingauth_socketforroot(allowingsudo mysqlaccess without a password), this step might seem redundant for localsudoaccess. However, setting a strong password here is still good practice, especially if you later decide to enable password authentication forroot. To switchrootto use password authentication, you would execute:ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'your_new_password'; FLUSH PRIVILEGES;inside the MySQL monitor.
- Note on
- Remove anonymous users? (Recommended: Yes) This prevents unauthenticated access.
- Disallow root login remotely? (Recommended: Yes) This is a critical security measure, restricting
rootaccess to the local machine only. - Remove test database and access to it? (Recommended: Yes) The test database is for development and testing, not production.
- Reload privilege tables now? (Recommended: Yes) This applies all the changes made immediately.
Completing these configurations is vital for a secure MySQL deployment. For advanced security measures or ongoing database administration, consider engaging with professional MySQL support services.
Step 4 — Testing MySQL
After running mysql_secure_installation, test your MySQL access. If auth_socket is still active for root (the default), connect using:
sudo mysql
If you reconfigured root to use password authentication (e.g., with mysql_native_password), you would connect using:
mysql -u root -p
Then, enter the root password you set.
Once connected, you will be at the MySQL monitor prompt (mysql>). You can run a test query, such as checking the server version or listing databases:
SELECT VERSION();
SHOW DATABASES;
To exit the MySQL monitor, type:
EXIT;
Step 5 — Managing the MySQL Service
The MySQL service on Ubuntu 22.04 is managed with systemctl.
- Start MySQL:
bash
sudo systemctl start mysql - Stop MySQL:
bash
sudo systemctl stop mysql - Restart MySQL:
bash
sudo systemctl restart mysql - Check MySQL Status:
bash
sudo systemctl status mysql - Enable MySQL on Boot: (This is typically done automatically during installation)
bash
sudo systemctl enable mysql - Disable MySQL on Boot:
bash
sudo systemctl disable mysql
Conclusion
You have successfully installed and secured MySQL on your Ubuntu 22.04 LTS server. Your database system is now ready for your applications. Remember the importance of regular backups and keeping your system and MySQL packages updated.
For further information and related guides, explore these resources:
- How to Install MySQL on Ubuntu 24.04 LTS
- How to Install MySQL on Ubuntu 20.04 LTS
- How to Install MySQL on Debian 12
- Return to the main How to Install MySQL series overview.
Robust database administration is essential for application reliability and data security. If your organization needs expert assistance with database management or is considering comprehensive Managed IT Services, Medha Cloud offers a variety of solutions. Additionally, for Managed Service Providers (MSPs), Medha Cloud provides white-label managed IT services, enabling you to enhance your service offerings under your own brand.