MySQL主從複製

來源:互聯網
上載者:User

標籤:mysql主從

  1. 兩台機器,192.168.162.128(主) 192.168.162.130(從)

  2. 配置主的設定檔

    vi /etc/my.cnf

    650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/8F/D0/wKiom1jsyRqCIwWwAAC8YJawgOg672.png-wh_500x0-wm_3-wmp_4-s_666601601.png" title="1.png" alt="wKiom1jsyRqCIwWwAAC8YJawgOg672.png-wh_50" />

  3. 建立複製使用者並授權給從伺服器

    grant replication slave on *.* to ‘rpl‘@‘192.168.162.130‘ identified by ‘123456‘;


  4. 重啟主伺服器的MySQL  /etc/init.d/mysqld restart

  5. 查看master資訊: 進入MySQL  show master status \G;

      650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8F/CE/wKioL1jsy7WQyTeJAAA_n7oCZOc906.png-wh_500x0-wm_3-wmp_4-s_3047961795.png" title="1.png" alt="wKioL1jsy7WQyTeJAAA_n7oCZOc906.png-wh_50" />

  6. 修改從庫設定檔

    vi /etc/my.cnf

    650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8F/D0/wKiom1jsyr3TeZCPAABWe1BNPbI928.png-wh_500x0-wm_3-wmp_4-s_756287969.png" title="1.png" alt="wKiom1jsyr3TeZCPAABWe1BNPbI928.png-wh_50" />

  7. 重啟從伺服器MySQL,進入MySQL

  8. 指定主伺服器IP等資訊

     change master to master_host=‘192.168.162.128 ‘,master_user=‘rpl‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=647;

  9. 啟動slave  :start slave;

  10. 查看slave狀態:show slave status \G;

   報錯Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/8F/CF/wKioL1jszITztAIvAACPCC4KGGY151.png-wh_500x0-wm_3-wmp_4-s_3169590262.png" title="1.png" alt="wKioL1jszITztAIvAACPCC4KGGY151.png-wh_50" />

意思是主和從必須都要有不同的uuid,我們分別去查看下主從的uuid

主:server-uuid=4e6c5a78-1e9a-11e7-8c65-000c29426312

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M02/8F/D0/wKiom1jszQDhD3ZMAACB80HbAQM050.png-wh_500x0-wm_3-wmp_4-s_1275673249.png" title="1.png" alt="wKiom1jszQDhD3ZMAACB80HbAQM050.png-wh_50" />

查看從庫uuid  從庫uuid:server-uuid=4e6c5a78-1e9a-11e7-8c65-000c29426312

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M01/8F/CF/wKioL1jszVbBKhnwAABdgcFp-SQ600.png-wh_500x0-wm_3-wmp_4-s_1416409111.png" title="1.png" alt="wKioL1jszVbBKhnwAABdgcFp-SQ600.png-wh_50" />

可以看出是真的一樣,原因在於 我是先安裝了一個虛擬機器並裝好MySQL,後面有複製了一遍,作為從伺服器,所以導致了這個UUID是一樣的。

接下來就是解決這個UUID問題,將從庫上的auto.cnf 備份下。然後重啟從庫。再看下從庫的UUID

[auto]

server-uuid=2e9c834a-1eb2-11e7-864b-000c2928472e

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/8F/CF/wKioL1jszgSCYwyPAAAPrLrnlso141.png-wh_500x0-wm_3-wmp_4-s_2479968937.png" title="1.png" alt="wKioL1jszgSCYwyPAAAPrLrnlso141.png-wh_50" />

再次查看從庫狀態

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/8F/D0/wKiom1jszkOCkwgBAACI2rriH40927.png-wh_500x0-wm_3-wmp_4-s_1192738443.png" title="1.png" alt="wKiom1jszkOCkwgBAACI2rriH40927.png-wh_50" />

從庫狀態正常了

11.測試主從是否正常

  主庫上建立一個測試庫,test :create database test;

  建立測試表:

create table test.test(

id int,

username varchar(10)

)ENGINE=‘innodb‘

12.查看從庫: 

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/8F/D0/wKiom1jszt-TnxS5AAAbyuhAsiA911.png-wh_500x0-wm_3-wmp_4-s_1246727120.png" title="1.png" alt="wKiom1jszt-TnxS5AAAbyuhAsiA911.png-wh_50" />

正常,說明主從搭建成功


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.