1. 程式人生 > >mysql主從介紹,準備工作,主配置,從配置,主從測試

mysql主從介紹,準備工作,主配置,從配置,主從測試

實時 ini warn 語句 rwx OS www. 技術分享 master

主從介紹
  • MySQL主從又叫做Replication、AB復制。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟著寫數據,兩者數據實時同步。
  • MySQL主從是基於binlog的,主上須開啟binlog才能進行主從。
  • 主從過程三個步驟
    • 主將更改操作記錄到binlog中
    • 從將主的binlog事件(SQL語句)同步到本機並記錄在relaylog中
    • 從根據relaylog裏面的SQL語句按順序執行
  • 該過程有三個線程,主上有一個logdump線程,用來和從的i/o線程傳遞binlog;從上有兩個線程,其中i/o線程用來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的SQL語句落地。
    技術分享圖片
  • 應用環境:備份數據和分擔主庫數據讀取壓力

    準備工作

  • 準備兩臺機器,分別安裝好mysql並啟動服務,akuilinux01為主,02為從。

    配置主服務器

  • 編輯配置文件,增加兩行,並重啟服務
    [root@akuilinux01 src]# vim /etc/my.cnf
    server-id=130
    #自定義
    log_bin=linux1
    #指定binlog前綴
    [root@akuilinux01 src]# /etc/init.d/mysqld restart
    Shutting down MySQL... SUCCESS! 
    Starting MySQL... SUCCESS! 
  • 把mysql庫備份並恢復成aming庫,作為測試數據
    查看庫,帶akuilinux01的是新生成的。
    [root@akuilinux01 src]# ll /data/mysql/
    總用量 110688
    -rw-rw---- 1 mysql mysql      120 6月  29 09:32 akuilinux01.000001
    -rw-rw---- 1 mysql mysql    71795 6月  29 09:32 akuilinux01.err
    -rw-rw---- 1 mysql mysql       21 6月  29 09:32 akuilinux01.index
    -rw-rw---- 1 mysql mysql        5 6月  29 09:32 akuilinux01.pid
    -rw-rw---- 1 mysql mysql       56 5月  23 15:38 auto.cnf
    -rw-rw---- 1 mysql mysql 12582912 6月  29 09:32 ibdata1
    -rw-rw---- 1 mysql mysql 50331648 6月  29 09:32 ib_logfile0
    -rw-rw---- 1 mysql mysql 50331648 5月  23 15:32 ib_logfile1
    drwx------ 2 mysql mysql     4096 5月  23 15:32 mysql
    drwx------ 2 mysql mysql     4096 5月  23 15:32 performance_schema
    drwx------ 2 mysql mysql        6 5月  23 15:27 test
    drwx------ 2 mysql mysql      324 6月  27 22:52 zrlog
    備份庫
    [root@akuilinux01 src]# cd /data/mysql/
    [root@akuilinux01 mysql]# mysqldump -uroot -ps5381561 mysql > /tmp/mysql.sql
    Warning: Using a password on the command line interface can be insecure.
    新建庫
    [root@akuilinux01 mysql]# mysql -uroot -ps5381561 -e "create database akui"
    備份恢復到新建庫
    [root@akuilinux01 mysql]# mysql -uroot -ps5381561 akui < /tmp/mysql.sql
  • 創建一個用戶用作同步數據
    mysql> grant replication slave on *.* to ‘repl‘@192.168.21.129 identified by ‘password‘;
    Query OK, 0 rows affected (0.00 sec)
    #ip為從機器的ip
    mysql> flush tables with read lock;
    Query OK, 0 rows affected (0.09 sec)
    #鎖定數據表(目的是暫時使其不能繼續寫,保持現有狀態用於同步)
    mysql> show master status;
    +--------------------+----------+--------------+------------------+-------------------+
    | File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +--------------------+----------+--------------+------------------+-------------------+
    | akuilinux01.000001 |   660260 |              |                  |                   |
    +--------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    #記住file和position(設置主從同步時會使用)
    mysql> quit
    Bye
  • 備份主中的所有數據庫,mysql中包含用戶和密碼的信息可以不用備份
    root@akuilinux01 ~]# cd /data/mysql/
    [root@akuilinux01 mysql]# ls
    akui                akuilinux01.index  ibdata1      mysql               zrlog
    akuilinux01.000001  akuilinux01.pid    ib_logfile0  performance_schema
    akuilinux01.err     auto.cnf           ib_logfile1  test
    [root@akuilinux01 mysql]# mysqldump -uroot -ps5381561 zrlog > /tmp/zrlog.sql
    Warning: Using a password on the command line interface can be insecure.
    [root@akuilinux01 mysql]# mysqldump -uroot -ps5381561 akui > /tmp/akui.sql
    Warning: Using a password on the command line interface can be insecure.

    配置從服務器

  • 安裝mysql並啟動
  • 查看my.cnf,配置server-id=131,要求和主不一樣,並重啟服務
    [root@akuilinux02 mysql]# vim /etc/my.cnf
    server-id=131
    [root@akuilinux02 mysql]# /etc/init.d/mysqld restart
    Shutting down MySQL.. SUCCESS! 
    Starting MySQL. SUCCESS! 
  • 同步主的備份數據到從上
    [root@akuilinux02 mysql]# scp 192.168.21.128:/tmp/*.sql /tmp/
    akui.sql                                                                            100%  644KB  15.6MB/s   00:00    
    mysql.sql                                                                           100%  644KB  26.1MB/s   00:00    
    zrlog.sql                                                                           100% 9869     1.7MB/s   00:00    
  • 創建從上的庫
    [root@akuilinux02 mysql]# alias ‘mysql=/usr/local/mysql/bin/mysql‘
    [root@akuilinux02 mysql]# alias ‘mysqldump=/usr/local/mysql/bin/mysqldump‘
    [root@akuilinux02 mysql]# mysql -uroot
    mysql> create database akui
    -> ;
    Query OK, 1 row affected (0.00 sec)
    mysql> create database zrlog;
    Query OK, 1 row affected (0.00 sec)
    mysql> quit
    Bye
  • 恢復庫
    [root@akuilinux02 mysql]# mysql -uroot akui < /tmp/akui.sql 
    [root@akuilinux02 mysql]# mysql -uroot zrlog < /tmp/zrlog.sql 
    保證和主上的一致
  • 主從同步
    [root@akuilinux02 mysql]# mysql -uroot 
    mysql> stop slave;
    Query OK, 0 rows affected (0.01 sec)
    mysql> change master to master_host=‘192.168.21.128‘,master_user=‘repl‘,master_password=‘password‘,master_log_file=‘akuilinux01.000001‘,master_log_pos=660260;
    #IP為主的IP;file、pos分別為主的filename和position。
    Query OK, 0 rows affected, 2 warnings (0.02 sec)
    mysql> start slave;
    Query OK, 0 rows affected (0.01 sec)
  • 從上檢測是否同步成功
    mysql> show slave status\G
    Relay_Master_Log_File: akuilinux01.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            Seconds_Behind_Master: 0  //為主從延遲的時間
            Last_IO_Errno: 0
            Last_IO_Error:
            Last_SQL_Errno: 0
            Last_SQL_Error:
  • 還要解鎖主庫的表
    [root@akuilinux01 mysql]# mysql -urelp -ppassword
    mysql> unlock tables;
    Query OK, 0 rows affected (0.00 sec)

    幾個重要的參數

  • 主服務器上/etc/my.cnf
    • binlog-do-db= //僅同步指定的庫
    • binlog-ignore-db= //忽略指定庫
  • 從服務器上/etc/my.cnf
    • replicate_do_db=
    • replicate_ignore_db= #這兩個盡量不要用
    • replicate_do_table=
    • replicate_ignore_table= #這兩個盡量不要用
    • replicate_wild_do_table= //如aming.%, 支持通配符%,代表aming這個庫
    • replicate_wild_ignore_table= #盡量使用這兩個,使匹配更精確

      主從測試

  • 主上
    mysql> use akui;
    Database changed
    mysql> show tables;
    +---------------------------+
    | Tables_in_akui            |
    +---------------------------+
    | columns_priv              |
    | db                        |
    | event                     |
    | func                      |
    | general_log               |
    | help_category             |
    | help_keyword              |
    | help_relation             |
    | help_topic                |
    | innodb_index_stats        |
    | innodb_table_stats        |
    | ndb_binlog_index          |
    | plugin                    |
    | proc                      |
    | procs_priv                |
    | proxies_priv              |
    | servers                   |
    | slave_master_info         |
    | slave_relay_log_info      |
    | slave_worker_info         |
    | slow_log                  |
    | tables_priv               |
    | time_zone                 |
    | time_zone_leap_second     |
    | time_zone_name            |
    | time_zone_transition      |
    | time_zone_transition_type |
    | user                      |
    +---------------------------+
    28 rows in set (0.00 sec)
    刪除akui庫
    mysql> drop database akui;
    Query OK, 28 rows affected (0.04 sec)
  • 從上
    刪除之前
    mysql> use akui;
    Database changed
    mysql> show tables;
    +---------------------------+
    | Tables_in_akui            |
    +---------------------------+
    | columns_priv              |
    | db                        |
    | event                     |
    | func                      |
    | general_log               |
    | help_category             |
    | help_keyword              |
    | help_relation             |
    | help_topic                |
    | innodb_index_stats        |
    | innodb_table_stats        |
    | ndb_binlog_index          |
    | plugin                    |
    | proc                      |
    | procs_priv                |
    | proxies_priv              |
    | servers                   |
    | slave_master_info         |
    | slave_relay_log_info      |
    | slave_worker_info         |
    | slow_log                  |
    | tables_priv               |
    | time_zone                 |
    | time_zone_leap_second     |
    | time_zone_name            |
    | time_zone_transition      |
    | time_zone_transition_type |
    | user                      |
    +---------------------------+
    28 rows in set (0.00 sec)
    刪除之後
    mysql> show tables;
    ERROR 1049 (42000): Unknown database ‘akui‘

    問題

  • " rel="nofollow">遇到主從不能正常同步,提示uuid相同的錯誤。這是因為克隆機器導致

mysql主從介紹,準備工作,主配置,從配置,主從測試