1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/etc/my
.cnf [root@edu-mysql-01 ~]
# vi /etc/my.cnf
# 在 [mysqld] 中增長如下配置項
# 設置 server_id,通常設置爲 IP
server_id=57
# 複製過濾:須要備份的數據庫,輸出 binlog
binlog-
do
-db=mbank
# 複製過濾:不須要備份的數據庫,不輸出(mysql 庫通常不一樣步)
binlog-ignore-db=mysql
# 開啓二進制日誌功能,能夠隨便取,最好有含義
log-bin=edu-mysql-bin
# 爲每一個 session 分配的內存,在事務過程當中用來存儲二進制日誌的緩存
binlog_cache_size=1M
# 主從複製的格式(mixed,statement,row,默認格式是 statement)
binlog_format=mixed
# 二進制日誌自動刪除/過時的天數。默認值爲 0,表示不自動刪除。
expire_logs_days=7
# 跳過主從複製中遇到的全部錯誤或指定類型的錯誤,避免 slave 端複製中斷。
# 如:1062 錯誤是指一些主鍵重複,1032 錯誤是由於主從數據庫數據不一 致
slave_skip_errors=1062
# 若是須要同步函數或者存儲過程
log_bin_trust_function_creators=
true
|
(如想了解以上參數的更多詳細解析,能夠直接百度參數名)html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@edu-mysql-01 ~]
# service mysql restart
Shutting down MySQL..[ OK ] Starting MySQL..[ OK ]
[root@edu-mysql-01 ~]
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection
id
is 1
Server version: 5.6.26-log Source distribution
Copyright (c) 2000, 2015, Oracle and
/or
its affiliates. All rights reser ved.
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 stat ement.
#建立數據同步用戶,並授予相應的權限
mysql> grant replication slave, replication client on *.* to
'repl'
@
'192. 168.31.59'
identified by
'123456'
;
Query OK, 0 rows affected (0.00 sec)
# 刷新受權表信息
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 查看 MySQL 如今有哪些用戶及對應的 IP 權限
mysql>
select
user,host from mysql.user;
# 查看 position 號,記下 position 號(從機上須要用到這個 position 號和現 在的日誌文件)
mysql> show master status;
Master 重啓後會修改 mysql-bin 文件名(序號加 1)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[root@edu-mysql-02 ~]
# vi /etc/my.cnf
## 在 [mysqld] 中增長如下配置項
# 設置 server_id,通常設置爲 IP
server_id=59
# 複製過濾:須要備份的數據庫,輸出 binlog
binlog-
do
-db=mbank
#複製過濾:不須要備份的數據庫,不輸出(mysql 庫通常不一樣步)
binlog-ignore-db=mysql
# 開啓二進制日誌,以備 Slave 做爲其它 Slave 的 Master 時使用
log-bin=edu-mysql-slave1-bin
# 爲每一個 session 分配的內存,在事務過程當中用來存儲二進制日誌的緩存
binlog_cache_size = 1M
# 主從複製的格式(mixed,statement,row,默認格式是 statement)
binlog_format=mixed
# 二進制日誌自動刪除/過時的天數。默認值爲 0,表示不自動刪除。
expire_logs_days=7
# 跳過主從複製中遇到的全部錯誤或指定類型的錯誤,避免 slave 端複製中 斷。
# 如:1062 錯誤是指一些主鍵重複,1032 錯誤是由於主從數據庫數據不一 致
slave_skip_errors=1062
# relay_log 配置中繼日誌
relay_log=edu-mysql-relay-bin
# log_slave_updates 表示 slave 將複製事件寫進本身的二進制日誌
log_slave_updates=1
# 防止改變數據(除了特殊的線程)
read_only=1
|
若是 Slave 爲其它 Slave 的 Master 時,必須設置 bin_log。在這裏,咱們 開啓了二進制日誌,並且顯式的命名(默認名稱爲 hostname,可是,若是 h ostname 改變則會出現問題)。mysql
1
2
|
[root@edu-mysql-02 ~]
# service mysql restart
Shutting down MySQL..[ OK ] Starting MySQL..[ OK ]
|
1
2
3
4
5
6
7
8
9
|
[root@edu-mysql-02 ~]
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection
id
is 3
Server version: 5.6.26-log Source distribution
Copyright (c) 2000, 2015, Oracle and
/or
its affiliates. All rights rese rved.
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 stat ement.
mysql> change master to master_host=
'192.168.31.57'
, master_user=
' repl'
, master_password=
'123456'
, master_port=3306, master_log_file=
' edu-mysql-bin.000001'
, master_log_pos=429, master_connect_retry=3 0;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
|
1
|
mysql> show slave status\G;
|
1
2
|
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
|
1
|
mysql> show slave status\G;
|
1
|
mysql> show processlist\G;
|
1
|
mysql> INSERT INTO `testuser`(`usercode`,`username`) VALUES (`1`,
'同步測試 1'
),( `2`,
'同步測試 2'
);
|
1
2
3
4
|
mysql> reset slave;
mysql> change master to master_host=
'192.168.31.57'
, master_user=
'repl'
,
master_password=
'123456'
, master_port=3306,
master_log_file=
'edu-mysql-bin.00000x'
, master_log_pos=xx, master_connect_retry=30;
|
1
2
3
4
5
6
|
mysql> show variables like ‘server_id';
+—————+——-+
| Variable_name | Value |
+—————+——-+
| server_id | 3 |
+—————+——-+
|