MySql Master Slave Replication Setup

In this article, I will let you know how to set up Master-Slave Replication in MySql.
For this you should have two IPs, one for Master server and another one for Slave server.
Consider we have the below two IPs for Master & Slave server.
10.10.19.20 – Master Server
10.10.19.22 – Slave Server
1. Install MySql in both the Master & Slave Server. You can visit the link given below and download the MySql as per your OS.
http://dev.mysql.com/doc/refman/5.7/en/installing.html
Step 1.
Configure the Master Server :-
Open the file vim /etc/mysql/my.cnf
Change the bind address as
bind-address = 10.10.19.20
Now change the Server-id, It should be unique and does not match with any server-id within the Replication group. I am giving the server-id 1, just for ease.
server-id=1
Now enable the logging of log_bin, our slave servers will copy the data from this bin file. Uncomment this line in /etc/mysql/my.cnf as given below.
log_bin = /var/log/mysql/mysql-bin.log 
Finally, we have to decide which database we want to replicate to slave server, mention the database name.
binlog_do_db = db_name 
Restart the MySql.
sudo service mysql restart
Now, we will log-in to mysql shell to give access to slave-user to replicate data from Master server.
mysql -uroot -p 
To give access to the slave-user, we will grant permission.
GRANT REPLICATION SLAVE ON *.* TO 'slave-user'@'%' IDENTIFIED BY 'password'; 
FLUSH PRIVILEGES; 
Switch to database which you want to replicate.
USE db_name 
FLUSH TABLES WITH READ LOCK; 
SHOW MASTER STATUS; 
Result will look this.
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      110 | db_name      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec) 
Above given is the position from where the Slave server start replication from Master.
Take the database backup using mysqldump
mysqldump -uroot -p db_name --master-data --single-transaction > db_name.sql 
Now Unlock the database and exit.
UNLOCK TABLES; 
QUIT; 
We are done with Master-Server configuration.
Step 2.
Import the database, which we have previously taken at Master-Server to Slave-Server.
mysql -u root -p db_name < /path/to/db_name.sql 
Configure the Slave-Server( Ip- 10.10.19.22) :-
Open the file /etc/mysql/my.cnf
vi /etc/mysql/my.cnf 
Change the server-id, log_bin, relay-log, binlog_do_db as given below.
server-id = 2 
relay-log = /var/log/mysql/mysql-relay-bin.log 
log_bin = /var/log/mysql/mysql-bin.log 
bin_log_do_db = db_name 
Restart the mysql.
sudo service mysql restart
Log-in to MySql shell in Slave-server and enable replication.
mysql -u root -p 
CHANGE MASTER TO MASTER_HOST='10.10.19.20',MASTER_USER='slave-user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  117; 
Above statement do following things.
1. It update the current server as slave of Master-Server.
2. It gives to server the correct login credentials.
3. It inform the Slave-Server replication should start from where; the master log file and log position come from the numbers we note down previously.
Start the slave server from the MySql shell.
START SLAVE; 
You can see the Slave-Server status from MySql shell.
SHOW SLAVE STATUS; 
If getting any issue while connecting start the slave with a command to skip over it.
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START; 
Your MySql Master-Slave Replication is ready to use.

Inner Join in MySQL

In this Article, I will let you know about Inner Join in MySQL.
Inner Join :-
Inner Join also known as Equi Join, Normal Join, it shows the only the result of two or more tables as condition satisfied.
Let’s say we have two tables EMPLOYEE and DEPARTMENT
Below is the data in both the tables.
mysql> SELECT * FROM EMPLOYEE ;
+------+------+-------------+--------+-----------+--------------------+---------+
| id   | EID  | EMP_NAME    | DEP_ID | Phone     | Email              | City    |
+------+------+-------------+--------+-----------+--------------------+---------+
|    1 | CS01 | RAM         | D01    | 96465646  | abcd1@gmail.com    | Delhi   |
|    2 | CS02 | Mohan       | D02    | 96423246  | abc1d@gmail.com    | Gurgaon |
|    3 | CS03 | Mohi        | D01    | 96464546  | a1bcd@gmail.com    | Noida   |
|    4 | CS04 | Mohit       | D02    | 9611116   | cd@gmail.com       | Delhi   |
|    5 | CS05 | Sanjay      | NULL   | 92222246  | a@gmail.com        | Gurgaon |
|    6 | CS06 | RAJ         | D02    | 963333    | xqwe@gmail.com     | Noida   |
|    7 | CS07 | InderJeet   | D01    | 94444446  | 43abcd@gmail.com   | Gurgaon |
|    8 | CS08 | Rohit sethy | D03    | 66665646  | 565abcd@gmail.com  | Delhi   |
|    9 | CS09 | Arindam     | NULL   | 98888846  | abcd343@gmail.com  | Delhi   |
|   10 | CS10 | Vijay       | D01    | 6677746   | ab3333cd@gmail.com | Delhi   |
|   11 | CS11 | Prashant    | NULL   | 9666666   | abcd232@gmail.com  | Noida   |
|   12 | CS12 | Vikram      | D01    | 98888886  | ab2c3d5@gmail.com  | Delhi   |
|   13 | CS13 | Prateek     | D03    | 923232326 | ab2cd55@gmail.com  | Noida   |
+------+------+-------------+--------+-----------+--------------------+---------+
13 rows in set (0.00 sec)
mysql> SELECT * FROM DEPARTMENT ;
+------+--------+----------+
| id   | Dep_id | dep_name |
+------+--------+----------+
|    1 | D01    | Finance  |
|    2 | D02    | IT       |
|    3 | D03    | HR       |
|    4 | D04    | INFRA    |
+------+--------+----------+
4 rows in set (0.00 sec)
As per definition, Inner Join will print only those records which match the condition.
mysql> SELECT E.EID, E.EMP_NAME, D.DEP_NAME FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON E.DEP_ID = D.DEP_ID ; 
+------+-------------+----------+
| EID  | EMP_name    | dep_name |
+------+-------------+----------+
| CS01 | RAM         | Finance  |
| CS02 | Mohan       | IT       |
| CS03 | Mohi        | Finance  |
| CS04 | Mohit       | IT       |
| CS06 | RAJ         | IT       |
| CS07 | InderJeet   | Finance  |
| CS08 | Rohit sethy | HR       |
| CS10 | Vijay       | Finance  |
| CS12 | Vikram      | Finance  |
| CS13 | Prateek     | HR       |
+------+-------------+----------+
10 rows in set (0.02 sec)
As we can see in above result, it shows the only result which match the condition. It doesn’t print the record where DEP_ID is NULL in Employee table i.e. DEP_ID does not matched.