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把主上數據備份後,上傳到從上,而後恢復html

    17.3 配置主

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

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

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

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

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

    [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就是二進制日誌文件
    四、登錄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)

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

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

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

    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 配置從

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

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

    二、修改完配置文件,重啓mysql服務線程

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

    三、登錄從的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文件和偏移量
    四、啓動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 測試主從同步

    一、幾個配置參數在主從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.%)

    二、測試主從是否同步
    在主上建立一個數據庫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主從
http://www.javashuo.com/article/p-ssmsbwqm-k.html

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

主從不一樣步
http://www.rfyy.net/archives/2309.html
http://www.javashuo.com/article/p-cthpurvb-bb.html

主主
關於 auto_increment http://www.javashuo.com/article/p-tamhibns-by.html
http://www.cnblogs.com/ygqygq2/p/6045279.html

mysql-proxy 實現讀寫分離
http://www.javashuo.com/article/p-gjdwpcia-k.html

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

mycat實現分庫分表
http://www.javashuo.com/article/p-xbjkpuop-bb.html

atlas 出自於360,不維護不更新了 http://www.javashuo.com/article/p-sofkphwt-m.html

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

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

MHA架構
http://www.javashuo.com/article/p-rrhwavha-k.html

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

相關文章
相關標籤/搜索