MySql Replication配置

來源:互聯網
上載者:User

標籤:

一、前言

Mysql Replication作為讀寫分離的廉價解決方案,支援一主多備的方式進行資料存放區,採用二進位記錄傳送,目前存在著廣泛應用,網上相關概念也比較多,不再重複介紹。引用一張官方提供的Replication應用情境圖片(http://dev.mysql.com/doc/refman/5.1/en/replication-solutions.html)。本文主要記錄了基於Linux環境下的Mysql Replication配置步驟。

 

二、環境準備
  IP_PORT OS Mysql
Master 10.129.221.14:3306 CentOS 6.5 5.1.73
Slave 10.129.221.16:3306 CentOS 6.5 5.1.73
Slave ...    

 

三、在Master和Slave上分別開啟root使用者遠端存取,並設定root使用者密碼,預設為空白
mysql> use mysql;mysql> update user set host = ‘%‘ where host =‘127.0.0.1‘ and user = ‘root‘;mysql> update user set password = PASSWORD(‘glf_123‘) where user = ‘root‘;mysql> flush privileges;

 

四、在Master資料庫上建立一個repl使用者並進行授權,用於slave機器訪問master資料庫
mysql> create user ‘repl‘@‘%‘ identified by ‘glf_123‘;mysql> grant replication slave on *.* to ‘repl‘@‘%‘ identified by ‘glf_123‘;mysql> flush privileges;

修改後的user表資料如下:

 

五、在Master伺服器上建立一個TestDB資料庫,用於Replication

 

六、修改Master資料庫的資料庫配置(/etc/my.cnf),開啟資料庫二進位日誌記錄
[mysqld]server-id=1                # 唯一IDlog-bin=mysql-bin          # 指定記錄檔binlog-do-db=TestDB        # 需要同步的資料庫datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid

 

七、重啟Master資料庫
[[email protected] /]# service mysqld restar

 

八、檢查Master庫的Master狀態
mysql> SHOW MASTER STATUS;

顯示結果如下:

 

九、修改Slave資料庫配置(/etc/my.cnf)
[mysqld]server-id=2     # 唯一IDread_only=1     # 設定Slave庫唯讀datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql # Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid

 

十、重啟Slave資料庫
[[email protected] /]# service mysqld restart

 

十一、設定slave節點replication指向master,在Slave庫執行change master command
change master to  master_host=‘10.129.221.14‘,master_user=‘repl‘,  master_password=‘glf_123‘,master_port=3306,  master_log_file=‘mysql-bin.000001‘,master_log_pos=0,  master_connect_retry=10;

master_log_file表示Master資料庫中的log檔案名稱,通過SHOW MASTER STATUS查詢;

master_log_pos 表示需要同步的二進位位移量 ,0表示全部重新同步;

第八步中SHOW MASTER STATUS查詢顯示的是當前Position;

 

十二、在Salve伺服器啟用slave模式
mysql> start slave;

 

十三、查看replication的執行情況、以及相關處理Process
mysql> SHOW SLAVE STATUS \G;mysql> show processlist \G;

 

十四、至此MySQL Replication 相關的配置完成,在Master庫的任何修改都會通過交易記錄提交到Slave。

 

MySql Replication配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.