MySQL主從複製(指令碼配合)

來源:互聯網
上載者:User

標籤:blank   伺服器   ffffff   target   color   

首先需要有兩台mysql伺服器,我是同過mysql的多執行個體來實現的主從複製,實驗中用的了指令碼,可以參考多執行個體環境的搭建

多執行個體環境搭建 http://aby028.blog.51cto.com/5371905/1892250

 

主:3306(master)

從:3307(slave)

 

保證 mysql 多執行個體的正常運行

netstat -tlunp | grep 330

650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8C/DE/wKioL1h8cXfBxAjyAAAYls69Kuo076.png" "818" height="78" />

 

查看 log-bin 和 server-id

egrep "log-bin|server-id" /data/{3306,3307}/my.cnf

/data/3306/my.cnf:log-bin = /data/3306/mysql-bin          # master開

/data/3306/my.cnf:server-id = 1

/data/3307/my.cnf:#log-bin = /data/3307/mysql-bin       # slave可以關閉

/data/3307/my.cnf:server-id = 3

 

若更改 mysql 的設定檔需要重啟資料庫

/data/3306/mysql stop

/data/3307/mysql stop

/data/3306/mysql start

/data/3307/mysql start

 

登陸主庫3306

mysql -uroot -p123456 -S /data/3306/mysql.sock

 

查看 mysql 的配置參數

mysql> show variables like "log_bin";

mysql> show variables like "server_id";

 

在主庫授權一個帳號,專門用於同步,然後重新整理許可權

grant replication slave on *.* to [email protected]‘%‘ identified by ‘123456‘;

flush privileges;

 

檢查剛才的操作

select user,host from mysql.user;

show grants for [email protected]‘%‘;

 

鎖表。鎖表後自己將無法建立資料庫

flush table with read lock;

 

備份打包 (需要另開一個視窗,保證之前的視窗不能退出)

mysqldump -uroot -p123456 --events -S /data/3306/mysql.sock -A -B | gzip > /opt/bak_$(date +%F).sql.gz

 

打包完成後,再次查看狀態,保證與之前的查詢狀態一樣,證明資料沒有寫入

show master status;

 

解鎖表

unlock tables;

 

將剛才備份好的 gzip 檔案匯入到 3307 資料庫  (全量備份)

cd /opt/

gzip -d bak_2017-01-06.sql.gz

mysql -uroot -p123456 -S /data/3307/mysql.sock < bak_2017-01-06.sql

 

增量備份,登陸從庫配置同步參數

CHANGE MASTER TO

MASTER_HOST=‘192.168.6.45‘,

MASTER_PORT=3306,

MASTER_USER=‘rep‘,

MASTER_PASSWORD=‘123456‘,

MASTER_LOG_FILE=‘mysql-bin.000001‘,

MASTER_LOG_POS=459;

 

在 salve 上執行 start slave 操作,IO 串連

start slave;

 

查看當前的狀態,關鍵有以下幾個內容

show slave status\G;

Slave_IO_State: Waiting for master to send event

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Seconds_Behind_Master: 0     #從庫落後主庫的秒數

MySQL主從複製(指令碼配合)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.