環境:CentOS 6.7 + MySQL 5.6.30
主節點:192.168.56.102
從節點:192.168.56.103
已經分別安裝好單機MySQL,現在配置兩台MySQL的主從復制。
官方文檔是分兩步進行的:
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON . TO 'repl'@'%.mydomain.com';
實際上一條命令即可:
grant replication slave,reload,super on *.* to 'repl'@'192.168.56.103' identified by 'asdfjkl';
vi /data/mysqldata/3306/my.cnf
添加內容,其實主要是server-id=102,另外對於InnoDB,官檔建議加兩個參數:
[mysqld]
server-id=102
#binlog-ignore-db = mysql
#replicate-do-db = xxdb
#InnoDB
innodb_flush_log_at_trx_commit=1
sync_binlog=1
主節點重啟MySQL:
shell> mysqladmin shutdown
shell> mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
vi /data/mysqldata/3306/my.cnf
添加:
[mysqld]
server-id=103
#replicate-do-db = xxdb
從節點重啟MySQL:
shell> mysqladmin shutdown
shell> mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
主節點上鎖:
mysql> FLUSH TABLES WITH READ LOCK;
主節點查看狀態:
(root@localhost)[(none)]> show master status
-> ;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000011 | 432 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
從節點配置:
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.56.102',
MASTER_USER='repl',
MASTER_PASSWORD='asdfjkl',
MASTER_LOG_FILE='mysql-bin.000011',
MASTER_LOG_POS=432;
這裡的日志文件和當前位置是根據上一步查到的主節點狀態填寫的。
啟動SLAVE:
mysql> START SLAVE;
查看從節點狀態:
mysql> show slave status \G
主節點解鎖:
mysql> UNLOCK TABLES;
主節點建庫:
mysql> create database xxdb;
從節點查看是否同步:
mysql> show databases;