[mysql]두서버간에 replication 하기

일주일간의 삽질로 완성하다. 부디 다른분은 삽질하지 마시라고..

## mysql로 replication 하기 2004.1.27 by bluesoul@news.hani.co.kr

+ 환경
master 서버 : 리눅스 2.4.18, mysql 4.0.13
slave 서버 : 리눅스 2.4.20 레드햇9.0, mysql 4.0.17

+ master 서버 셋팅
1. 마스터에서 replication을 위한 사용자를 추가한다
mysql> grant file on *.* to repl@”%” identified by “패스워드” ;
repl대신 자신이 원하는 아이디 사용해도 된다.
% 대신 해당 아이피를 써도 된다.

주의)repl 계정의 권한에서 Repl_slave_priv, Repl_client_priv 가 yes로 되어있어야 한다. 확인하려면 mysql디비의 select * from user 에서

2. /etc/my.cnf 파일을 카피하고 수정한다.
mysql을 처음 설치하면 support-files/ 디렉토리 밑에
my-huge.cnf(1G~2G), my-large.cnf(512M), my-medium.cnf(64~256M), my-small.cnf(64M)
중에 메모리 크기에 맞게 한가지를 카피한다.

my.cnf 에서 아래와 같이 해준다.

# Replication Master Server (default)
# binary logging is required for replication
log-bin
binlog-do-db=test //복제할 디비명

# required unique id between 1 and 2^32 – 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1 //unique 한 값이어야 한다.

3. slave서버와 동기화 하기
– 테이블 락을 걸어서 쓰기 방지를 해놓고 데이터 옮긴후 풀어준다.
물론 디비서버 죽이고 해도 되면 더 좋다.
mysql>lock tables koko read;
mysql>unlock tables;

– dump해서 똑같은 디비명으로 생성하여 로드해도 된다.
테스트디비만 백업
# ./mysqldump -uroot -p패스워드 test > test.sql

복구
# ./mysql -uroot -p패스워드 test < test.sql

- 자신이 설치한 mysql 데이터 디렉토리에 가면 해당 디비이름의 디렉토리가
있다. 거기에 있는 파일을 tar로 묶어서 slave에 데이터있는 디렉토리에 마찬가지로 풀어놓기만
하면 된다.

4. master 서버의 mysql 재시작

# /etc/rc.d/init.d/mysql.server stop
# /etc/rc.d/init.d/mysql.server start

mysql>show master status;
+—————+———-+————–+——————+
| File | Position | Binlog_do_db | Binlog_ignore_db |
+—————+———-+————–+——————+
| peace-bin.007 | 159 | test | |
+—————+———-+————–+——————+

peace-bin.007과 159 를 기억해두자 slave 서버에 셋팅할때 쓴다.

+ slave 서버 셋팅

1. my.cnf 설정
server-id = 2 //master 서버(server-id =1)와 다른값
master-host = 211.233.22.187 //master 서버 아이피
master-user = 아이디 //리플리케이션할 계정아이디
master-password = 패스워드
master-port = 3306
slave-skip-errors=all
replicate-do-db = test //리플리케이션할 디비명

2. 이렇게 설정하고 mysql을 재가동하면 된다.

3. 문제가 있을경우 데이터 디렉토리에 보면 호스트이름.err에 로그가 남는다.
확인해서 수정한후 재가동하자

040127 16:57:08 Slave I/O thread: connected to master ‘repl@211.233.22.187:3306′,
replication started in log ‘peace-bin.007′ at position 79
위와 같은 문장이 있으면 성공한것이다.

– 데이터 디렉토리에 master.info master 관련 정보가 있다. 직접 수정하여도 되고
아래와 같이 sql문으로 수정할수도 있다.
mysql>change master to
>master_host=’host_string’
>master_user=’user_name’
>master_port=prot_no
>master_password=’password’
>master_log_file=’log_file_name’
>master_log_pos=pos_no;

요문장을 수행하기전에
mysql>slave stop; 하고 나서 하자

시작은
mysql>slave start;

4. 확인
mysql> show slave statusG // 그냥 ; 로 끝나면 보기 어렵게 나온다. G 옵션 뒤에 ; 빼고 해보자
아래와 같이 나온다.

*************************** 1. row ***************************
Master_Host: 211.233.22.187
Master_User: repl
Master_Port: 3306
Connect_retry: 60
Master_Log_File: peace-bin.007
Read_Master_Log_Pos: 159
Relay_Log_File: bluesoul2-relay-bin.002
Relay_Log_Pos: 84
Relay_Master_Log_File: peace-bin.007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_do_db: test
Replicate_ignore_db:
Last_errno: 0
Last_error:
Skip_counter: 0
Exec_master_log_pos: 159
Relay_log_space: 84
1 row in set (0.00 sec)

여기서 Read_Master_Log_Pos: 159 요 값이 아까 master서버에서 show master status할때 나온값가 같아야 한다.

아래와 같이 나오면 정상이다.
mysql> show processlistG
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 3635
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 1913
State: Has read all relay log; waiting for the I/O slave thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 4
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
3 rows in set (0.00 sec)

+ 진짜 확인
master 서버에서 업데이트, 인서트, 딜리트해보자.. slave서버에 반영된걸 확인할수 있다.

참고로 create구문은 리플리케이션 안된다.
마스터에서 디비 크리에이트하면 슬레이브에 반영안된다는 뜻이다.

Comments

Powered by Facebook Comments

댓글 남기기

당신의 이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

*

다음의 HTML 태그와 속성을 사용할 수 있습니다: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>