標籤:主從複製 雙主模型
一、主從伺服器的IP地址
Master IP:172.16.1.1
Salve IP:172.16.1.64
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/49/F6/wKioL1Qf6-Twg6x9AAKZqVU98Co146.jpg" title="拓撲圖.png" alt="wKioL1Qf6-Twg6x9AAKZqVU98Co146.jpg" />
二、安裝mysql並配置好Master與Slave
1、安裝musql伺服器
#yum install mysql-server -y
2、建立MySQL資料存放位置
mkdir /mydata/data -pv
mkdir /mydata/binlogs -pv
mkdir /mydata/reaylogs -pv (從伺服器上建立)
3、修改目錄的屬主、屬組
chown -R mysql.mysql /mydata/*
Mster Server:
#vim /etc/mysql/my.cnf
datadir = /mydata/data
log-bin=/mydata/binlogs/master-bin
Salve Server:
vim /etc/mysql/my.cnf
datadir = /mydata/data
relay-log = /mydata/reaylogs/relay-log
#log-bin=mysql-bin
#binlog_format=mixed
server-id = 11
***主從伺服器的Server-id的值不同
三、為Slave建立串連Master的帳號:
MariaDB [(none)]> grant replication slave, replication client on *.* to [email protected]‘172.16.%.%‘ identified by ‘mageedu‘;
MariaDB [(none)]> flush privileges;
查看主節點的status資訊:
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000007 | 245 | | |
+-------------------+----------+--------------+------------------+
四、在Salev上串連Master:
1、串連主伺服器時如果報:
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.16.1.1‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘mageedu‘,MASTER_LOG_FILE=‘master-log.000007‘,MASTER_LOG_POS=245;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log
解決方案:
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.16.1.1‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘mageedu‘,MASTER_LOG_FILE=‘master-log.000007‘,MASTER_LOG_POS=245;
查看從節點狀態:
MariaDB [(none)]> show slave status\G
2、啟動IO_THREAD、SQL_THREAD線程:
MariaDB [(none)]> START SLAVE IO_THREAD;
MariaDB [(none)]> START SLAVE SQL_THREAD;
或者使用
MariaDB [(none)]>START SLAVE;
3、再次查看從節點狀態:
MariaDB [(none)]> SHOW SLAVE STATUS\G
對比啟動SLAVE前後的區別
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/49/F5/wKioL1Qf6e7BFMrgAAI5GjHr6wI341.jpg" style="float:none;" title="noSlave.png" alt="wKioL1Qf6e7BFMrgAAI5GjHr6wI341.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/49/F4/wKiom1Qf6c_x_LmoAAKJynOd4Wg996.jpg" style="float:none;" title="startSlave.png" alt="wKiom1Qf6c_x_LmoAAKJynOd4Wg996.jpg" />
在主節點上建立資料庫
MariaDB [(none)]>create database NAME;
在從節點查看:
MariaDB [(none)]>show databases;
#################################################################
###############補充部分############################################
1、停止從節點複製功能方法:
MariaDB [(none)]> stop slave;
2、如從伺服器中途加入,則需要從Master匯入資料
在Master上:#mysqldump --all-databases --lock-all-tables --routines --triggers --master-data=2 --flush-logs > /tmp/all.sql
在備份的資料匯入到Slave上:#scp /tmp/all/sql [email protected]:/tmp
匯入備份檔案後再Slave上進行如下操作:
#mysql < /tmp/all.sql
3、如何限制從伺服器唯讀?
MariaDB [mysql]> SET GLOBAL read_only = 1;
*************************
[mysqld]
read_only = 1
4、阻止所有使用者執行寫操作:
MariaDB [mysql]> flush tables with read lock;
5、如何保證主從複製時的事務安全?
前提:mysql對二進位日誌事件數目據會有緩衝;
在master上設定如下參數:
sync_binlog = 1
本文出自 “菜鳥前行之路” 部落格,謝絕轉載!
MySQL的主從複製和雙主模型