mysql主從複製環境搭建

來源:互聯網
上載者:User

標籤:mysql   主從搭建   

mysql 主從

第一次我用不同版本mysql做會出不同步問題,建議用相同版本的mysql做主從


  1. 主my.cnf檔案 (192.168.1.64)

mysqld模組加入

log-bin=mysql-bin 啟動二進位檔案

server-id=1 伺服器ID

binlog-do-db = 需要複製的庫名可以用,分割

如果主要資料庫存有資料

首先進行鎖表操作,不讓資料進行寫入動作,這麼做事為了防止從資料庫的未經處理資料和主要資料庫的未經處理資料不一致。

主進入mysql

mysql> flush tables with read lock;

mysqldump –uroot –p123456 testDB > /home/testDB.sql

scp  -r /home/testDB.sql [email protected]:/home

從進入mysql

create database testDB;

mysql -u root  "你的資料庫名"< “你的sql檔案”

做完之後進行unlock tables; 解鎖表操作

進入mysql建立使用者並且給從使用#grant replication slave on *.* to ‘test‘@‘192.168.1.12‘ identified by ‘1234‘;也可以Grant all on testDB.* to ‘test‘@‘192.168.1.12‘ identified by ‘1234‘ with grant option;show master status;| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000002 |      106 | testDB       |                  |+------------------+----------+--------------+------------------+


2.從my.cnf檔案(192.168.1.12)


mysqld模組加入

log-bin=mysql-bin 啟動二進位檔案

server-id=2 伺服器ID

進入mysqlchange master to master_host=‘192.168.1.64‘,master_user=‘test‘,master_password=‘1234‘,master_log_file=‘mysql-bin.000002‘,master_log_pos=2852;start slave;show slave status\G
mysql>  show slave status\G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.64                  Master_User: test                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000002          Read_Master_Log_Pos: 2852               Relay_Log_File: mysqld-relay-bin.000002                Relay_Log_Pos: 251        Relay_Master_Log_File: mysql-bin.000002             Slave_IO_Running: Yes            Slave_SQL_Running: Yes

     

要求Slave_IO_Running 和SQL等於yes才行

如果倆個有一個沒有yes

檢查資料庫使用者權限和防火牆,

從伺服器登陸主測試mysql -h192.168.1.64 -utest -p 

在檢查serverip

mysql> show variables like ‘server_id‘;

或者slave stop;   slave start;

如果還不行就裝倆個一樣版本的mysql測試一下




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.