This guide walks you through installing MySQL on CentOS using the official MySQL YUM repository, which works with both the classic yum and the newer dnf package managers. You’ll add the repository, install the MySQL server, start the service, secure the installation, and verify that your database is running. By the end, you’ll have a working MySQL server ready for your applications.
What You’ll Need
- A CentOS system (7, 8, or 9) with sudo or root access.
- An internet connection to download the repository package.
- Basic familiarity with the command line.
Step-by-Step
- Update your system packages
Runsudo yum update -y(orsudo dnf update -yon CentOS 8+). This ensures your package index is current and reduces the chance of dependency conflicts. - Download the MySQL YUM repository package
Visit the official MySQL Yum Repository page to get the latest RPM URL. As of this writing, the current version is mysql80-community-release-el7-7.noarch.rpm for CentOS 7 and mysql80-community-release-el9-1.noarch.rpm for CentOS 9. Usewgetorcurlto download it. For example, on CentOS 7:
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm - Install the repository package
Runsudo yum localinstall mysql80-community-release-el7-7.noarch.rpm(orsudo dnf localinstall ...). This adds the MySQL repository to your system. - Verify the repository is enabled
Runyum repolist enabled | grep mysql(ordnf repolist). You should see entries likemysql80-community. If you don’t, check the repository file at/etc/yum.repos.d/mysql-community.repo. - Install MySQL server
Runsudo yum install mysql-community-server -y(orsudo dnf install mysql-community-server -y). This installs the MySQL server, client, and common libraries. - Start the MySQL service
Runsudo systemctl start mysqld. To ensure it starts automatically at boot, runsudo systemctl enable mysqld. - Find the temporary root password
During the first start, MySQL generates a temporary password. Find it with:
sudo grep 'temporary password' /var/log/mysqld.log - Secure the installation
Runsudo mysql_secure_installation. This interactive script will ask you to enter the temporary password, then prompt you to set a new root password, remove anonymous users, disallow remote root login, remove the test database, and reload privileges. AnswerYto all security prompts. - Test the connection
Log in as root with your new password:
mysql -u root -p
You should see the MySQL prompt. RunSELECT VERSION();to confirm the server is working. - Optional: Create a dedicated database user
Instead of using root for applications, create a user with limited privileges. At the MySQL prompt, run:
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPass123!';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
Common Mistakes
- Using the wrong repository package – CentOS 7 and CentOS 8/9 use different RPMs. Downloading the wrong one will lead to missing dependencies or installation failures.
- Forgetting to start the service – The server won’t run until you run
systemctl start mysqld. - Skipping the temporary password step – You cannot log in without it; it’s only generated on the first start.
- Setting a weak root password – MySQL 8 enforces a password policy by default. If you try to set a simple password, the secure installation script will reject it.
- Not enabling the service – If you don’t run
systemctl enable mysqld, MySQL won’t start after a reboot.
Tips
- Use a strong password – Include uppercase, lowercase, digits, and special characters. MySQL 8’s default policy requires this.
- Configure firewall – If you need remote access, open port 3306:
sudo firewall-cmd --permanent --add-port=3306/tcpand reload withsudo firewall-cmd --reload. Only do this if necessary. - Back up your data – Regularly use
mysqldumpto back up your databases. - Check the MySQL error log – If something goes wrong, look at
/var/log/mysqld.logfor details. - Use a dedicated user for applications – Avoid using root for day-to-day operations.
Troubleshooting
- Cannot find the temporary password – If the log file is empty or the password line is missing, the server may have already been initialized. Try stopping the service, removing the data directory (
sudo rm -rf /var/lib/mysql), and starting it again. This will regenerate a new temporary password. - Installation fails with dependency errors – This often happens when the repository isn’t properly set up. Run
sudo yum clean alland try again. If using CentOS 8/9, ensure themysql80-communityrepo is enabled and themysql-tools-communityrepo is disabled if it conflicts. - Service fails to start – Check the error log at
/var/log/mysqld.log. Common causes include incorrect permissions on the data directory or a port conflict. Ensure no other MySQL instance is running. - Can’t connect via socket – If you get “Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock'”, the server may not be running. Check with
systemctl status mysqld. - Password policy rejects your new password – The default policy requires a minimum length of 8 characters and at least one uppercase, one lowercase, one digit, and one special character. If you need a less strict policy, you can change it after login with
SET GLOBAL validate_password.policy = LOW;but this is not recommended for production.
Frequently Asked Questions
What is the difference between yum and dnf?
Yum is the traditional package manager on CentOS 7, while DNF is the default on CentOS 8 and 9. Both work with the same repositories; you can use either command on systems that have it. The steps are identical.
How do I change the root password after installation?
Log in as root and run ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass123!'; Then run FLUSH PRIVILEGES;.
Can I install MySQL alongside MariaDB?
It’s not recommended. They both use the same port and data directories. If you have MariaDB installed, remove it first: sudo yum remove mariadb-server.
How do I enable remote access to MySQL?
Edit the MySQL configuration file (/etc/my.cnf) and comment out the bind-address line or set it to 0.0.0.0. Then restart MySQL and open port 3306 in the firewall. Also ensure your user is allowed to connect from remote hosts (e.g., 'user'@'%').
What is the default data directory?
The default is /var/lib/mysql. You can change it in /etc/my.cnf under the [mysqld] section with the datadir option.
How do I uninstall MySQL completely?
Stop the service, then run sudo yum remove mysql-community-server (or dnf remove). Remove the data directory and configuration files manually if you want a clean slate.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- Soundbar vs 2.1 Speaker System: Which Is Better?
- DisplayPort vs HDMI: Which Cable Should You Use?
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors
- Mouse Moving on Its Own? Causes & Fixes