As we know
that MySQL replication works with binary log. Master create logs of the queries
which made modification in database. And then master send event to the slave to
execute that query. So on master server we need to setup binary log and also we
can inform master server to create log for which database(the databases which
we want to replicate).
In my case I have added following line in my master server my.ini
file to start binary logging for database replication_test.
create log-bin directory if folder not available log-bin="C:/xampp/mysql/data/binlog/bin-log"
#Specifing path to store binary log
log-bin-index=c:/xampp/mysql/data/bin-log-index #specify path to store
binary log indexes
binlog-do-db=replication_test #Database for which binary log is enable.
server-id=1 #Server ID which is mandatory for replication
After specifying above setting in your master server please restart mysql service on your master server.
After restarting your master server please hit
following query on your master :
If your query is giving following result then every thing is fine:
Now let us
create a user on master server which will be used for the replication purpose.
All you need to do here is to create a user and grant replication slave
privileges to that user. You can do it using following query on master server
Now almost you are done with the setting which is required on master server. Below steps are now for the setting of slave server. Let us jump to another server now.
show master status
If your query is giving following result then every thing is fine:

CREATE USER 'replication_user'@'%' IDENTIFIED BY
'replication';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
Now almost you are done with the setting which is required on master server. Below steps are now for the setting of slave server. Let us jump to another server now.
Slave Server Setup: On slave
first you need to mention your master server details in your my.cnf/my.ini
server-id=2 #Unique number like primary server
master-host=192.168.43.210 #IP of master server
master-user=replication_user #User we have created for the replication on
slave
master-password=replication #Password of the userver
master-connect-retry=60 #retry time
replicate-do-db=replication_test #name of the database to be replicated.
Now after adding this configuration please make
sure that your both server(master and slave) has same data for the database
which you are going to replicate(in our case replication_test database). If
your both server has identical data then you can restart your slave server
after adding above configuration. Otherwise first make both master and slave
identical and then restart your slave server.Now hit the following query on
slave server:
And if you are getting following result then every thing is fine and you did it:
SHOW SLAVE STATUS;
And if you are getting following result then every thing is fine and you did it:
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Waiting for master to send event | 192.168.43.210 | replication_user | 3306 | 60 | bin-log.000043 | 106 | mysqld-relay-bin.000002 | 241 | bin-log.000043 | Yes | Yes | replication_test | | | | | | 0 | | 0 | 106 | 241 | None | | 0 | No | | | | | | 0 |
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
To check the replication let us create a table on master server and see what is happening on slave server. I have used following query to create table on master server:
Now I can see the same table created in the slave replication_test database. WOW!!!!!
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Waiting for master to send event | 192.168.43.210 | replication_user | 3306 | 60 | bin-log.000043 | 106 | mysqld-relay-bin.000002 | 241 | bin-log.000043 | Yes | Yes | replication_test | | | | | | 0 | | 0 | 106 | 241 | None | | 0 | No | | | | | | 0 |
+----------------------------------+----------------+------------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+------------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
To check the replication let us create a table on master server and see what is happening on slave server. I have used following query to create table on master server:
CREATE TABLE `replication_test`.`test`( `id` INT(11) NOT
NULL AUTO_INCREMENT, `name` VARCHAR(255), PRIMARY KEY (`id`) );
Now I can see the same table created in the slave replication_test database. WOW!!!!!
No comments:
Post a Comment