一、安裝環境
操作系統: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 /etc/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 'root'@'192.168.2.2' IDENTIFIED BY 'root password';   #創建用戶
mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.2.2' IDENTIFIED BY 'root password';  #修改用戶權限
mysql>select host ,user ,password from mysql.user;  #查看是否修改成功
mysql>FLUSH PRIVILEGES;   #刷新
 
        
注:如果數據庫有數據需要進行數據遷移保證數據的一致性 數據遷移
創建數據庫: 在從庫中創建一個和主庫相同的數據庫,不然兩個數據庫不能同步(進行過數據遷移就跳過)
CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci; 
        
4.重啟MySQL,登錄MySQL,查看主庫信息
mysql> show master status; 
        顯示內容
+------------------+----------+--------------+----------------------------------+-------------------+
| 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='root',master_password='root password',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 /usr/local/mysql/data/auto.cnf #這是我的安裝路徑修改auto.cnf的server-uuid
注:如果Slave_IO_Running: Connecting 並出現下面錯誤
error connecting to master 'root@192.168.3.28: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;  //開啟 
        
以上主從MySQL已經可以使用了,歡迎各位多提bug
小擴展
當只針對某些庫的某張表進行同步時,如下,只同步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 ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019
 
        
手動清理master日志,最好關閉日志,在/etc/my.cnf
#手動刷新日志 mysql> show master status; #刪除全部
mysql> reset slave;或 rest master;
#刪除MySQL-bin.010
mysql> PURGE MASTER LOGS TO 'MySQL-bin.010';
