MySQL主從複製配置

1.  環境html

操做系統:CentOS-7mysql

MySQL:mysql-5.6sql

一臺虛擬機又克隆了兩臺數據庫

192.168.102.31  masteride

192.168.102.56  slavespa

192.168.102.36  slave操作系統

啓動/中止rest

service  mysqld  start|stop|restart
systemctl  start|stop|restart  mysqld

本機的話,直接mysql就能夠進去了

2.  主數據庫配置code

第1步:編輯/etc/my.cnf文件,在[mysqld]下增長以下兩行設置:server

[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

http://www.javashuo.com/article/p-gzsohdrv-bw.html

http://www.javashuo.com/article/p-wudtmrxb-ed.html

相關文章
相關標籤/搜索