If you try and use “yum” to install MySql on CentOS, you’ll find that it actually installs MariaDB instead. Basically, MariaDB is a fork of MySql after it went under Oracle’s control. It’s meant to be a “drop in” replacement, meaning that the user is not supposed to notice any difference between the two and all packages that work with MySql will work with MariaDB.
However, if you’re interested in installing MySql directly from the repos, here’s how you should go about it.
Install the “PHP-PDO” Package
PHP includes a special interface for interacting with databases called “PHP PDO”. It brings a bunch of benefits including efficiency and security. It’s installed automatically when you install the package for MySql support using the following command:
yum install php-mysql
Installing this package will install “php-pdo” as a dependency as shown here:
In addition, this package is also required by the popular web hosting control panel WHMCS in case you ever want to install it. So it’s a good thing to have in general.
Getting the Repo for MySql
To get the official repo for MySql, visit the following website: https://dev.mysql.com/downloads/repo/yum/. Over here, scroll down and you’ll find a bunch of download locations for RedHat and Fedor distributions. CentOS is basically just RedHat, so choose the version of the OS that you’re running and click the “Download” link as shown here:
This will take you to a page asking you to sign up for an Oracle account. You can ignore all that and go directly to the link at the bottom saying “No thanks, just start my download”. However, instead of downloading the rpm onto your PC, we’re going to copy the URL of the link instead:
In this example, the URL links to https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm. This is the RPM we’re going to use in Linux to add the MySql repository.
Open up your Linux terminal and enter the following commands:
rpm -ivh mysql-community-release-el7-5.noarch.rpm yum update
Replace the text in bold with the URL you got earlier, and the name of the file that was actually downloaded. Before you do this, I suggest you open up a new temporary folder to download the file using wget, and then delete it.
Running these commands might take a while. Especially running “yum update” will download a lot of stuff. After it’s done, you’re now ready to install MySql server.
Installing MySQL Server
After the repo has been installed, installing MySql is easy. Just run:
yum install mysql-server
This will install the most stable version of MySql. You can get information about the “Generally Available” (GA) release on this page: https://dev.mysql.com/downloads/mysql/. You can see that the version there is 5.7.18. And that’s what is installed when we run the above command in the Linux command line as shown here:
In addition, it also includes the MySql client. This allows us to interact with the server, add users, passwords, and all that sort of thing. You can check the version after installation using the command:
Which gives the following output:
We should also start the mysql daemon service like this:
systemctl start mysqld
Logging in for the First Time
When MySql installs, it generates a temporary random password for root in the following file:
Open it using a text editor like vi. You should see the following line somewhere inside:
“[Note] A temporary password is generated…”. Copy this password – you’ll need it for the first time you log in as root. Then enter the following command:
mysql -u root -p
This will prompt you for the root password you obtained earlier. Simply enter it and you’re in! Here’s a screenshot:
Congratulations! You’ve just logged into the MySql server for the first time on Linux using the CLI.