Tuesday, August 28, 2018

.::: How To Redundant Set Master to Master(Replica) and Master to Slave(Mirroring) On MySQL (Include MariaDB) :::.

A. Configuring MySQL Server on Server01
1. Edit & add db file conf /etc/my.cnf
server_id=1
replicate-do-db=teguht
#bind-address=10.10.10.10
log-bin      = mysql-bin
binlog_do_db = teguht

set global read_only=on;
set global read_only=off;
skip-slave-start

set global read_only=1;

======  

[root@MariaDB002 ~]# cat /etc/my.cnf.d/server.cnf

## -> only 1 database (teguhth)
[mariadb]
server_id=1
replicate-do-db=teguhth
#bind-address=10.10.10.10
log-bin      = mysql-bin
binlog_do_db = teguhth

## -> more 2 database (teguhth, secretdb, labdb)
[mariadb]
server_id=1
replicate-do-db=teguhth
replicate-do-db=secretdb
replicate-do-db=labdb

#bind-address=10.10.10.10
log-bin      = mysql-bin
binlog_do_db = teguhth
binlog_do_db = secretdb
binlog_do_db = labdb


sample log


[root@teguht01 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=1
replicate-do-db=teguht
#bind-address=10.10.10.10
log-bin      = mysql-bin
binlog_do_db = teguht
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = /var/lib/mysql/mysql-bin

skip-slave-start

 
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@teguht01 ~]#

2. Log on to the MariaDB server01 as root, create the user slave and assign the necessary grants
create database teguht;
CREATE USER 'replica'@'localhost' IDENTIFIED BY 'password';
grant replication slave on *.* to replica@'%' identified by 'password';
flush privileges;
SHOW GRANTS FOR replica@'%';
exit

sample log

mysql> create database teguht;
mysql> CREATE USER 'replica'@'localhost' IDENTIFIED BY 'password';
mysql> grant replication slave on *.* to replica@'%' identified by 'password';
mysql> flush privileges;
mysql> SHOW GRANTS FOR replica@'%';
Mysql> exit

3. Log on to the MariaDB server as root, create the user slave and assign the necessary grants
show master status

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     2511 | teguht       |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

4. record master status on notepad from result output status server01
change master to master_host='10.10.10.20', master_user='replica', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=2511;

5. Snapsot Database MySQL
set global read_only=on;

# mysqldump -u root -p  teguht  > /data/backup/teguht.sql
set global read_only=off; 


6. Copied to server 2
# scp /data/backup/teguht.sql root@10.10.10.20:/root/


B. Configuring MySQL Server on Server02

1. Edit & add db file conf /etc/my.cnf
server_id=2
replicate-do-db=teguht
#bind-address=10.10.10.20
log-bin      = mysql-bin
binlog_do_db = teguht

sample log

[root@teguht02 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=2
replicate-do-db=teguht
#bind-address=10.10.10.20
log-bin      = mysql-bin
binlog_do_db = teguht
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = /var/lib/mysql/mysql-bin

skip-slave-start

 
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@teguht01 ~]#

2. Log on to the MariaDB server02 as root, create the user slave and assign the necessary grants
create database teguht;
CREATE USER 'replica'@'localhost' IDENTIFIED BY 'password';
grant replication slave on *.* to replica@'%' identified by 'password';
flush privileges;
SHOW GRANTS FOR replica@'%';
exit

sample log

mysql> create database teguht;
mysql> CREATE USER 'replica'@'localhost' IDENTIFIED BY 'password';
mysql> grant replication slave on *.* to replica@'%' identified by 'password';
mysql> flush privileges;
mysql> SHOW GRANTS FOR replica@'%';
Mysql> exit

3. Log on to the MariaDB server as root, create the user slave and assign the necessary grants
show master status

sample log

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     2522 | teguht       |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

4. record master status on notepad from result output status server02
change master to master_host='10.10.10.20', master_user='replica', master_password='password', master_log_file='mysql-bin.000002', master_log_pos=2522;


C. Set Master to Master On Server01 if you want to set Master to slave, donot do it

1. Set master to master status on MySQL Server01
stop slave;
change master to master_host='10.10.10.20', master_user='replica', master_password='password', master_log_file='mysql-bin.000002', master_log_pos=2522;
start slave;
SHOW SLAVE STATUS\G;

sample output

mysql> stop slave;
mysql> change master to master_host='10.10.10.20', master_user='replica', master_password='password', master_log_file='mysql-bin.000002', master_log_pos=2522;
mysql> start slave;
mysql> SHOW SLAVE STATUS\G;

2. Checking master status
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.10.20
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2896
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 617
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: teguht
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2896
              Relay_Log_Space: 790
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 7f28cbab-a449-11e8-8cd0-000c29e77568
             Master_Info_File: /var/lib/mysql/mysql-master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

D. Set Master to Master On Server02

1. Set master to master status on MySQL Server02
stop slave;
change master to master_host='10.10.10.10', master_user='replica', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=2511;
start slave;
SHOW SLAVE STATUS\G;

sample output

mysql> stop slave;
mysql> change master to master_host='10.10.10.10', master_user='replica', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=2511;
mysql> start slave;
mysql> SHOW SLAVE STATUS\G;

2. Checking master status
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.10.10
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2896
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 617
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: teguht
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2896
              Relay_Log_Space: 790
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 7f28cbab-a449-11e8-8cd0-000c29e77568
             Master_Info_File: /var/lib/mysql/mysql-master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

E. Testing  insert table on database server01
1. Server 01

use teguht;
create table teguhttable(No int(5), Name varchar(30), Email varchar(30));
insert into teguhttable values (1,'Teguh','teguh@microsoft.com');
insert into teguhttable values (2,'Tri','tri@microsoft.com');
select * from teguhttable;

sample log
[root@teguht01 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.34-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use teguht;
Database changed
mysql> create table teguhttable(No int(5), Name varchar(30), Email varchar(30));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into teguhttable values (1,'Teguh','teguh@microsoft.com');
Query OK, 1 row affected (0.00 sec)

mysql> insert into teguhttable values (2,'Tri','tri@microsoft.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from teguhttable;
+------+-------+---------------------+
| No   | Name  | Email               |
+------+-------+---------------------+
|    1 | Teguh | teguh@microsoft.com |
|    2 | Tri   | tri@microsoft.com   |
+------+-------+---------------------+
2 rows in set (0.01 sec)

mysql>


2. Server02
use teguht;
SELECT * FROM teguhttable;

sample log

[root@teguht02 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.34-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use teguht;
Database changed

mysql> select * from teguhttable;
+------+-------+---------------------+
| No   | Name  | Email               |
+------+-------+---------------------+
|    1 | Teguh | teguh@microsoft.com |
|    2 | Tri   | tri@microsoft.com   |
+------+-------+---------------------+
2 rows in set (0.01 sec)

mysql>

F. Testing  insert table on database server02
1. Server02

use teguht;
insert into teguhttable values (3,'Harto','harto@microsoft.com');
insert into teguhttable values (4,'TH','th@microsoft.com');
select * from teguhttable;

sample log

[root@teguht02 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.34-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use teguht;
Database changed
mysql> insert into teguhttable values (3,'Harto','harto@microsoft.com');
Query OK, 1 row affected (0.01 sec)

mysql> insert into teguhttable values (4,'TH','th@microsoft.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from teguhttable;
+------+-------+---------------------+
| No   | Name  | Email               |
+------+-------+---------------------+
|    1 | Teguh | teguh@microsoft.com |
|    2 | Tri   | tri@microsoft.com   |
|    3 | Harto | harto@microsoft.com |
|    4 | TH    | th@microsoft.com    |
+------+-------+---------------------+
4 rows in set (0.00 sec)

mysql>

2. Server01
[root@teguht01 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.34-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use teguht;
Database changed

mysql> select * from teguhttable;
+------+-------+---------------------+
| No   | Name  | Email               |
+------+-------+---------------------+
|    1 | Teguh | teguh@microsoft.com |
|    2 | Tri   | tri@microsoft.com   |
|    3 | Harto | harto@microsoft.com |
|    4 | TH    | th@microsoft.com    |
+------+-------+---------------------+
4 rows in set (0.00 sec)

mysql> 


G. If You want to Install and Configuration PCS, COROSYNC, and Pacemaker for HA(High Availability) On Linux, Centos, Rhel visit
http://teguhth.blogspot.com/2018/08/how-to-install-and-configuration-pcs.html

I. if you get Error "Duplicate entry"  on slave or master go to http://teguhth.blogspot.com/2018/09/how-to-simple-troubleshooting-error.html

No comments:

Post a Comment

Popular Posts