MySQL is an opensource, Relational Database Management System. MySQL is a most popular database server for Linux systems, it also supports a large number of platforms. In MySQL, we can easily create a stored procedure and execute SQL queries. MySQL Community Edition is a freely downloadable version and uses for your applications.
The MySQL official team provides yum repository for the MySQL installation on RPM based systems. Which can be used to install MySQL on CentOS and Fedora systems.
This tutorial will help you to Install MySQL Server on CentOS/RHEL 7/6, Fedora 32/31/30/29 using the default package manager.
Step 1 – Configure Yum Repository
First, we need to add the MySQL yum repository in our system provided by MySQL. Execute one of below command as per your operating system version.
### On CentOS/RHEL 7 system ### rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm ### On CentOS/RHEL 6 system ### rpm -Uvh https://repo.mysql.com/mysql80-community-release-el6-3.noarch.rpm ### On Fedora 32 system ### rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc32-1.noarch.rpm ### On Fedora 31 system ### rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc31-1.noarch.rpm ### On Fedora 30 system ### rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc30-1.noarch.rpm
Step 2 – Install MySQL Community Server
The MySQL yum repository contains multiple repositories configuration for multiple MySQL versions. So first disable all repositories in mysql repo file.
sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo
Then execute one of the followings commands as per your operating system to install MySQL.
yum --enablerepo=mysql57-community install mysql-community-server ## CentOS & RedHat dnf --enablerepo=mysql57-community install mysql-community-server ## Fedora Systems
Step 3 – Start MySQL Service
Start the MySQL server using the following command from the Linux terminal.
service mysqld start
systemctl start mysqld.service
Step 4 – Find MySQL root Password
With the installation of MySQL 5.7, a temporary password is created for the MySQL root user. You can find the temporary password generated in log files.
grep "A temporary password" /var/log/mysqld.log
[Note] A temporary password is generated for [email protected]: hosygMikj1+t636
Step 5 – MySQL Post Install Setup
After installing MySQL first time, execute
mysql_secure_installation command to secure MySQL server. It will prompt for few question’s, we recommended to say yes ( y ) for each.
Enter password for user root: The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. 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 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 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 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 - 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 Success. All done!
Step 6 – Restart and Enable MySQL Service
After completing all MySQL installation steps and doing all initial settings, restart MySQL service using the following command.
### Using SysVinit service mysqld restart ### Using Systemd systemctl restart mysqld.service
Also, enable service to auto start on system reboot with the following command.
### Using SysVinit chkconfig mysqld on ### Using Systemd systemctl enable mysqld.service
Step 7 – Working with MySQL
Now connect mysql database server Linux shell using below command. It will prompt for the password for authentication. On successful login, you will get the MySQL command prompt, where we can execute SQL queries.
mysql -h localhost -u root -p
After login, You can use following commands to create a new database, create a user and assign privileges to the user on the database. Change values as per your requirements.
### CREATE DATABASE
mysql> CREATE DATABASE mydb;
### CREATE USER ACCOUNT
mysql> CREATE USER 'dbuser'@'192.168.10.101' IDENTIFIED BY 'secret';
### GRANT PERMISSIONS ON DATABASE
mysql> GRANT ALL ON mydb.* TO 'dbuser'@'192.168.10.101';
### RELOAD PRIVILEGES
mysql> FLUSH PRIVILEGES;