搭建(192.168.1.10 -> 192.168.1.20)
1 master 上執行
阻塞 DML
flush tables with read lock;
記錄 File 和 Position
show master status;
mysqldump -u root -p --all-databases > /data/dbbak/db.dmp
如果此 master 同時亦為 slave
mysqldump -u root -p --all-databases --dump-slave > /data/dbbak/db.dmp
unlock tables;
scp /data/dbbak/db.dmp mysql@192.168.1.20:/data/dbbak
2 slave 上執行
如果備份產生於 slave
mysqladmin -u root -p shutdown
/usr/local/mysql/bin/mysqld_safe --user=mysql --skip-slave-start &
mysql -u root -p < /data/dbbak/db.dmp
3 master 上執行
[mysqld]
server-id = 110
log-bin = /data/mysql/mysql-bin
binlog_format = MIXED
skip-slave-start = 1
#auto_increment_offset = 1
#auto_increment_increment = 1
create user 'repl'@'192.168.1.20' identified by 'xxxxxxxx';
grant replication slave on *.* to 'repl'@'192.168.1.20';
注明: 這裡的IP指的從庫的IP,如主庫是192.168.1.10,這裡加的連線使用者是:192.168.1.20,不是1.10的IP。
4 slave 上執行
[mysqld]
server-id = 120
log-bin = /data/mysql/mysql-bin
binlog_format = MIXED
skip-slave-start = 1
relay-log = rep_relay_log
relay-log-index = rep_relay_log
read_only = 1
#sync_master_info = 0
#sync_relay_log = 0
#sync_relay_log_info = 0
#log-slave-updates = 1
#replicate-rewrite-db = cisdb -> newdb
#replicate-do-db = cisdb
#replicate-ignore-db = cisdb
#replicate-do-table = cisdb.t1
#replicate-ignore-table = cisdb.t1
#replicate-wild-do-table = cisdb.%
#replicate-wild-ignore-table = cisdb.%
#slave-skip-errors = xxxx,xxxx
#relay_log_purge
#relay_log_space_limit
#slave_exec_mode
#slave_compressed_protocol
#slave_parallel_workers
change master to
MASTER_HOST='192.168.60.202',
MASTER_USER='repl',
MASTER_PASSWORD='1',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120;
start slave;
5 salve 上執行
檢查 Slave_IO_Running 和 Slave_SQL_Running 是否為 Yes
檢查 Seconds_Behind_Master
show slave status\G;
檢查 Slave_SQL_Running 執行緒的 Time 值(延時,單位為秒)
show processlist\G;
為方便主從切換:
create user 'repl'@'192.168.1.10' identified by 'xxxxxxxx';
grant replication slave on *.* to 'repl'@'192.168.1.10';
注明: 這裡的IP指的從庫的IP,如從庫是192.168.1.20,切換後從庫就是1.10,這裡加的連線使用者是:192.168.1.10,部署1.20的IP。
新加的使用者是,從庫連線的IP,使用者名稱和密碼。
當使用者許可權中沒有SUPER許可權(ALL許可權是包括SUPER的)時,從庫的read-only生效!
使用start slave命令開啟失敗:
mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
使用reset slave命令解決:
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;