2018-07-18筆記(mysql主從)

17.1 MySQL主從介紹

  1. MySQL主從又叫做Replication、AB複製。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟着寫數據,兩者數據實時同步的
  2. MySQL主從是基於binlog的,主上須開啓binlog才能進行主從。
  3. 主從過程大致有3個步驟
    1)主將更改操作記錄到binlog裏
    2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏
    3)從根據relaylog裏面的sql語句按順序執行
  4. 主上有一個log dump線程,用來和從的I/O線程傳遞binlog
  5. 從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的sql語句落地
  6. 使用場景:
    1). 數據庫備份
    2).數據庫備份, 客戶端也要到 從數據庫上讀數據, 但不能到 從數據庫上寫數據. 減輕主庫讀的壓力
    2018-07-18筆記(mysql主從)

    17.2 準備工作

    準備兩臺機器,分別安裝mysql,版本最好是相同的
    安裝過程可以參考https://blog.51cto.com/13736286/2135537 mysql的安裝
    這裏已經安裝好了,一臺hostname爲master,ip爲192.168.66.128 一臺hostname爲slave,ip爲192.168.66.129
    配置之前,保持兩臺mysql的數據是一致的,可以通過mysqldump把主上數據備份後,上傳到從上,然後恢復

    17.3 配置主

    1、在master上修改mysql配置文件/etc/my.cnf,設置server-id和打開log-bin二進制日誌

    [root@master ~]# vi /etc/my.cnf      #增加下面兩行  
    server-id=1                         #本機標識號,唯一,數字可以隨意設置,和從上要不一樣     
    log_bin=master                       #打開log-bin,master爲log-bin日誌的前綴

    2、修改完配置文件後,啓動或者重啓mysqld服務

    [root@master ~]# /etc/init.d/mysqld restart

    3、重啓完成後在mysql數據目錄下會生成相應的mater開頭的二進制日誌文件, 這些文件是實現主從的根本,
    我的數據目錄是在/var/lib/mysql下

    [root@master ~]# ll /var/lib/mysql/
    總用量 28772
    -rw-rw----. 1 mysql mysql 18874368 7月  16 06:42 ibdata1
    -rw-rw----. 1 mysql mysql  5242880 7月  16 06:42 ib_logfile0
    -rw-rw----. 1 mysql mysql  5242880 7月  16 06:42 ib_logfile1
    -rw-rw----  1 mysql mysql      106 7月  16 06:39 master.000001
    -rw-rw----  1 mysql mysql       16 7月  16 06:39 master.index
    drwx------. 2 mysql mysql     4096 5月   5 02:28 mysql
    srwxrwxrwx  1 mysql mysql        0 7月  16 06:39 mysql.sock
    drwx------. 2 mysql mysql     4096 5月   5 04:23 performance_schema
    drwx------  2 mysql mysql     4096 5月  20 10:12 test

    注:myster.000001就是二進制日誌文件
    4、登陸mysql,創建用作同步數據的用戶

    mysql> grant replication slave on *.* to 'repl'@'192.168.66.129' identified by '123456';
    Query OK, 0 rows affected (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    5、爲了保持數據一致, 把表鎖一下, 不繼續寫.

    mysql> flush tables with read lock;
    Query OK, 0 rows affected (0.00 sec)

    6、查看主的狀態,並記錄下log-bin文件的名字和偏移量

    mysql> show master status;
    +---------------+----------+--------------+------------------+
    | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +---------------+----------+--------------+------------------+
    | master.000001 |      336 |              |                  |
    +---------------+----------+--------------+------------------+
    1 row in set (0.00 sec)

    注:master.000001爲log-bin文件的名字,336爲偏移量,指定從這個點開始同步

    17.4 配置從

    1、修改從的mysql配置文件/etc/my.cnf,只用設置server-id即可,不用打開log-bin

    [root@slave ~]# vi /etc/my.cnf          #增加一行
    server-id=2                                       #這個數字可以隨意設置,但要區別於主設置的id

    2、修改完配置文件,重啓mysql服務

    [root@slave ~]# /etc/init.d/mysqld restart

    3、登陸從的mysql,停止slave,並更改主的信息,這一步非常關鍵

    mysql> stop slave;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    mysql> change master to master_host='192.168.66.128', master_user='repl', master_password='123456', master_log_file='master.000001', master_log_pos=336;
    Query OK, 0 rows affected (0.06 sec)

    注意:
    這一步很關鍵,這裏的change master的信息就是主的ip 還有創建授權的用戶和密碼,還有master的 log-bin文件和偏移量
    4、啓動slave,並查看狀態

    mysql> start slave;
    Query OK, 0 rows affected (0.00 sec)
    mysql> show slave status\G
    *************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.66.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000001
          Read_Master_Log_Pos: 336
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 249
        Relay_Master_Log_File: master.000001
             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: 336
              Relay_Log_Space: 405
              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)

    可以看到剛纔配置的主的一些信息,關鍵的兩個指標 Slave_IO_Running: Yes Slave_SQL_Running: Yes,看這兩個狀態是否爲Yes,Yes表示正常,如果其中有一個爲No表示不正常,如果有錯誤可以在 Last_Errno: 0 Last_Error: 兩個指標中體現出

    至此,從配置完成
    下面這一步在主上執行
    不要忘記,剛纔主上爲了防止寫入數據鎖表了,要解鎖,回到主上,登陸mysql,執行

    mysql> unlock tables;
    Query OK, 0 rows affected (0.00 sec)

    17.5 測試主從同步

    1、幾個配置參數在主從my.cnf設置
    (主和從設置一個即可)
    可在mysql的配置文件my.cnf中增加以下參數,表示,要同步或忽略的庫
    主:可支持逗號分隔設置多個

    binlog-do-db=      //僅同步指定的庫,多個建議寫多行
    binlog-ignore-db=  //忽略指定庫

    從:

    replicate_do_db=  //僅同步指定的庫
    replicate_ignore_db= //忽略指定庫
    replicate_do_table=  //僅同步指定的表(慎用) --假設表示test.xx 帶結尾的也會忽略導致庫不完整,so,建議用wild下面2個支持通配。
    replicate_ignore_table= //忽略指定的表(慎用)
    replicate_wild_do_table=   //同步指定的表支持通配(如test.%) 
    replicate_wild_ignore_table=   //忽略指定的表支持通配(如test.%)

    2、測試主從是否同步
    在主上創建一個數據庫luo

    mysql> create database luo;
    Query OK, 1 row affected (0.02 sec)
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | luo                |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    5 rows in set (0.01 sec)

    在從上直接查看是否自動創建了數據庫luo

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | luo                |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    5 rows in set (0.01 sec)

    可以看到從上已經自動創建了數據庫luo,說明數據是同步的

擴展的一些鏈接:
mysql主從
https://my.oschina.net/u/3791387/blog/1837336

相關擴展
不停庫不鎖表在線主從配置
http://seanlook.com/2015/12/14/mysql-replicas/

主從不同步
http://www.rfyy.net/archives/2309.html
https://blog.51cto.com/storysky/259280

主主
關於 auto_increment https://blog.csdn.net/leshami/article/details/39779509
http://www.cnblogs.com/ygqygq2/p/6045279.html

mysql-proxy 實現讀寫分離
https://blog.51cto.com/zzclinux/1980487

mysql-proxy類似的產品有:
mycat 基於阿里的開源軟件cobar,官網 www.mycat.io
https://my.oschina.net/ruoli/blog/1789370

mycat實現分庫分表
https://www.cnblogs.com/joylee/p/7513038.html

atlas 出自於360,不維護不更新了 https://blog.csdn.net/AnPHPer/article/details/80566385

mysql環形主從
http://ask.apelearn.com/question/11437

mysql架構演變 http://www.aminglinux.com/bbs/thread-8025-1-1.html

MHA架構
https://blog.51cto.com/xiaoshuaigege/2060768

比較複雜的mysql集羣架構 http://ask.apelearn.com/question/17026

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章