mysql主從複製

準備兩臺服務器:python

主庫:192.168.0.1
從庫:192.168.0.2mysql

第一步:主庫開啓二進制日誌sql

log-bin=mysql-bin

 緣由:二進制日誌必須開啓,由於數據的同步實質上就是主數據庫服務器將這個數據變動的二進制日誌在從庫上再執行一遍。數據庫

第二步:把主庫全備一份,再從庫上恢復
緣由:在開啓主從複製前保證兩個庫數據一致服務器

第三步:開始構建主從複製測試

1>在(192.168.0.1)主庫上建立(192.168.0.2)從庫主機能夠登陸的mysql用戶

mysql>>CREATE USER tongbu@'192.168.0.2' IDENTIFIED BY '123123';
mysql>>GRANT REPLICATION SLAVE ON *.* TO tongbu@'192.168.0.2' IDENTIFIED BY '123123';
mysql>>FLUSH PRIVILEGES;

2>查看(192.168.0.1)主庫mysql服務器二進制文件名和位置

mysql>>show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000022 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3>定義(192.168.0.2)從庫主機訪問(192.168.0.1)主庫二進制日誌起始點
!!注意:在從庫上執行
mysql>>change master to
      >master_host='192.168.0.1',               #主庫主機ip
	  >master_user='tongbu',                    #主庫上定義的從庫同步用戶
	  >master_password='123123',                #密碼
	  >master_log_file='mysql-bin.000022',      #主庫當前在寫二進制日誌文件
	  >master_log_pos=120;                      #主庫當前在寫二進制日誌指針位置

4>(192.168.0.2)從庫上開啓複製
mysql>>start slave;
#查看主從複製是否配置成功

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.26.164.201
                  Master_User: tongbu
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000022
          Read_Master_Log_Pos: 120
               Relay_Log_File: iZ2ze104wexxcmflfj6d7sZ-relay-bin.000069
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000022
             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: 120
              Relay_Log_Space: 637
              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: 111
                  Master_UUID: ca33e05a-7f55-11e8-9bdb-00163e0c8d2f
             Master_Info_File: /d/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)


是否成功主要看參數:
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

以上就是mysql的主從複製了,想測試直接在主庫上插入一條記錄,看從庫上是否同步出現!!!.net

!!!注意:
(1)若是有人誤操做想從庫寫數據或者改數據,那數據不就亂了嗎?
解決:從庫my.cnf里加
read_only=1 #只讀
(2)主庫mysql庫裏配置的各類用戶或者權限,不必同步到從庫裏,怎麼解?
           binlog-ignore=mysql   #忽略mysql庫
           binlog-ignore=information_schema   #忽略information_schema庫
(3)主庫只想同步指定的數據庫給從庫,怎麼解?
           binlog-do-db = testa
           binlog-do-db = testb           
(4)從庫指定接收的數據庫是哪些,操做以下
           replicate_do_db=testa #不建議用
           replicate_wild_do_table= testa.% #建議用
        緣由:https://blog.csdn.net/nuli888/article/details/51924152指針

#############################################################日誌

擴展:mysql的主主複製code

實現原理:
主主複製即在兩臺MySQL主機內均可以變動數據,並且另一臺主機也會作出相應的變動。聰明的你也許已經想到該怎麼實現了。對,就是將兩個主從複製有機合併起來就行了。 

!!!注意: 

只不過在配置的時候咱們須要注意一些問題,
例如,主鍵重複,server-id不能重複等等。

server-id=11   #任意天然數n,只要保證兩臺MySQL主機不重複就能夠了。
auto_increment_increment=2   #步進值auto_imcrement。通常有n臺主MySQL就填n
auto_increment_offset=1   #起始值。通常填第n臺主MySQL。此時爲第一臺主MySQL
相關文章
相關標籤/搜索