CentOS7+mysql5.6配置主從

 

一、安裝環境 

操作系統:CentOS-7-x86_64-DVD-1611.iso

數據庫版本:mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

數據庫地址:
  192.168.2.1(主)
  192.168.2.2(從)

MySQL在5.6之前和之後的安裝方式是不一樣的。 

自己整理的mysql安裝,主從配置基於這篇安裝:http://www.cnblogs.com/cypress/p/8608496.html

首先保證3306端口的可用,或者關閉防火牆,兩臺機子可以互相ping

二、Master的配置

1.修改MySQL配置文件

[root@localhost ~]# vim /opt/mysql/my.cnf



[mysqld]
#開啓二進制日誌
log-bin=mysql-bin
#標識唯一id(必須),一般使用ip最後位
server-id=2
#不同步的數據庫,可設置多個
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
#指定需要同步的數據庫(和slave是相互匹配的),可以設置多個
binlog-do-db=test

#添加日誌存儲方式和規則(選填)
#設置存儲模式不設置默認
binlog_format=MIXED
#日誌清理時間
expire_logs_days=7
#日誌大小
max_binlog_size=100m
#緩存大小
binlog_cache_size=4m
#最大緩存大小
max_binlog_cache_size=521m

2.重啓MySQL

service mysqld restart

3.進去mysql設置允許從庫獲得主庫日誌  

注:這裏使用root用戶配置,不建議使用,正常使用新創建的用戶進行授權

[root@localhost ~]# mysql -u root -p

#給從庫放權限
mysql>GRANT FILE ON *.* TO 'zhw'@'192.168.2.2' IDENTIFIED BY '123456';   #創建用戶
mysql>GRANT REPLICATION SLAVE ON *.* TO 'zhq'@'192.168.2.2' IDENTIFIED BY '123456';  #修改用戶權限
mysql>select host ,user ,password from mysql.user;  #查看是否修改成功
mysql>FLUSH PRIVILEGES;   #刷新

注:如果數據庫有數據需要進行數據遷移保證數據的一致性

創建數據庫: 在從庫中創建一個和主庫相同的數據庫,不然兩個數據庫不能同步(進行過數據遷移就跳過)

1

CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci;

4.重啓MySQL,登錄MySQL,查看主庫信息

1

mysql> show master status;

 顯示內容

1

2

3

4

5

6

7

8

+------------------+----------+--------------+----------------------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set |

+------------------+----------+--------------+----------------------------------+-------------------+

| mysql-bin.000006 |    120 | ufind_db     | information_schema,cluster,mysql |                   |

+------------------+----------+--------------+----------------------------------+-------------------+

1 row in set (0.00 sec)

 

mysql>

注:如果執行這個步驟始終爲Empty set(0.00 sec),那說明前面的my.cnf沒配置對

三、Slave的配置

1.從庫配置

#開啓二進制日誌(可以不配置)
log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql#與主庫配置一直
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

4.重啓MySQL,登錄MySQL

#關閉Slave
mysql> stop slave;  #設置連接主庫信息
mysql> change master to master_host='192.168.2.1',master_user='zhw',master_password='123456',master_log_file='mysql-bin.000006', master_log_pos=120;
#開啓Slave
mysql> start slave;

 注:上面的master_log_file是在配置Master的時候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一對應

5.查看信息

mysql> show slave status \G;


*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000006
                Relay_Log_Pos: 520
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes  //顯示yes爲成功
            Slave_SQL_Running: Yes  //顯示yes爲成功,如果爲no,一般爲沒有啓動master
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql//上面的都是配置文件中的信息
           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: 357
              Relay_Log_Space: 697
              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:  //如果爲no,此處會顯示錯誤信息
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
                  Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)
 
ERROR:
No query specified

 注:如果Slave_IO_Running: No  出現下面的錯誤

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

說明主服務器的UUID和從服務器的UUID重複,更改方式

[root@localhost ~]# vim /opt/mysql/data/auto.cnf  #這是我的安裝路徑修改auto.cnf的server-uuid

###查看auto.cnf文件
[root@dbsrv1 ~] cat /data/mysqldata/auto.cnf  ### 主上的uuid
[auto]
server-uuid=62ee10aa-b1f7-11e4-90ae-080027615026

[root@dbsrv2 ~]# more /data/mysqldata/auto.cnf ###從上的uuid,果然出現了重複,原因是克隆了虛擬機,只改server_id不行
[auto]
server-uuid=62ee10aa-b1f7-11e4-90ae-080027615026

[root@dbsrv2 ~]# mv /data/mysqldata/auto.cnf  /data/mysqldata/auto.cnf.bk  ###重命名該文件
[root@dbsrv2 ~]# service mysql restart          ###重啓mysql
Shutting down MySQL.[  OK  ]
Starting MySQL.[  OK  ]
[root@dbsrv2 ~]# more /data/mysqldata/auto.cnf  ###重啓後自動生成新的auto.cnf文件,即新的UUID
[auto]
server-uuid=6ac0fdae-b5d7-11e4-a9f3-0800278ce5c
###再次查看slave的狀態已經正常
[root@dbsrv1 ~]# mysql -uroot -pxxx -e "show slave status\G"|grep Running

Warning: Using a password on the command line interface can be insecure.
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

 

 注:如果Slave_IO_Running: Connecting 並出現下面錯誤

error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1

  解決方法,查看主庫是否授權,查看change master to... 是否有用戶密碼ip填寫錯誤

 

   注:如果Slave_IO_Running: No 出現下面錯誤

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

  解決方法:復位

mysql>stop slave;  //停止
mysql>reset slave;  //清空
mysql>start slave;  //開啓

原文:https://www.cnblogs.com/cypress/p/8610279.html

小擴展

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

關於增刪改查,主從數據不一致問題:

#select 語句,暫時沒有發現問題
 
#insert 語句,暫時沒有發現問題
 
#update 語句,暫時沒有發現問題
 
#delete 語句,主庫刪除多條數據,發現數據不一致
原因:在主庫的logbin中的確有執行刪除語句,但是在從庫的logbin中卻沒有刪除語句
解決:使用 use database 選取當前數據庫架構中的需要操作的數據庫,然後在執行刪除,OK同步成功

查詢binlog主從日誌的方法

#查看binlog全部文件
mysql>show binary logs;
 
#查看binlog是否開啓NO爲開啓
mysql> show variables like 'log_bin%';
 
#詳細信息
mysql>  show variables like 'binlog%';
 
#查看binlog日誌
mysql> show binlog events in'mysql-bin.000019';
 
#或者使用mysqlbinlog,如果報錯使用--no-defaults(使用全路徑)
[root@localhost ~]# /opt/mysql/bin/mysqlbinlog --no-defaults /opt/mysql/data/mysql-bin.000019

手動清理master日誌,最好關閉日誌,在/opt/mysql/my.cnf

#手動刷新日誌
mysql> show master status;
#刪除全部mysql> reset slave;或 rest master;#刪除MySQL-bin.010mysql> PURGE MASTER LOGS TO 'MySQL-bin.010';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章