MySQL is a widely used open-source relational database management system. Its versatility and robust features make it a preferred choice for developers and administrators. This guide provides detailed steps to install MySQL Server on Ubuntu 24.04 LTS, ensuring a fully operational database environment.
TL;DR
To install MySQL Server on Ubuntu 24.04 LTS:
- Update your package index using
apt update
. - Install the MySQL server package with
apt install mysql-server
. - Start and enable the MySQL service.
- Secure the installation using
mysql_secure_installation
.
Prerequisites
Before you begin, ensure your system meets the following requirements:
- Operating System: Ubuntu 24.04 LTS.
- User Privileges: Administrative privileges or
sudo
access. - Internet Access: To download packages and dependencies.
Step 1: Update the Package Index
First, update the package index to ensure you install the latest version of MySQL from the Ubuntu repository.
Command:
sudo apt update
This command synchronizes your local package index with the repository, ensuring access to the latest software versions.
Step 2: Install MySQL Server
Use the apt
package manager to install MySQL Server.
Command:
sudo apt install mysql-server
When prompted, confirm the installation by typing Y
and pressing Enter
. This command installs MySQL Server along with its dependencies.
Step 3: Start and Enable MySQL Service
After installation, start the MySQL service and enable it to start on system boot.
Commands:
sudo systemctl start mysql
sudo systemctl enable mysql
You can verify the status of the MySQL service using:
sudo systemctl status mysql
Expected Output:
Step 4: Secure the MySQL Installation
Use the mysql_secure_installation
script to configure the security settings for your MySQL installation.
Command:
sudo mysql_secure_installation
Prompts You’ll Encounter:
- Validate Password Component: Decide whether to enable the password validation plugin.
- Set Root Password: Enter and confirm a strong root password.
- Remove Anonymous Users: Restrict access to authenticated users.
- Disallow Root Login Remotely: Limit root user access to localhost.
- Remove Test Database: Delete the default test database.
- Reload Privilege Tables: Apply the new changes immediately.
This script enhances the security of your MySQL server by enforcing best practices.
Step 5: Verify the MySQL Installation
Log in to the MySQL server to ensure it is installed and running correctly.
Command:
sudo mysql -u root -p
Enter the root password set during the secure installation process. Once logged in, verify the server status:
Command:
SELECT VERSION();
Expected Output:
+----------------+
| VERSION() |
+----------------+
| 8.0.x |
+----------------+
Optional: Configure MySQL for Remote Access
If your use case requires remote access to MySQL, modify the server configuration and allow traffic through the firewall.
Steps:
1. Edit the MySQL Configuration File: Open the MySQL configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Find the line bind-address
and set it to 0.0.0.0
to allow connections from any IP address:
bind-address = 0.0.0.0
Save and exit the file.
2. Restart MySQL Service:
sudo systemctl restart mysql
3. Open Firewall Port: Allow MySQL traffic through the firewall:
sudo ufw allow 3306
sudo ufw reload
4. Grant Remote Access Privileges: Log in to MySQL and grant remote access to the desired user:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Common Issues and Solutions
1: MySQL Service Fails to Start
- Cause: Misconfigured settings or insufficient resources.
- Solution: Check the logs for errors:
sudo journalctl -u mysql
2: Password Authentication Fails
- Cause: Incorrect root password.
- Solution: Reset the root password:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
3: Remote Access Not Working
- Cause: Firewall or incorrect bind address.
- Solution: Ensure the firewall allows traffic on port
3306
and the bind address is set to0.0.0.0
.