- 1 1. Purpose and Benefits of Installing MySQL on Ubuntu
- 2 2. Preparation: Setting Up the Ubuntu Environment
- 3 3. MySQL Installation Steps
- 4 4. Initial MySQL Configuration and Security Enhancement
- 5 5. Connecting to MySQL and User Management
- 6 6. Basic Database and Table Operations
- 7 7. Regular Maintenance and Backups
- 8 8. Troubleshooting and Support Resources
- 9 9. Conclusion
- 10 10. Related Websites
1. Purpose and Benefits of Installing MySQL on Ubuntu
Installing MySQL in an Ubuntu environment allows for efficient management of data for business and web applications. MySQL is lightweight, fast, and compatible with various systems, making it widely used by engineers and businesses. This article provides a comprehensive guide on MySQL installation, basic operations, maintenance, and troubleshooting.
2. Preparation: Setting Up the Ubuntu Environment
Before installing MySQL, update your Ubuntu system packages to ensure a smooth installation process and minimize the risk of errors.
2-1. Updating and Upgrading Packages
Run the following commands to update your system packages to the latest version:
sudo apt update
sudo apt upgrade
These commands update existing packages, improving system compatibility.
2-2. Checking for Dependencies
MySQL requires multiple system dependency packages. Checking for dependencies beforehand helps avoid issues during installation. Use the following command to check for missing packages:
sudo apt install -f

3. MySQL Installation Steps
Install MySQL on Ubuntu using the mysql-server
package. After installation, verify that the MySQL service is running properly.
3-1. Installing MySQL
Run the following command to download and install MySQL:
sudo apt install mysql-server
3-2. Verifying MySQL Status and Enabling Auto-Start
After installation, check whether the MySQL service is running:
sudo systemctl status mysql
If the status shows “active (running),” MySQL has been installed successfully. To ensure MySQL starts automatically on system boot, run the following command:
sudo systemctl enable mysql
4. Initial MySQL Configuration and Security Enhancement
After installing MySQL, use the mysql_secure_installation
script to enhance security settings.
4-1. Running mysql_secure_installation
Execute the following command to configure security settings:
sudo mysql_secure_installation
During the script execution, you will be prompted to configure the following:
- Password Policy: Set password strength (Low, Medium, High) to improve security.
- Removing Anonymous Users: Delete default anonymous users.
- Deleting Test Databases: Remove test databases to reduce security risks.
- Restricting Remote Access: Limit root user login from remote connections.
4-2. Recommended Security Settings
For each prompt, entering “Y” (Yes) is generally recommended to apply secure settings. It is advisable to set the password policy to “Medium” or “High” and restrict remote access for better security.
5. Connecting to MySQL and User Management
After installing MySQL, connect to the database and configure initial settings and user management.
5-1. Connecting to the MySQL Shell
To operate MySQL as the root user, use the following command to access the MySQL shell:
sudo mysql
5-2. Changing Authentication Method (Optional)
From MySQL 8.0 onwards, the default authentication method for the root user is set to auth_socket
. If you need to connect using external tools (e.g., phpMyAdmin), you can change it to mysql_native_password
using the following commands:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'StrongPassword';
FLUSH PRIVILEGES;

6. Basic Database and Table Operations
Understanding basic MySQL operations allows for efficient database management. This section explains how to create databases and tables.
6-1. Creating Databases and Tables
Use the following command to create a new database:
CREATE DATABASE database_name;
USE database_name;
Next, create a table. For example, to create a table for managing user information, use the following command:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
6-2. Inserting and Retrieving Data
To insert data into a table and retrieve it, use the following commands:
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
SELECT * FROM users;
7. Regular Maintenance and Backups
Proper maintenance is crucial for managing MySQL efficiently. This section covers how to back up data and optimize performance.
7-1. Taking Backups
Regular backups are essential for data safety. Use the mysqldump
command to create a backup:
mysqldump -u root -p database_name > backup.sql
7-2. Optimizing Performance
To maintain MySQL performance, periodically optimize tables using the following command:
OPTIMIZE TABLE table_name;
7-3. FAQ
- What if I encounter non-recommended options in the
mysql_secure_installation
script?
If you choose not to restrict remote access for the root user, consider additional security measures to protect your database.
8. Troubleshooting and Support Resources
If you encounter issues while using MySQL, follow these troubleshooting steps.
8-1. Checking Service Status and Restarting
To verify whether the MySQL service is running correctly, use the following commands. Restart the service if necessary:
sudo systemctl status mysql
sudo systemctl restart mysql
8-2. Checking Error Logs
To identify the cause of an issue, check MySQL’s error logs:
sudo cat /var/log/mysql/error.log
8-3. Support Resources
Refer to official documentation and community forums for further support. MySQL’s official website and Q&A sites like Stack Overflow provide useful troubleshooting information.
9. Conclusion
This guide has covered the steps to install, configure, and maintain MySQL on Ubuntu. By following the installation and security procedures, you can manage databases efficiently and securely. As a next step, consider optimizing query performance and automating backups for a more robust system.