Removing Existing Database Packages
CentOS 7 typically ships with MariaDB as the default database, which conflicts with MySQL. Before installation, check for and remove any existing MySQL or MariaDB installations.
rpm -qa | grep -E 'mysql|mariadb'If MariaDB libraries are present, remove them. Note that dependent packages like postfix might block this removal; remove them first if necessary.
yum remove postfix -y
yum remove mariadb-libs -y
rm -rf /var/lib/mysqlConfiguring the Yum Repository
Navigate to a working directory and download the official MySQL Yum repository RPM package.
cd /usr/local/src
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpmInstall the repository definition to enable yum to locate MySQL packages.
rpm -ivh mysql57-community-release-el7-11.noarch.rpmInstalling MySQL Server
Proceed to install the MySQL community server.
yum install mysql-community-server -yIf the installation halts due to GPG key verification failures, import the updated key from the MySQL official site.
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server -yInitializing and Starting the Service
Initialize the MySQL data directory and ensure the correct file ownership is set.
mysqld --initialize
chown -R mysql:mysql /var/lib/mysqlStart the mysqld service and verify it is running correctly.
systemctl start mysqld
systemctl status mysqldSecuring the Root Account
MySQL 5.7 generates a temporary password for the root user during initialization. Retrieve this password from the log file.
grep 'temporary password' /var/log/mysqld.logLog in to the MySQL client using the retrieved password.
mysql -u root -pUpdate the root password immediately. The default security policy requires a strong password containing uppercase, lowercase, numbers, and special characters.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New_Strong_Password_123';Configuring Remote Access
To enable remote connections for the root user, switch to the system database and update the host permissions.
USE mysql;
UPDATE user SET Host='%' WHERE User='root' AND Host='localhost';
FLUSH PRIVILEGES;Adjust the firewall settings to allow incoming traffic on the default MySQL port.
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reloadFinally, enable the MySQL service to start automatically on system boot.
systemctl enable mysqld