Sunday, July 2, 2017

Install MySQL

This post is one of a series

Create an environment for Web development
  1. Vagrant/Virtualbox
  2. Install Apache or Nginx for PHP or Nginx for Python
  3. Install MySQL - You are here
  4. Install PHP / Install Python

MySQL

We will install MySQL. MySQL is a famous software that is used to use relational database. MySQL is a opensource software and is widely used in the world.
At first, we need to uninstall MariaDB that is by default installed in the Cent OS 7:
$ sudo yum remove mariadb-libs
$ sudo rm -rf /var/lib/mysql/
We will install MySQL now. Go to the link: https://dev.mysql.com/downloads/repo/yum/
Note the information highlighted by red underline. We are using Cent OS 7 (Linux). Cent OS is a very similar OS to Red Hat Enterprise Linux, so we can use same MySQL as Red Hat Linux. We will download this package for our Cent OS.

Run these commands to install:
$ wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
$ sudo rpm -ivh mysql57-community-release-el7-11.noarch.rpm
$ sudo yum install mysql-server
Then MySQL is installed in your virtual machine.
Start MySQL with this command:
$ sudo systemctl start mysqld
To check the status of MySQL, do this command:
$ sudo systemctl status mysqld
Register mysqld to make it start from the beginning:
$ sudo systemctl enable mysqld
If you have made some mistake and want to un-install the rpm package, see here: Remove package on CentOs

FYI For CentOS6

But if you are using CentOS 6, you need to use the following:

The commands for CentOS6: 
$ wget https://dev.mysql.com/get/mysql57-community-release-el6-11.noarch.rpm 
$ sudo rpm -ivh mysql57-community-release-el6-11.noarch.rpm 
$ sudo yum install mysql-server 
$ sudo service mysqld start 
$ sudo service mysqld status 
$ sudo chkconfig mysqld on

Initialize MySQL

Now we will set up our MySQL that is needed when we start MySQL for the first time. Do the following command:
$ sudo grep 'temporary password' /var/log/mysqld.log
The text underlined with red is the temporary password

We have a temporary password now. Run the following:
$ sudo mysql_secure_installation
You will be asked password, so use the temporary password that we obtained just now. Every question other than it should be ok to answer yes (y). Maybe you will be asked a new password, but the new password must contain a big letter, a small letter and a special letter like "@" and it must be longer than 8 letters.
Do the following command to log into MySQL with the new password you created just now:
$ mysql -u root -p
But I think 8 letters with a big letter, a small letter and a special letter is not useful if we often login and logout. So we will change the password and the password rule. At first, we will check current password rule:
mysql> SHOW VARIABLES LIKE 'validate_password%';
The condition for password creation

Then do the following commands to change the rule:
mysql> SET GLOBAL validate_password_length=4;
mysql> SET GLOBAL validate_password_policy=LOW;
Conditions are changed

We will define the password as "root". Do the following command:
mysql> set password for root@localhost=password('root');

Now your password for MySQL is changed to "root". User name is also "root". To quit MySQL, do the following command:
mysql> quit

Use MySQL from the host computer

You need to add privileges to access from outside the server:
$ mysql -u root -proot
mysql> use mysql;
mysql> GRANT ALL ON *.* to root@'192.168.33.10' IDENTIFIED BY 'root';
mysql> GRANT ALL ON *.* to root@'192.168.33.1' IDENTIFIED BY 'root';
mysql> SET PASSWORD FOR 'root'@'192.168.33.10' = PASSWORD('root');
mysql> flush privileges;
mysql> exit;

The use the following information to access from outside the server:
Host name: 192.168.33.10 (or your virtual machine's IP address)
port: 3306
user: root
password: root