(1)規劃主從主機 主機:10.10.54.86 從機1:10.10.54.85 從機2:10.10.54.87 (2)更改主機配置文件 [root@nan86 ~]# vim /etc/my.cnf log-bin=master-bin binlog_format=mixed server-id=1 (3)更改從機配置文件 從1 [root@nan85 ~]# vim /etc/my.cnf log-bin=slave-bin binlog_format=mixed server-id=10 從2 [root@nan87 ~]# vim /etc/my.cnf log-bin=slave-bin binlog_format=mixed server-id=11 (4)在master上建立複製用戶,並授予權限 mysql> grant replication slave on *.* to 'emp1'@'10.10.54.85' identified by 'emp1'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to 'emp2'@'10.10.54.87' identified by 'emp2'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) (5)查看master上二進制日誌和position位置 mysql> show master status; +-------------------+-----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+-----------+--------------+------------------+ | master-bin.000014 | 189672455 | | | +-------------------+-----------+--------------+------------------+ 1 row in set (0.00 sec) (6)備份master上的數據,把備份master數據庫還原到從庫上 [root@nan86 tmp]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --flush-logs --database employees >emp.sql [root@nan86 tmp]# mysql -uroot -p123456 -h10.10.54.85<employees.sql [root@nan86 tmp]# mysql -uroot -p123 -h10.10.54.87<employees.sql (7)在slave上面change master操做 從1: mysql> change master to master_host='10.10.54.86',master_user='emp1', master_password='emp1', master_log_file='master-bin.000014',master_log_pos=189672455; Query OK, 0 rows affected (0.01 sec) 從2: mysql> change master to master_host='10.10.54.86',master_user='emp2',master_password='emp2',master_log_file='master-bin.000014',master_log_pos=189672455; Query OK, 0 rows affected (0.02 sec) (8)在slave上啓動slave 從1: mysql> slave start; Query OK, 0 rows affected (0.00 sec) 從2: mysql> slave start; Query OK, 0 rows affected (0.00 sec) (9)查看slave狀態 肯定slave上的I/O線程和SQL線程狀態爲YES 從1: mysql> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes 從2: mysql> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes (10)測試 主上: 建立一個數據庫 mysql> create database a; Query OK, 1 row affected (0.00 sec) 從1上: 同步建立一個數據庫a mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | a | | employees | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) 從2上: 同步建立一個數據庫a mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | a | | employees | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) 經測試在master上建立刪除增長數據,兩個slave上都能同步。