mysql 主從實現

來源:互聯網
上載者:User

標籤:

主庫:192.168.1.19

從庫:192.168.1.20

開啟db_test單庫複製

常見問題

參考文檔

主配置 
  • 以mysql root使用者登入,用下面sql建立專門用於主從複製的mysql使用者repl,並授權給從庫20,密碼是mysql

grant replication slave on *.* to ‘repl‘@‘192.168.1.19‘ identified by ‘mysql‘;

flush privileges;

  • 再修改mysql設定檔,開啟主庫binlog輸出

cd /opt/svr/mariadb5/etc

vi my.cnf

開啟binlog某些配置項,即開啟主庫的寫binlog配置,調整過的參數如下:

log-bin=/opt/dbdat/mariadb5_data3306/log/mysql-bin #binlog存放路徑
log-bin-index=/opt/dbdat/mariadb5_data3306/log/mysql-bin.index 
binlog-do-db = db_test #只開啟db_test庫的複製,其他庫不複製
binlog-format=row #行模式複製

  • 儲存並重啟mysql,查看下面目錄已產生binlog檔案:

/opt/dbdat/mariadb5_data3306/log/mysql-bin

mysql-bin.000001 mysql-bin.index

  • 也可以通過下面sql查詢主庫複製狀態

show master status;

從配置
  • 串連主庫

通過下面sql串連主庫,注意其中的使用者密碼,以及binlog檔案名稱、讀binlog的位置(可通過上面show master status;sql查詢master最新位置)

執行sql:change master to master_host=‘192.168.1.19‘, master_user=‘repl‘, master_password=‘mysql‘, master_log_file=‘mysql-bin.000001‘, master_log_pos=0;

  • 開啟slave複製

執行sql: slave start

  • 查看slave狀態

執行sql:show slave status \G;

  • 查看slave配置參數

執行sql:show variables like "%slave%";

常見問題
  • slave不穩定,容易失效,經常要手工啟動
    測試過程中,需要先執行下面sql串連主庫
    slave stop;
    change master to master_log_file=‘mysql-bin.000005‘, master_log_pos=83773;
    slave start;

  • 發現下面一些情況需要手工啟動slave
    在主庫、備庫drop某些表
    當主庫有某張表,但從庫沒有,通過java插入、查詢該表時,也會造成slave停止
    當master binlog postion與slave 讀取的位置不一致時,要重新按照新的位置串連master,並重啟slave
  • 參考文檔
  • http://www.examw.com/linux/all/186202/index-3.html
  • http://blog.longwin.com.tw/2008/12/mysql-replication-slave-ignore-error-msg-2008/
  • http://shanchao7932297.blog.163.com/blog/static/13636242011284192652/
  • http://os.51cto.com/art/201311/418982.htm

 

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.