YOMEDIA
Cấu hình replicate database MySQL Master-to-master
Chia sẻ: Abcdef_43 Abcdef_43
| Ngày:
| Loại File: PDF
| Số trang:10
152
lượt xem
9
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Có nhiều kiểu cấu hình replicate, tùy từng trường hợp cụ thể mà sử dụng theo cách nào cho phù hợp. Ở đây trường hợp của tôi, bài tóan đặt ra như sau : Code+database website được đặt trên 2 server (1 trong nước, 1 nước ngoài ). Yêu cầu 2 database này phải được đồng bộ thường xuyên với nhau. Lí do tôi chọn cách cấu hình master to master là bởi thế, với cấu hình này thì 1 server vừa là master vừa là slave, tức nó vừa lắng nghe vừa ra hiệu cho server còn lại nếu...
AMBIENT/
Chủ đề:
Nội dung Text: Cấu hình replicate database MySQL Master-to-master
- Cấu hình replicate database MySQL Master-to-master.
Có nhiều kiểu cấu hình replicate, tùy t ừng
trường hợp cụ thể mà sử dụng theo cách
nào cho phù hợp.
Ở đây trường hợp của tôi, bài tóan đặt ra
như sau :
Code+database website được đặt trên 2
server (1 trong nước, 1 nước ngoài ). Yêu
cầu 2 database này phải được đồng bộ
thường xuyên với nhau.
Lí do tôi chọn cách cấu hình master to master là bởi thế, với cấu hình này thì 1
server vừa là master vừa là slave, tức nó vừa lắng nghe vừa ra hiệu cho server
còn lại nếu có sự thay đổi dữ liệu trong database.
Cả 2 server sử dụng hệ điều hành CentOs, database MySQL version 5.x
Server 1 có IP dạng : 192.168.1.10
Server 2 có IP dạng : 192.168.1.11
Đăng nhập vào chế độ dòng lệnh của MySQL:
$ mysql -u root -p
Thực thi câu lệnh t ương tự cho cả 2 server :
view plaincopy to clipboardprint?
1. GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTI
FIED BY '%slave_password%';
2. FLUSH PRIVILEGES;
3. quit;
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED
BY '%slave_password%';
FLUSH PRIVILEGES;
quit;
Trong đó replication là user,slave_password là m ật khẩu của user replication.
Edit file my.cnf c ủa server 1 :
view plaincopy to clipboardprint?
1. [mysqld]
- 2. datadir=/var/lib/mysql
3. socket=/var/lib/mysql/mysql.sock
4. #user=mysql
5. # Default to using old password format for compatibility with mysql 3.x
6. # clients (those using the mysqlclient10 compatibility package).
7. old_passwords=1
8. max_connections = 400
9. key_buffer = 16M
10. myisam_sort_buffer_size = 32M
11. join_buffer_size = 1M
12. read_buffer_size = 1M
13. sort_buffer_size = 2M
14. table_cache = 1024
15. thread_cache_size = 286
16. interactive_timeout = 25
17. wait_timeout = 1000
18. connect_timeout = 60
19. max_allowed_packet = 16M
20. max_connect_errors = 10
21. query_cache_limit = 1M
22. query_cache_size = 16M
23. query_cache_type = 1
24. tmp_table_size = 16M
25. skip-innodb
26.
27. log-bin=mysql-bin
28. binlog-do-db=replicate_test
29. binlog-ignore-db=mysql
30. binlog-ignore-db=test
31.
32. server-id=1
33.
34. master-host = 192.168.2.11
35. master-user = replication
36. master-password = 111111
37. master-port = 3306
38.
39. auto_increment_increment= 2
40. auto_increment_offset = 2
41.
- 42. slave-net-timeout = 30
43. master-connect-retry = 30
44.
45. [mysql.server]
46. user=mysql
47. basedir=/var/lib
48.
49. [mysqld_safe]
50. relay-log = relay-bin
51. log-error=/var/log/mysqld.log
52. pid-file=/var/run/mysqld/mysqld.pid
53. max_allowed_packet = 16M
54.
55. [myisamchk]
56. keybuffer = 32M
57. sort_buffer = 32M
58. read_buffer = 16M
59. write_buffer = 16M
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
max_connections = 400
key_buffer = 16M
myisam_sort_buffer_size = 32M
join_buffer_size = 1M
read_buffer_size = 1M
sort_buffer_size = 2M
table_cache = 1024
thread_cache_size = 286
interactive_timeout = 25
wait_timeout = 1000
connect_timeout = 60
max_allowed_packet = 16M
max_connect_errors = 10
query_cache_limit = 1M
- query_cache_size = 16M
query_cache_type = 1
tmp_table_size = 16M
skip-innodb
log-bin=mysql-bin
binlog-do-db=replicate_test
binlog-ignore-db=mysql
binlog-ignore-db=test
server-id=1
master-host = 192.168.2.11
master-user = replication
master-password = 111111
master-port = 3306
auto_increment_increment= 2
auto_increment_offset = 2
slave-net-timeout = 30
master-connect-retry = 30
[mysql.server]
user=mysql
basedir=/var/lib
[mysqld_safe]
relay-log = relay-bin
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
max_allowed_packet = 16M
[myisamchk]
keybuffer = 32M
sort_buffer = 32M
read_buffer = 16M
write_buffer = 16M
Edit file my.cnf c ủa server 2 :
- view plaincopy to clipboardprint?
1. [mysqld]
2. datadir=/var/lib/mysql
3. socket=/var/lib/mysql/mysql.sock
4. #user=mysql
5. # Default to using old password format for compatibility with mysql 3.x
6. # clients (those using the mysqlclient10 compatibility package).
7. old_passwords=1
8. max_connections = 400
9. key_buffer = 16M
10. myisam_sort_buffer_size = 32M
11. join_buffer_size = 1M
12. read_buffer_size = 1M
13. sort_buffer_size = 2M
14. table_cache = 1024
15. thread_cache_size = 286
16. interactive_timeout = 25
17. wait_timeout = 1000
18. connect_timeout = 60
19. max_allowed_packet = 16M
20. max_connect_errors = 10
21. query_cache_limit = 1M
22. query_cache_size = 16M
23. query_cache_type = 1
24. tmp_table_size = 16M
25. skip-innodb
26.
27. log-bin=mysql-bin
28. binlog-do-db=replicate_test
29. binlog-ignore-db=mysql
30. binlog-ignore-db=test
31.
32. server-id=2
33.
34. master-host = 192.168.2.10
35. master-user = replication
36. master-password = 111111
37. master-port = 3306
38.
- 39. auto_increment_increment= 2
40. auto_increment_offset = 2
41.
42. slave-net-timeout = 30
43. master-connect-retry = 30
44.
45. [mysql.server]
46. user=mysql
47. basedir=/var/lib
48.
49. [mysqld_safe]
50. relay-log = relay-bin
51. log-error=/var/log/mysqld.log
52. pid-file=/var/run/mysqld/mysqld.pid
53. max_allowed_packet = 16M
54.
55. [myisamchk]
56. keybuffer = 32M
57. sort_buffer = 32M
58. read_buffer = 16M
59. write_buffer = 16M
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility packa ge).
old_passwords=1
max_connections = 400
key_buffer = 16M
myisam_sort_buffer_size = 32M
join_buffer_size = 1M
read_buffer_size = 1M
sort_buffer_size = 2M
table_cache = 1024
thread_cache_size = 286
interactive_timeout = 25
wait_timeout = 1000
connect_timeout = 60
- max_allowed_packet = 16M
max_connect_errors = 10
query_cache_limit = 1M
query_cache_size = 16M
query_cache_type = 1
tmp_table_size = 16M
skip-innodb
log-bin=mysql-bin
binlog-do-db=replicate_test
binlog-ignore-db=mysql
binlog-ignore-db=test
server-id=2
master-host = 192.168.2.10
master-user = replication
master-password = 111111
master-port = 3306
auto_increment_increment= 2
auto_increment_offset = 2
slave-net-timeout = 30
master-connect-retry = 30
[mysql.server]
user=mysql
basedir=/var/lib
[mysqld_safe]
relay-log = relay-bin
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
max_allowed_packet = 16M
[myisamchk]
keybuffer = 32M
sort_buffer = 32M
read_buffer = 16M
- write_buffer = 16M
Bạn cần để ý các thông số sau ở cả 2 serv er :
view plaincopy to clipboardprint?
1. log-bin=mysql-bin
2. binlog-do-db=replicate_test
3. binlog-ignore-db=mysql
4. binlog-ignore-db=test
5.
6. server-id=2
7.
8. master-host = 192.168.2.10
9. master-user = replication
10. master-password = 111111
11. master-port = 3306
12.
13. auto_increment_increment= 2
14. auto_increme nt_offset = 2
log-bin=mysql-bin
binlog-do-db=replicate_test
binlog-ignore-db=mysql
binlog-ignore-db=test
server-id=2
master-host = 192.168.2.10
master-user = replication
master-password = 111111
master-port = 3306
auto_increment_increment= 2
auto_increment_offset = 2
Chú thích các thông số :
binlog-do-db=replicate_test : chỉ định các bảng sẽ được thực hiện replicate
binlog-ignore-db=mysql : Chỉ định các bảng sẽ không đ ược thực hiện replicate
server-id : Id chỉ định cho server.
- master-host : Địa chỉ ip của host master
master-user,master-password : user và password theo master-host
master-port: cổng truy vấn của database.
auto_increment_offset : cần đặc biệt chú ý thông số này, trường hợp bạn không
thiết lập thông số này database có trường autoincrement sẽ tăng theo giá trị +1
cho giá trị tiếp theo khi insert dữ liệu mới.
Để tránh trường hợp 2 database ở 2 server bị tr ùng giá trị autoincrement tôi đặt
giá trị này auto_increment_offset=1 cho server1 và =2 cho server 2 v ới mục
đích giá trị tự động tăng ở server1 là số lẻ , ở server 2 l à số chẵn.
Đăng nhập chế độ dòng lệnh MySQL và thực hiện việc gán master cho
server2:
view plaincopy to clipboardprint?
1. stop slave;
2. CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_
USER='replication', MASTER_PASSWORD='%slave_password%';
3. start slave;
stop slave;
CHANGE MASTER TO MASTER_HOST='192.168.1.10',
MASTER_USER='replication',
MASTER_PASSWORD='%slave_password%';
start slave;
Thực hiện câu lệnh t ương tự cho server 1 với MASTER_HOST là
192.168.1.11, user và password.
Kiểm tra sự hoạt động của cả 2 server :
view plaincopy to clipboardprint?
1. SHOW MASTER STATUS;
SHOW MASTER STATUS;
view plaincopy to clipboardprint?
1. SHOW SLAVE STATUS\G;
SHOW SLAVE STATUS\G;
- Nếu 2 option Slave_IO_Running and Slave_SQL_Running đều hiển thị Yes th ì
bạn đã thành công.
Nếu 1 trong 2 chưa thành công bạn cần kiểm tra lại user và password chính
xác, hoặc master-port đã được mở hay chưa.
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
ERROR:connection to 10.20.1.100:9315 failed (errno=111, msg=Connection refused)
ERROR:connection to 10.20.1.100:9315 failed (errno=111, msg=Connection refused)
Đang xử lý...