How to Install MySQL on CentOS 7
1: Download the Yum repository.
In order to add the new repository, we need to download it first. To do this, go to the MySQL Yum Repository page, linked here. You then need to click on the “Download” button next to the download for Red Hat Enterprise Linux 7. Once the page loads, right-click “No thanks, just start my download” found at the bottom half of the page, and click on “Copy Link” or “Copy Linked Address”.
Now, SSH or log into your CentOS 7 machine, and make sure that you have the “wget” tool installed by just typing “wget” into the bash shell. If you don’t have wget installed, simply install it by entering the following command:
yum install -y wget
Within the next minute, wget will be downloaded and installed onto your system. Once you have wget, you can now download the repository file. Navigate to a folder where you want the download to be saved, and enter the following command:
wget <paste the download link here>
Give it a moment to download and save the file to your machine. When done, proceed to the next step.
2: Add the MySQL Yum repository.
Adding this new repository to your Yum program is very easy. Just enter the following command:
sudo rpm -Uvh <enter the package name here>
Replace <enter the package name here> with the name of your package. You can use the Tab key to autocomplete the entry if you have partially typed it already. Enter your user account’s password when prompted to. Once you have successfully added the Yum repository, proceed to the next step.
3: Install MySQL.
Now that we have the repository added, we can now install the official MySQL software package. Just enter the following command to install:
sudo yum install mysql-community-server
Press the ‘y’ key if prompted to. Within a couple of minutes, the MySQL server will be downloaded and installed onto your CentOS 7 system.
4: Set up MySQL.
You now HAVE to start the server and set the root password. First, start your MySQL server by executing the following command:
sudo systemctl start mysqld.service
When this is done for the first time, the root user is created with a random password, and the password can be found in the error log file. Use this next command to reveal the password:
sudo grep ‘temporary password’ /var/log/mysqld.log
the output should contain the password itself. Now you need to log in as the root user using this password. Enter this next command to enter the MySQL query shell:
mysql -uroot -p
Enter the password you just read from the output. Once you have logged in, change the root password by entering the following command (NOTE: Don’t forget to change ‘mypassword’ to the password you wish to use! Your password must be at least 8 characters long, and contain an uppercase letter, a lowercase letter, a special character, and at least one digit. ):
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘mypassword’;
You have now successfully set your new root account password.
#: Additional Tips
If you ever need to update your MySQL server, just do it though Yum by simply upgrading the package:
sudo yum update mysql-server
This update process also happens when you update the entire system using Yum. Once the server is updated, it automatically restarts, it is important to run “mysql_upgrade” in order to have MySQL perform check with its plugins to make sure that everything will continue to run smoothly.
If you need your MySQL server to run every time you boot the machine up, enter the following command:
sudo systemctl enable mysqld.service
Now when you restart your machine, your MySQL server will be automatically started.
If you have followed this tutorial correctly, your CentOS 7 machine should now have a copy of MySQL server running on it. From here, you can work with databases, add plugins and extensions to make working with these databases easier, and so on.