標籤:mysql 主從搭建
mysql 主從
第一次我用不同版本mysql做會出不同步問題,建議用相同版本的mysql做主從
主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主從複製環境搭建