mysql group replication절차입니다. 쉽게 말해서 원래 서버의 백업 서버를 만드는 것이라고 보시면 됩니다.
일단은 똑같은 가상환경을 복사해줍니다. 클론 만들면 여태까지 만든 환경을 똑같이 복제를 할 수 있죠. 가상머신의 장점이라고 할 수 있죠.
다음은 mysql에 접속합니다.
일단 repl이라고 리플리케이션 할 DB를 만듭시다.
create database repl;
그리고 유저도 만듭시다.
create user 'repl'@'192.168.211.143(여기는 마스터의 IP적어줍니다.)' identified by 'repl(패스워드)';
원래는 이렇게 입력하면 잘 되어야 하지만 아래와 같이 mysql을 업그레이드가 필요하다는 에러가 나오면 mysql을 종료하고 업그레이드를 해줍니다. 업그레이드가 완료되면 mysqld를 다시 시작해주세요
mysql_upgrade -u root -p
완료후
systemctl restart mysqld
그다음 다시 create user 'repl'@'192.168.211.143(여기는 마스터의 IP적어줍니다.)' identified by 'repl(패스워드)'; 입력해주면 쿼리가 OK가 나옵니다.
그다음은 권한을 줍시다.
grant all on repl.* to 'repl'@'192.168.211.143(마스터의 IP)';
그리고 설정 완료를 위해
flush privileges;
다 했으면 exit를 입력해서 나와줍니다.
커맨드 창으로 나오면
vi /etc/my.cnf
입력해줍니다.
그리고 맨 밑에 다음과 같이 적어줍니다.
server-id=1
binlog-do-db=repl
log-bin=mysql-bin
그리고 mysql을 재시작 해주시고 다시 접속합니다.
다음 여기서 slave설정을 해줍니다.
grant replication slave on *.* to 'repl'@'192.168.211.149(salve의 IP)' identified by 'repl';
ip적어 주는 부분에 꼭 slave의 IP를 적어줍니다. 여기서 한번 실수해서 다시 했네요.
그다음 flush privileges를 입력해줍니다.
그리고 slave에게 db설정을 보내주기 위해서 잠시 잠궈둡시다.
use repl;
flush tables with read lock;
다음 마스터의 상태를 확인합니다.
show master status;
여기서 확인 할 것은 position의 숫자입니다. 저는 603이 나왔군요.
그리고 repl.sql 파일을 만들어서 slave에게 전해줍시다.
mysqldump -u root -p repl > repl.sql
그리고 scp를 이용해서 파일은 전송합시다.
scp repl.sql root@192.168.211.149(slave의 ip주소):/root/
라고 하고 yes라고 입력하면 전송이 완료됩니다. 이로써 마스터 설정은 완료.
--------------------------------------------------------------------------------------------------------------------------------
다음은 slave에서 설정합니다.
일단 slave의 mysql도 업그레이드를 해줍니다. 위와 똑같습니다
mysql_upreade -u root -p
완료가 되었으면 mysql을 재시작 해줍니다
systemctl restart mysqld
다음 vi /etc/my.cnf로 들어갑니다.
맨 밑에 다음과 같이 추가해줍니다.
server-id=2
binlog-do-db=repl
log-bin=mysql-bin
그리고 mysql을 재시작해줍니다. 그리고 아까 마스터로부터 받아온 설정 파일을 적용합니다.
mysql -u root- p repl < repl.sql
그런데 slave에는 repl이라는 데이터베이스가 없으므로 만들어줘야 합니다.
create database repl;
하고 나와주고 다시 입력해주면 됩니다.
다음 slave에서 repl이라는 사용자를 만들어줍시다.
create user 'repl'@'192.168.211.149(slave의 IP주소)' identified by 'repl(패스워드)';
그리고 권한부여
grant all on repl.* to 'repl'@'192.168.211.149(slave의 IP주소)' ;
마지막으로 리플리케이션 설정
grant replication slave on *.* to 'repl'@'192.168.211.149(slave의 IP 주소)' identified by 'repl(패스워드)';
그리고 설정 완료
flush privileges;
그리고 마지막 대망의 마스터 호스트 등록하기.
change master to master_host='192.168.211.143(마스터의 IP주소)', master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=603(아까 포지션 넘버);
master PC에서 mysql접속후
unlock tables;
------------------------------------------------------------------------------------------------------------------
일단 이렇게 하고 SHOW SLAVE STATUS\G 라고 입력했을때 3가지 로그가 대표적으로 나옵니다. 빨간색으로 표시한 부분만 중점적으로 보시면 차이점을 알 수 있습니다.
일단 성공 로그
SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.211.143 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 154 Relay_Log_File: localhost-relay-bin.000004 Relay_Log_Pos: 367 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: 154 Relay_Log_Space: 744 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: 58e93fdc-a917-11e7-99c3-080027f72cdc 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) |
Slave_IO_State: Waiting for master to send event 라고 나오고 Last_IO_Error 가 공백입니다. 즉 데이터를 받기 위해서 대기중이라는것이죠.
--------------------------------------------------------------------------------------------------------------------------------------
다음 아래는 실패로그입니다. 참고로 저는 밑에 두개가 다 나왔더군요. 야 신난다..는 이것때문에 3~4번은 다시 했네요.
Last_IO_Error: error connecting to master 'repl@192.168.211.143:3306' - retry-time: 60 retries: 1 이런식의 오류 로그
mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.211.143 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 603 Relay_Log_File: localhost-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Connecting 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: 603 Relay_Log_Space: 154 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 2003 Last_IO_Error: error connecting to master 'repl@192.168.211.143:3306' - retry-time: 60 retries: 1 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: 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: 171005 00:06:36 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) |
Slave_IO_State: Connecting to master 라고 나오고
Last_IO_Error: error connecting to master 'repl@192.168.211.143:3306' - retry-time: 60 retries: 1 나오는 에러인 경우.
slave가 마스터에 연결을 진행중이고 열심히 재시도를 하는것입니다. 이 경우 가장 대표적인 해결책은 3306 방화벽을 열어줍니다.
마스터와 슬레이브 둘다 설정합니다. 일단 mysql을 종료하고 커맨드에서 3306 방화벽을 열어줍시다.
firewall-cmd --permanent --zone=public --add-port=3306/tcp
열어주고
systemctl restart firewalld
로 방화벽을 재시작해주고 mysqld에 접속해서 다시한번 SHOW SLAVE STATUS\G를 입력해줍니다.
만약에 방화벽도 열어줬는데 저 에러가 계속 나오면 위에 환경설정에서 마스터 IP입력할때 슬레이브 IP를 넣었거나 오타가 생겼나 등 설정시 오류이므로 다시 하셔야 합니다..
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. 식의 로그
mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.211.143 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 603 Relay_Log_File: localhost-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No 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: 603 Relay_Log_Space: 154 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 1593 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 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: 171005 00:11:14 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)
|
일단 위의 에러로그를 처리하면 위와 같은 에러 로그가 나오더군요.
Slave_IO_State: 는 공백이고
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. 라고 나옵니다.
이것은 마스터와 slave의 UUID가 같아서 생기는 오류입니다.
일단은 마스터와 슬레이브에서 show variables like '%uuid%'; 라고 입력해줍니다.
show variables like '%uuid%';
라고 입력했을때 value 인 UUID값이 같다. 그러면 해결책은 간단합니다.
auto.cnf라는 파일을 삭제해주면 됩니다. 마스터와 슬레이브 둘다 삭제해줍니다. 이 파일은 /var/lib/mysql/auto.cnf에 있습니다.
rm -rf /var/lib/mysql/auto.cnf
그리고 둘다 mysql을 재실행해줍시다.
systemctl restart mysqld
참고로 seLinuex와 같이 사용할려면 다음과 같이 하면됩니다.
semanage port -l | grep mysqld
이걸로 포트가 있는지 확인을 하고 없으면
semanage port -a -t mysqld_port_t -p tcp 3306.
추가하시면 됩니다.
이렇게 하면 mysql group replication설정 완료입니다.
'가상머신 환경 세팅' 카테고리의 다른 글
post body확인하기 (0) | 2017.12.08 |
---|---|
크로스 사이트 스크립팅 공격 (0) | 2017.12.06 |
openssl을 사용해서 https 페이지 만들기. (0) | 2017.10.07 |
워드프레스 가상호스트 설정하기 (0) | 2017.09.29 |
아파치 설치부터 워드프레스까지 (0) | 2017.09.27 |