環境說明:
一、兩臺虛擬服務器
windows2008 R2,mysql 5.5
IP1:192.168.95.11(主庫); IP2:192.168.95.12(從庫)
二、主從複製
前提:
1.主從數據庫名稱必須一致,否則影響讀寫分離;
2.主庫的數據新建一個備份,然後還原到從庫;保證主從數據庫一致;
開始構建主從複製:
1.在11機器上創建一個12主機可以登陸的賬號
mysql>GRANT REPLICATION SLAVE ON *.* TO ‘mysql12’@’192.168.95.12’ IDENTIFIED BY ‘mysql12’;
mysql>FLUSH PRIVILEGES;
2.在mysql配置文件my.ini中添加:
--192.168.95.11
[mysqld]
log-bin=mysql-bin //開啓二進制
server-id=11 #任意自然數n,只要保證兩臺MySQL主機不重複就可以了。
auto_increment_increment=2 #步進值auto_imcrement。一般有n臺主MySQL就填n
auto_increment_offset=1 #起始值。一般填第n臺主MySQL。此時爲第一臺主MySQL
binlog-ignore=mysql #忽略mysql庫【我一般都不寫】
binlog-ignore=information_schema #忽略information_schema庫【我一般都不寫】
binlog-do-db=ceshi_test #要同步的數據庫,默認所有庫
--192.168.95.12
server-id=12
log-bin=mysql-bin
auto_increment_increment=2
auto_increment_offset=2
binlog-do-db=aa
重啓mysql服務
3.查看11主機文件名和位置
mysql>SHOW MASTER STATUS;
4.在12主機的my.ini文件配置
[mysqld]
serverid=2
log-bin=mysql-bin
重啓mysql服務器
5.在12主機執行
mysql>change master to master_host='192.168.95.11' >master_user='user12',master_password='user12',master_log_file='mysql-bin.000048',master_log_pos=432;
完成主從配置;
測試主從複製;
在12主機中
mysql>start slave;//開啓主從複製
mysql>show slave status\G //查看主從複製是否成功;
當看到Slave_IO_Running:YES,Slave_SQL_Running=YES;表明狀態正常;
主主配置:
1.在12主機上創建11主機可以登陸的用戶
mysql>GRANT REPLICATION SLAVE ON *.* TO ‘mysql11’@’192.168.95.11’ IDENTIFIED BY ‘mysql11’;
mysql>FLUSH PRIVILEGES;
2.在12上查看二進制名稱和位置
mysql>show master status;
3.在12主機執行
mysql>CHANGE MASTER TO
MASTER_HOST=’192.168.95.12’,
MASTER_USER=’user11’,
MASTER_PASSWORD=’mysql11’,
MASTER_LOG_FILE=’mysql-bin.000084’,
MASTER_LOG_POS=107;
主主配置完畢;