How to Install MySQL on Ubuntu 24.04
MySQL is one of the most popular open-source relational database management systems. It’s fast, reliable, and widely used for managing databases in various applications. If you’re running Ubuntu 24.04 and want to install MySQL, this guide will take you through the steps.
Prerequisites
Before you begin, ensure you have:
- A system running Ubuntu 24.04
- A user with sudo privileges
- Internet connection
Step 1: Update Your System
It’s always a good idea to start by updating your system to ensure you have the latest software packages. Open your terminal and run:
sudo apt update
sudo apt upgrade
Step 2: Install MySQL
Ubuntu’s package manager makes installing MySQL simple. To install the MySQL server, type the following command:
sudo apt install mysql-server
This command will install MySQL and its dependencies. When prompted, confirm the installation by typing Y and hitting Enter.
Step 3: Start MySQL and Enable Automatic Start on Boot
Once the installation is complete, you need to ensure that MySQL is running and set to start automatically on boot. You can start MySQL with the following command:
sudo systemctl start mysql
Next, enable it to start automatically at boot:
sudo systemctl enable mysql
Step 4: Secure MySQL Installation
MySQL comes with a security script that allows you to enhance the default security settings. Run this script using the following command:
sudo mysql_secure_installation
You’ll be prompted to set up various security features:
- Set up the root password: You can choose to set a strong password for the MySQL root user.
- Remove anonymous users: It’s a good idea to remove anonymous users for better security.
- Disallow remote root login: This ensures that the root account can only be used to connect locally.
- Remove the test database: The test database is usually unnecessary in a production environment.
- Reload privilege tables: This ensures all the changes take effect immediately.
Follow the prompts to secure your MySQL installation according to your preferences.
Step 5: Verify MySQL Installation
After installation, you can check if MySQL is running properly with the following command:
sudo systemctl status mysql
You should see an active status indicating that MySQL is running.
Additionally, log in to the MySQL shell to verify that everything is working:
sudo mysql
You’ll enter the MySQL command prompt. From here, you can check the MySQL version by typing:
SELECT VERSION();
To exit the MySQL prompt, simply type:
exit;
Step 6: Configure MySQL (Optional)
If you want to make changes to the MySQL configuration, the primary configuration file is located at:
/etc/mysql/mysql.conf.d/mysqld.cnf
You can edit this file using a text editor like Nano:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
After making changes, restart MySQL to apply them:
sudo systemctl restart mysql
Step 7: Allow Remote Access to MySQL (Optional)
If you need to access MySQL remotely, you’ll have to modify the bind address in the MySQL configuration file. Open the configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Look for the line that reads:
bind-address = 127.0.0.1
Change this to:
bind-address = 0.0.0.0
Save the changes and restart MySQL:
sudo systemctl restart mysql
You may also need to grant access to the MySQL user for the remote IP address:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'remote_ip' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Conclusion
You now have MySQL successfully installed on your Ubuntu 24.04 system. You can manage databases, users, and connections as needed for your applications. If you need further configuration or optimization, MySQL has a rich set of tools and configurations to explore!
