mysql數據庫主從同步

 

 

環境:mysql

Mater:   CentOS7.1  5.5.52-MariaDB  192.168.108.133sql

Slave:   CentOS7.1  5.5.52-MariaDB  192.168.108.140數據庫

1.導出主服務數據,將主備初始數據同步vim

master:ide

//從master上導出須要同步的數據庫信息
mysqldump -u*** -p*** --database test > test.sql
//將master上的備份信息傳輸到slave上
scp /root/test.sql root@192.168.108.140:/opt/

slave:post

複製代碼
//進入slave的數據庫
mysql -u*** -p***
//清空test數據庫
drop database test
//導入master的test數據庫信息
source /opt/test.sql
複製代碼

2.配置master和slave上的mysql數據庫測試

master:spa

複製代碼
//修改master的my.cnf文件
vim /etc/my.cnf
//master配置以下,在[mysqld]下添加以下配置
#log-bin
server-id          =   1
log_bin            =   master-bin
expire_logs_days   =   10
max_binlog_size    =   100M
binlog-do_db       =   test
binlog_ignore_db   =   mysql
//重啓mysql數據庫
service mysqld restart
//若是安裝的是mariadb能夠重啓mariadb
systemctl restart mariadb.service
複製代碼

slave:rest

複製代碼
//修改slave的my.cnf文件
vim /etc/my.cnf
//slave配置以下,在[mysqld]下添加以下配置
server-id         =   2
//重啓mysql數據庫
service mysqld restart
//若是安裝的是mariadb能夠重啓mariadb
systemctl restart mariadb.service
複製代碼

簡單說明一下參數配置,保證主備server-id惟一。在master上須要開啓mysql的binlog,log_bin=master_bin,指定binlog文件的名稱。日誌

3.建立一個複製用戶,具備replication slave 權限,能保證slave能把master的數據同步過去

master:

grant replication slave on *.* to 'replication'@'192.168.108.140' identified by 'replication';

4.獲取master的binlog位置

master:

複製代碼
//進入mysql數據庫
mysql -u*** -p***
//設置讀鎖
flush tables with read lock;
//獲取mysql的binlog文件信息和偏移量
show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000010 |     3713 | test         | mysql            |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
//解鎖
unlock tables;
複製代碼

5.設置備端數據庫

複製代碼
//進入mysql數據庫
mysql -u*** -p***
//中止slave
stop slave;
//設置對應master的binlog信息
MariaDB [(none)]> change master to
    -> master_host='192.168.108.133',
    -> master_user='replication',
    -> master_password='replication',
    -> master_log_file='master-bin.000010',
    -> master_log_pos=3713;
//啓動slave
start slave;
複製代碼

6.查看備端狀態

複製代碼
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.108.133
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000010
          Read_Master_Log_Pos: 3881
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 698
        Relay_Master_Log_File: master-bin.000010
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 3881
              Relay_Log_Space: 994
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified
複製代碼
若是:Slave_IO_Running: Yes,Slave_SQL_Running: Yes則爲配置成功,配置錯誤重複上面操做便可。若是解決不了可經過查看mysql日誌分析處理。
vim /var/log/mariadb/mariadb.log

7.測試。其實測試沒啥好寫的,配置成功以後直接連到主從數據庫,在master上改變表、字段、數據,slave會同步變化。

寫在最後:當時想的試一試能不能用mysql自帶的功能作數據庫災備,後來發現mysql數據庫主從同步會有一些問題。第一個很差腳本化的東西是在同步以前需保證兩邊的數據庫初始信息同樣,由於備端配置的mysql-binlog位置只是當前主數據庫信息的位置,在該位置以前的數據只能經過人工導入。第二個就是mysql主從同步時,只能進行數據庫的增量同步,不能進行全量同步;還有就是若是備端出現髒數據,多了一條數據,當主那邊新增一條主鍵相同的數據,則同步失敗。以後我會嘗試的能不能把這些操做腳本化,發現mysql自帶的同步功能限制性很大,而且手工干預的東西太多了。

相關文章
相關標籤/搜索