1. 程式人生 > >mysql主從同步/主主同步

mysql主從同步/主主同步

got 如果 status not unlock sql數據庫 cati 重啟 stop

Centos環境
198.168.1.20
198.168.1.21

master操作:

修改mysql配置文件(my.cnf)

[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=xxx     //要同步的庫
binlog-ignore-db=mysql   //不同步的
sync_binlog = 1      //類似實時存入binlog,=0則會先寫進緩存
binlog_checksum = none   //此命令可以同步不同版本的mysql數據庫,mysql5.6.5以後的版本中binlog_checksum默認值是crc32,
binlog_format = mixed
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

鎖定數據庫
flush tables with read lock;
//此命令為只讀鎖,unlock tables命令解除鎖定


導出要同步的庫

 mysqldump  -uroot 庫名 -p > /root/mobi.sql

rsync傳輸sql庫
rsync -r "ssh -p22" -avpgolr /root/mobi.sql 192.168.1.21:/root


授權用戶(測試註意防火墻的關閉)

grant replication slave,replication client on *.* to root@‘192.168.1.21‘ identified by ‘123456‘;
flush privileges;

查看授權:
show grants for root@‘192.168.1.21‘;


查看主服務器狀態:

show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000024 |       592 |              |                  |
+------------------+----------+--------------+------------------+

從服務器操作

修改配置文件my.cnf

server-id=2
log-bin=mysql-bin
replicate-do-db=mobi
replicate-ignore-db=mysql
slave-skip-errors = all
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql

如果有庫則先dump下備份,避免操作失誤

mysqldump  -uroot 要備份的庫 -p > /root/要備份的庫.sql.bak
drop 要備份的庫;

創建空庫然後導入

mysql> create database 要備份的庫;
mysql> use mobi;
mysql> source /root/要備份的庫.sql

額外內容(同步表內容):


當只針對某些庫的某張表進行同步時,如下,只同步huanqiu庫的haha表和huanpc庫的heihei表:
replicate-do-db = 庫名
replicate-wild-do-table = 庫名.表名       //當只同步幾個或少數表時,可以這樣設置。註意這要跟上面的庫指定配合使用;
replicate-do-db = 庫名
replicate-wild-do-table = 庫名.表名      //如果同步的庫的表比較多時,就不能這樣一一指定了,就把這個選項配置去掉,直接根據指定的庫進行同步。

=================================

配置主從指令:

mysql> stop slave;(同步前先關閉)
mysql> change  master to master_host=‘192.168.1.20‘,master_user=‘root‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000024‘,master_log_pos=592;
mysql> start slave;

從庫先查看slave狀態

show slave status\G;

看是不是兩個yes 如果不是就有問題

測試階段(主庫)

mysql> unlock tables;  

//解鎖
mysql> create table zhang (id,int)

從庫查看
mysql> desc zws;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| student | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+

主主同步

=============================================================================

=============================================================================

master中修改

修改配置文件 my.cnf

server-id = 1        
log-bin = mysql-bin  
binlog-ignore-db = mysql,information_schema
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2    
auto-increment-offset = 1   
slave-skip-errors = all 

授權用戶:

mysql> grant replication slave,replication client on *.* to root@‘192.168.1.21‘ identified by "123456";
mysql> flush privileges;

mysql> FLUSH TABLES WITH READ LOCK; //註意該參數設置後,如果自己同步對方數據,同步前一定要記得先解鎖!

mysql> show master status; //查看主信息

slave庫配置

修改配置文件my.cnf

[mysqld]
server-id=2
log-bin=mysql-bin
replicate-do-db=mobi
replicate-ignore-db=mysql
slave-skip-errors = all
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 2
slave-skip-errors = all
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql

授權用戶

mysql> grant replication slave ,replication client on *.* to root@‘192.168.1.20‘ identified by "123456";  
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000020 |      602 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

主庫解鎖

mysql> unlock tables;     //先解鎖,將對方數據同步到自己的數據庫中
mysql> stop slave ;
mysql> change master to master_host=‘192.168.1.20‘,master_user=‘root‘,master_password=‘123456‘,master_log_file=‘master-bin.000027‘,master_log_pos=150;
mysql> start slave;
mysql> show slave status \G;

此處實現了slave--->master的功能

====================================================================

master---->slave功能


mysql> unlock tables;
mysql> stop slave ;
mysql> change master to master_host=‘192.168.1.21‘,master_user=‘root‘,master_password=‘123456‘,master_log_file=‘master-bin.000023‘,master_log_pos=150;
mysql> start slave;
mysql> show slave status \G;

此刻完成

====================================================================

問題排錯:
出現

Slave_IO_Running: No
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘

解決方案:
1)從庫上執行
stop slave;
2)主庫進行刷新日誌
flush logs;
3)查看日誌
mysql> show master status\G;
4)重新
CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000114‘,MASTER_LOG_POS=120;
5)重啟
start slave;

mysql主從同步/主主同步