본문 바로가기

리눅스

[리눅스] mysql 5.7 replication 설정

반응형

mysql 5.7 replication 설정

DB IP 비고
master 192.168.0.100  
slave 192.168.0.101  
slave 192.168.0.102  

mysql my.cnf 편집

[마스터]

  • server_id = 1
  • log-bin = mysql-bin.log
vim /etc/my.cnf
[client]
default-character-set = utf8

[mysqld]
server_id = 1
log-bin = mysql-bin.log
binlog_cache_size = 2M
max_binlog_size = 512M
expire_logs_days = 7
log-bin-trust-function-creators = 1
#binlog_do_db = wwwdb
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysqldump]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
systemctl restart mysqld

[슬레이브]

  • server_id = 2
  • log-bin = mysql-bin.log
  • relay-log = mysql-relay-bin.log
  • expire_logs_days = 7
  • max_binlog_size = 512M
vim /etc/my.cnf
[client]
default-character-set = utf8

[mysqld]
server_id = 2
log-bin = mysql-bin.log
relay-log = mysql-relay-bin.log
expire_logs_days = 7
max_binlog_size = 512M
#binlog_do_db = wwwdb
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysqldump]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
systemctl restart mysqld

mysql replication SQL 설정

REPLICATION 유저 생성 및 권한 부여(master/slave)

  • CREATE USER 'ID'@'IPADDR' IDENTIFIED BY 'PASSWORD';
  • GRANT REPLICATION SLAVE ON *.* TO 'ID';
create user 'repl'@'%' identified by 'Repl11@@@';
grant replication slave on *.* to 'repl';
flush privileges;
mysql> create user 'repl'@'%' identified by 'Repl11@@@';

mysql> grant replication slave on *.* to 'repl';

mysql> flush privileges;

생성된 유저 조회

mysql -uroot -p'password1!' mysql -e "select user, host from user;"
$ mysql -uroot -p'password1!' mysql -e "select user, host from user;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| repl      | %         |
| mysql.sys | localhost |
| root      | localhost |
+-----------+-----------+

####GRANT REPLICATION SLAVE ON *.* TO 'ID'@'IPADDR' IDENTIFIED BY 'PASSWORD';

728x90

mysql sql 설정

[마스터 설정]

log_bin 로그 경로 확인

mysql -uroot -p'password1!' -e "show variables like '%log_bin%';"
#SHELL 모드
$ mysql -uroot -p'password1!' -e "show variables like '%log_bin%';"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------------+--------------------------------+
| Variable_name                   | Value                          |
+---------------------------------+--------------------------------+
| log_bin                         | ON                             |
| log_bin_basename                | /var/lib/mysql/mysql-bin       |
| log_bin_index                   | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | ON                             |
| log_bin_use_v1_row_events       | OFF                            |
| sql_log_bin                     | ON                             |
+---------------------------------+--------------------------------+
  • SQL 모드
show variables like '%log_bin%';

마스터 서버 상태 확인

mysql -uroot -p'password1!' -e "SHOW MASTER STATUS;"
#SHELL 모드
$ mysql -uroot -p'password1!' -e "SHOW MASTER STATUS;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     1285 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
  • SQL 모드
SHOW MASTER STATUS;

[슬레이브 설정]

log_bin 로그 경로 확인

mysql -uroot -p'password1!' -e "show variables like '%log_bin%';"
#SHELL 모드
$ mysql -uroot -p'password1!' -e "show variables like '%log_bin%';"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------------+--------------------------------+
| Variable_name                   | Value                          |
+---------------------------------+--------------------------------+
| log_bin                         | ON                             |
| log_bin_basename                | /var/lib/mysql/mysql-bin       |
| log_bin_index                   | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF                            |
| log_bin_use_v1_row_events       | OFF                            |
| sql_log_bin                     | ON                             |
+---------------------------------+--------------------------------+
  • SQL 모드
show variables like '%log_bin%';

Slave - Master 연결하기 위한 설정(리플리케이션 설정)

STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO MASTER_HOST='192.168.0.100',MASTER_USER='repl', MASTER_PASSWORD='Repl11@@@', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=1285;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.100',MASTER_USER='repl', MASTER_PASSWORD='Repl11@@@', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

Replication 시작

START SLAVE;

슬레이브 서버 상태 확인

  • Master_Host: 192.168.00.100
  • Master_User: repl
  • Master_Port: 3306
  • Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  • Slave_IO_Running: Yes
  • Slave_SQL_Running: Yes
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)


mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1285
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 1451
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          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: 1285
              Relay_Log_Space: 1658
              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: e7a4c0da-0661-11ec-a549-000c2986d295
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

리플리케이션 상태 확인

master 서버

mysql -uroot -p'password1!' -e "show full processlist\G;"
$ mysql -uroot -p'password1!' -e "show full processlist\G;"
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
     Id: 5
   User: repl
   Host: sdb1:34422
     db: NULL
Command: Binlog Dump
   Time: 5187
  State: Master has sent all binlog to slave; waiting for more updates
   Info: NULL
*************************** 2. row ***************************
     Id: 6
   User: repl
   Host: sdb2:33172
     db: NULL
Command: Binlog Dump
   Time: 5185
  State: Master has sent all binlog to slave; waiting for more updates
   Info: NULL
*************************** 3. row ***************************
     Id: 11
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: starting
   Info: show full processlist

slave 서버

mysql -uroot -p'password1!' -e "show full processlist\G;"
$ mysql -uroot -p'password1!' -e "show full processlist\G;"
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
     Id: 5
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 5086
  State: Waiting for master to send event
   Info: NULL
*************************** 2. row ***************************
     Id: 6
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 3159
  State: Slave has read all relay log; waiting for more updates
   Info: NULL
*************************** 3. row ***************************
     Id: 7
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: starting
   Info: show full processlist

디비 생성 테스트

[마스터 설정]

#마스터 상태 확인
mysql> SHOW MASTER STATUS;


# 마스터 로그 리셋
mysql> RESET MASTER;


mysql> show processlist;


# 테스트(wwwdb) 디비 생성
mysql> CREATE DATABASE wwwdb default CHARACTER SET UTF8;
mysql> create table t_www (id int);
mysql> insert into t_www values (1);
mysql> insert into t_www values (2);
mysql> insert into t_www values (3);


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wwwdb              |
+--------------------+
5 rows in set (0.00 sec)

[슬레이브 설정]

mysql> STOP SLAVE;


mysql> RESET SLAVE;


mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='repl', MASTER_PASSWORD='Repl11@@@', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=1285;


mysql> START SLAVE;


mysql> SHOW SLAVE STATUS\G;


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wwwdb              |
+--------------------+
5 rows in set (0.00 sec)

 

728x90
반응형