1. 環境
操作系統:CentOS-7
MySQL:mysql-5.6
一台虛擬機又克隆了兩台
192.168.102.31 master
192.168.102.56 slave
192.168.102.36 slave
啟動/停止
service mysqld start|stop|restart systemctl start|stop|restart mysqld
本機的話,直接mysql就可以進去了
2. 主數據庫配置
第1步:編輯/etc/my.cnf文件,在[mysqld]下增加如下兩行設置:
[mysqld]
log-bin=mysql-bin # 非必需
server-id=1 # 必需
第2步:創建用於數據同步的賬戶
CREATE USER 'repl'@'192.168.102.%' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.102.%'; FLUSH PRIVILEGES;
第3步:查看master狀態
show master status;
3. 從數據庫配置
第1步:編輯/etc/my.cnf文件,設置server-id
[mysqld]
server-id=2
第2步:執行同步語句,並啟動slave
change master to master_host='192.168.102.31', master_user='repl', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=514;
第3步:查看slave狀態
show slave status\G;
另外一台從數據庫也是這樣設置
4. 驗證是否同步成功
在主數據上操作,從數據庫中查看
5. 設置只讀賬戶
mysql> create user 'pig'@'%' identified by '123456'; mysql> grant select on test.* to 'pig'@'%'; mysql> flush privileges;
6. 參考
https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html
http://www.cnblogs.com/gl-developer/p/6170423.html