keepcalculator

06 Numbers and Powers

Install MySQL on CentOS: YUM, DNF & Setup

Card No. 06·39665 · Updated · Standard formula, shown below

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

  1. Update your system packages
    Run sudo yum update -y (or sudo dnf update -y on CentOS 8+). This ensures your package index is current and reduces the chance of dependency conflicts.
  2. 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. Use wget or curl to download it. For example, on CentOS 7:
    sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
  3. Install the repository package
    Run sudo yum localinstall mysql80-community-release-el7-7.noarch.rpm (or sudo dnf localinstall ...). This adds the MySQL repository to your system.
  4. Verify the repository is enabled
    Run yum repolist enabled | grep mysql (or dnf repolist). You should see entries like mysql80-community. If you don’t, check the repository file at /etc/yum.repos.d/mysql-community.repo.
  5. Install MySQL server
    Run sudo yum install mysql-community-server -y (or sudo dnf install mysql-community-server -y). This installs the MySQL server, client, and common libraries.
  6. Start the MySQL service
    Run sudo systemctl start mysqld. To ensure it starts automatically at boot, run sudo systemctl enable mysqld.
  7. 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
  8. Secure the installation
    Run sudo 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. Answer Y to all security prompts.
  9. Test the connection
    Log in as root with your new password:
    mysql -u root -p
    You should see the MySQL prompt. Run SELECT VERSION(); to confirm the server is working.
  10. 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/tcp and reload with sudo firewall-cmd --reload. Only do this if necessary.
  • Back up your data – Regularly use mysqldump to back up your databases.
  • Check the MySQL error log – If something goes wrong, look at /var/log/mysqld.log for 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 all and try again. If using CentOS 8/9, ensure the mysql80-community repo is enabled and the mysql-tools-community repo 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

The whole Numbers and Powers drawer →