Database management on Linux starts with configuring MySQL’s repository for your specific distribution. If you’ve been wondering how to install mysql on linux, this guide walks you through the entire process step by step. Whether you’re using Ubuntu, Debian, CentOS, or Fedora, the steps are clear and practical. You’ll have MySQL running in under 15 minutes.
Prerequisites For Mysql Installation On Linux
Before you begin, make sure your system is up to date. You need sudo or root access to install packages. A stable internet connection is also required to download the repository files.
- A Linux distribution (Ubuntu 20.04+, Debian 11+, CentOS 7+, or Fedora 35+)
- Terminal access with sudo privileges
- Basic familiarity with command-line commands
- At least 1 GB of free disk space
Check your current system version with lsb_release -a or cat /etc/os-release. This helps you pick the right repository.
How To Install Mysql On Linux
This section covers the complete installation process for major Linux distributions. Follow the steps that match your system.
Step 1: Update Your Package Manager
Always start with a clean slate. Run the update command for your distribution.
For Debian/Ubuntu:
sudo apt update && sudo apt upgrade -y
For RHEL/CentOS/Fedora:
sudo dnf update -y
This ensures no package conflicts occur during installation.
Step 2: Add The Official Mysql Repository
Using the official repository gives you the latest stable version. Avoid using distribution default packages as they may be outdated.
For Ubuntu/Debian:
- Download the MySQL APT repository configuration package:
- Install the package:
- Select your MySQL version when prompted (default is 8.0).
- Update the package list again:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb
sudo apt update
For RHEL/CentOS/Fedora:
- Download the MySQL Yum repository RPM:
- For CentOS 7, use the el7 version instead.
- Import the GPG key if prompted:
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el9-5.noarch.rpm
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql
Step 3: Install The Mysql Server
Now you’re ready to install the server package. The command is simple.
For Debian/Ubuntu:
sudo apt install mysql-server -y
For RHEL/CentOS/Fedora:
sudo dnf install mysql-server -y
During installation on Debian-based systems, you’ll be asked to set a root password. Choose a strong one and remember it. On RHEL-based systems, the root password is set later.
Step 4: Start And Enable The Mysql Service
After installation, start the MySQL service and enable it to run on boot.
sudo systemctl start mysql
sudo systemctl enable mysql
Check the status to confirm it’s running:
sudo systemctl status mysql
You should see “active (running)” in green.
Step 5: Secure The Installation
MySQL includes a security script to remove default settings. Run it immediately.
sudo mysql_secure_installation
This script will:
- Set or change the root password
- Remove anonymous users
- Disallow remote root login
- Remove test databases
- Reload privilege tables
Answer “Y” to all prompts for maximum security.
Step 6: Verify The Installation
Log into MySQL to confirm everything works.
sudo mysql -u root -p
Enter your root password. You should see the MySQL monitor prompt:
mysql>
Run a simple query to test:
SHOW DATABASES;
You’ll see the default databases: information_schema, mysql, performance_schema, and sys.
Installing Mysql On Ubuntu 22.04
Ubuntu 22.04 LTS is a popular choice. The process is identical to the general Debian method above, but here are specific notes.
- Use the mysql-apt-config_0.8.29-1_all.deb package
- Select Ubuntu 22.04 LTS when prompted
- If you get a “mysql-community-server” dependency error, run:
sudo apt --fix-broken install
After installation, check the MySQL version:
mysql --version
You should see something like “mysql Ver 8.0.35 for Linux on x86_64”.
Installing Mysql On Centos 7
CentOS 7 uses Yum instead of DNF. The steps are slightly different.
- Download the repository RPM for el7:
- Install MySQL server:
- Start the service:
- Find the temporary root password:
- Use that password to log in and change it immediately.
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
sudo yum install mysql-server -y
sudo systemctl start mysqld
sudo grep 'temporary password' /var/log/mysqld.log
Note: CentOS 7 reached end of life in 2024. Consider migrating to Rocky Linux or AlmaLinux for continued support.
Installing Mysql On Fedora 38
Fedora users can use the same repository as CentOS but with DNF.
sudo dnf install https://dev.mysql.com/get/mysql80-community-release-fc38-1.noarch.rpm
sudo dnf install mysql-server -y
Fedora’s default firewall may block MySQL. Open port 3306 if you need remote access:
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
Post-Installation Configuration
Once MySQL is installed, you may want to tweak some settings.
Create A New Database And User
For security, avoid using root for everyday tasks. Create a dedicated user.
CREATE DATABASE myapp;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
Configure Mysql To Start On Boot
This is already done if you used systemctl enable. Verify with:
sudo systemctl is-enabled mysql
It should return “enabled”.
Enable Remote Access (Optional)
Edit the MySQL configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Find the line bind-address = 127.0.0.1 and change it to 0.0.0.0. Restart MySQL:
sudo systemctl restart mysql
Warning: This exposes your database to the network. Use a firewall and strong passwords.
Troubleshooting Common Installation Issues
Even with clear steps, things can go wrong. Here are fixes for common problems.
Gpg Key Errors
If you see “NO_PUBKEY” errors, import the key manually:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
For RHEL-based systems, update the GPG key:
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql
Dependency Issues
Sometimes packages conflict. Use the following to resolve:
sudo apt --fix-broken install
sudo dnf distro-sync
Mysql Service Fails To Start
Check the error log:
sudo tail -100 /var/log/mysql/error.log
Common causes include port conflicts or insufficient disk space. Free up space or change the port in the config file.
Can’t Connect To Local Mysql Server
This often happens after a password change. Reset the root password:
sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
exit
sudo systemctl restart mysql
Uninstalling Mysql From Linux
If you need to remove MySQL completely, follow these steps.
For Debian/Ubuntu:
sudo apt remove --purge mysql-server mysql-client mysql-common -y
sudo apt autoremove -y
sudo rm -rf /var/lib/mysql
For RHEL/CentOS/Fedora:
sudo dnf remove mysql-server -y
sudo rm -rf /var/lib/mysql /etc/my.cnf
Back up your databases before removal if needed.
Frequently Asked Questions
Q: What is the easiest way to install MySQL on Linux?
A: Using the official MySQL repository with your package manager is the easiest and most reliable method. Just add the repo and run the install command.
Q: How do I check if MySQL is installed on Linux?
A: Run mysql --version or systemctl status mysql. If the service is active, MySQL is installed.
Q: Can I install MySQL without internet on Linux?
A: Yes, download the .deb or .rpm package on another machine, transfer it via USB, and install with dpkg -i or rpm -ivh. Dependencies may still require internet.
Q: Why does MySQL ask for a password during installation on Ubuntu?
A: The installer prompts you to set a root password for security. If you skip it, you can set it later with mysql_secure_installation.
Q: How to install MySQL on Linux without root access?
A: You can compile from source in your home directory, but it’s complex. Consider using a containerized version like Docker instead.
Final Thoughts On Installing Mysql On Linux
Learning how to install mysql on linux is a fundamental skill for any developer or sysadmin. The process is straightforward once you understand the repository system. Always use the official MySQL repository for security and stability. After installation, run the security script and create a non-root user for daily use. With MySQL running, you can start building databases for your applications. If you encounter issues, check the logs and consult the MySQL documentation. Practice on a virtual machine first if you’re unsure. Now you have a fully functional database server on your Linux system.