Estimated reading time: 2 minutes
Installing MySQL on Ubuntu is relatively easy. I’ve done it multiple times and will walk you through each step, including solutions to common issues and FAQs.
Table of contents
Prerequisites for Installing MySQL
Before we dive into the installation, ensure your system meets the following requirements:
- Ubuntu 20.04 or later
- A user account with sudo privileges to do the installation
- An internet connection for downloading the packages from the repository
Installing MySQL
Step 1: Update Your System
First, update your package index. This ensures all your repositories are up-to-date.
sudo apt update
Step 2: Install MySQL Server
Next, install the MySQL server package. This command installs MySQL and its dependencies.
sudo apt install mysql-server
Step 3: Secure MySQL Installation
After installation, it’s critical to secure your MySQL server. Run the security script to enhance security and set up a root password.
sudo mysql_secure_installation
Step 4: Verify MySQL Installation
Finally, verify that MySQL is installed correctly and running.
sudo systemctl status mysql
You should see a status message indicating that MySQL is active and running.
Common Issues and How to Fix Them
Issue 1: MySQL Service Not Starting
If MySQL isn’t starting, check the service status and logs.
sudo systemctl status mysql
sudo journalctl -xe
Look for any error messages in the logs. A common fix involves correcting the configuration file:
sudo nano /etc/mysql/my.cnf
Ensure the settings are correct and restart the service.
sudo systemctl restart mysql
Issue 2: Access Denied for User ‘root’@’localhost’
You might need to reset the root password if you encounter an “Access denied” error. Start by stopping the MySQL service:
sudo systemctl stop mysql
Then, restart MySQL without loading the grant tables:
sudo mysqld_safe --skip-grant-tables &
Log in to MySQL as root:
mysql -u root
Update the root user’s password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
Finally, restart MySQL normally:
sudo systemctl start mysql
Frequently Asked Questions (FAQs)
How do I check my MySQL version?
To check your MySQL version, use:
mysql --version
Can I install MySQL Workbench on Ubuntu?
Yes, you can install MySQL Workbench with the following command:
sudo apt install mysql-workbench
How do I uninstall MySQL?
If you need to remove MySQL, run:
sudo apt remove --purge mysql-server mysql-client mysql-common
sudo apt autoremove
sudo apt autoclean