Mysql主從同步

來源:互聯網
上載者:User

標籤:mysql 同步 安全

主(Master)IP:192.168.1.101

從(Slave) IP:192.168.1.102


Master配置

1、首先登入資料庫,建立複製帳號

mysql -uroot -p

mysql>grant replication slave,reload,super on *.*to [email protected]’192.168.1.102’ identified by ‘123456’;

2、進入設定檔my.cnf中

vim /etc/mysql/mysql.conf.d/mysqld.cnf

在設定檔加入如下值

server-id=1  #用於標識該語句最初是從哪個server寫入的log-bin=/var/log/mysql/mysql-bin.log  #日誌路徑

3、重啟mysql伺服器

service mysqld restart 或 /etc/init.d/mysqld restart (用於Ubuntu系統)

4、進入mysql下查看主機狀態

show master status;

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8C/AC/wKiom1h0Pw7jvhf8AAAVgUys-1c399.png-wh_500x0-wm_3-wmp_4-s_924730556.png" title="1.png" alt="wKiom1h0Pw7jvhf8AAAVgUys-1c399.png-wh_50" />


Slave配置

1、進入設定檔my.cnf中

vim /etc/mysql/mysql.conf.d/mysqld.cnf

在設定檔中加入如下值

server_id         = 2relay_log         =mysql-relay-binreplicate-do-db    =genecrab_report                               //指定的資料庫名log_bin          =/var/log/mysql/mysql-bin.log

2、進入mysql中

mysql>CHANGE MASTER TO                       

        MASTER_HOST=’192.168.1.101’,       //Master IP地址

        MASTER_USER=‘mysqler‘,               //資料庫帳號

        MASTER_PASSWORD=’123456’,          //對應的帳號密碼

        MASTER_LOG_FILE=‘mysql-bin.000001‘,   

        MASTER_LOG_POS=184677;                // MASTER_LOG_POS的值為0,

注意

MASTER_LOG_FILE一定跟主中File 一致

MASTER_LOG_POS 一定跟主中Position一致

3、查看狀態

show slave status\G

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M02/8C/AC/wKiom1h0Py3w8appAACA1HUrh9o322.png-wh_500x0-wm_3-wmp_4-s_3847695559.png" title="2.png" alt="wKiom1h0Py3w8appAACA1HUrh9o322.png-wh_50" />

4、開啟同步

start slave

註:

1、主要是看:
                  Slave_IO_Running=Yes
                  Slave_SQL_Running=Yes

slave的I/O和SQL線程都已經開始運行,而且Seconds_Behind_Master不再是NULL。日誌的位置增加了,意味著一些事件被擷取並執行了。如果你在master上進行修改,你可以在slave上看到各種記錄檔的位置的變化,同樣,你也可以看到資料庫中資料的變化。

你可查看master和slave上線程的狀態。在master上,你可以看到slave的I/O線程建立的串連:

2、可能還用到的命令

slave stop   //停止從資料庫配置

reset slave //複位從資料庫配置


本文出自 “5311” 部落格,請務必保留此出處http://cuis1.blog.51cto.com/8805022/1890626

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.