How to Install MySQL 8.0/5.7 on CentOS 7 / RHEL 7
MySQL is a free and open-source database management system, commonly used in web applications to store and retrieve records and information.
Add MySQL Repository
MySQL is no longer distributed through OS repositories. So, you would need to add an official repository of MySQL to install the MySQL community server.
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-4.noarch.rpm
Make sure the MySQL repository has been added and enabled by using the following command.
yum repolist all | grep mysql | grep enabled
Output: May Look like.
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 221 mysql-tools-community/x86_64 MySQL Tools Community enabled: 135 mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 301
Install MySQL Community Server
Oracle currently offers both stable versions (v8.0 and v5.7). You can choose the one you want to install on your machine.
Install MySQL 8.0
Use the yum command to install MySQL community server 8.0.
yum install -y mysql-community-server
Install MySQL 5.7
If you want to try the older version of MySQL, then install MySQL 5.7 on your machine with the below command.
yum install -y mysql-community-server --disablerepo=mysql80-community --enablerepo=mysql57-community
After the installation of MySQL, you can start the MySQL server using the following command.
systemctl start mysqld
Then, enable MySQL service to start automatically at system startup.
systemctl enable mysqld
Finally, verify if the MySQL server is started using the following command.
systemctl status mysqld
Initial MySQL Root Password
In CentOS/RHEL, you can find the initial MySQL root password in
/var/log/mysqld.log. You can use the below command to take the password from the log file.
cat /var/log/mysqld.log | grep -i 'temporary password'
2021-11-27T08:27:27.063799Z 6 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: PJyCjhTjP1:n
Secure MySQL server
Now, you need to run
mysql_secure_installation to secure your MySQL installation. This command takes care of setting the root password, removing anonymous users, disallowing root login remotely, etc.
Securing the MySQL server deployment. Enter password for user root: << Enter the initital MySQL root password The existing password for the user account root has expired. Please set a new password. New password: << Enter new MySQL root password Re-enter new password: << Re-enter new MySQL root password The 'validate_password' component is installed on the server. The subsequent steps will run with the existing configuration of the component. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : N << Type N as we have already set new password ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y << Type Y to remove anonymous users Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y << Type Y to disallow root login remotely Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y << Type Y to remove test database - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y << Type Y to reload privilege tables Success. All done!
Work with MySQL Server
Login to MySQL server with the root user and its password.
mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
phpMyAdmin is an open-source web-based management tool to manage MySQL and MariaDB databases. Follow the below link to install and configure phpMyAdmin based on your operating system.