mysql主從同步配置方法和原理


mysql主從複製試驗環境:
    1.CentOS5.8 x86_64位 採用最小化安裝,系統經過了基本優化篇
    2.mysql版本: 5.1.73-log ,同臺服務器多實例,安裝方法參考:mysql多實例安裝方法

    3.源碼包存放位置:/home/oldboy/tools
    4.源碼包編譯安裝位置:/application/
一.修改master配置
    1.修改my.cnf配置
         vi /data/3306/my.cnf
         egrep "log-bin|server-id" /data/3306/my.cnf    #檢查設置成如下
            log-bin = /data/3306/mysql-bin
            server-id = 1
            #transaction_isolation = READ-COMMITTED        
            #要註釋掉.不然插入數據,會報如下錯誤
                mysql> insert into stu values (1, 'txidc'),(2,'gaogao');
                ERROR 1598 (HY000): Binary logging not possible. Message: Transaction level 'READ-COMMITTED' in InnoDB is not safe for binlog mode 'STATEMENT'
        /data/3306/mysql start
        
    2.mysql主庫增加用於同步用戶:
        
        mysql> grant replication slave on *.* to rep@'192.168.1.%' identified by 'mygirl99'
        mysql> select user,host from mysql.user;
        +------+-------------+
        | user | host        |
        +------+-------------+
        | root | 127.0.0.1   |
        | rep  | 192.168.1.% |
        | root | localhost   |
        | root | ser200      |
        +------+-------------+
        4 rows in set (0.00 sec)

        mysql> show grants for rep@'192.168.1.%';
        +--------------------------------------------------------------------------------------------------------------------------+
        | Grants for [email protected].%                                                                                               |
        +--------------------------------------------------------------------------------------------------------------------------+
        | GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.1.%' IDENTIFIED BY PASSWORD '*3561920064C790E42F7FBEBD0303ACC7C1EF855E' |
        +--------------------------------------------------------------------------------------------------------------------------+
        1 row in set (0.00 sec)
    3.主庫鎖表只讀:此時業務會受影響.生產環境要考慮.要是停機選擇流量最小時間
        mysql> flush tables with read lock;
        Query OK, 0 rows affected (0.00 sec)

        mysql> show variables like '%timeout%'
            -> ;
        +----------------------------+-------+
        | Variable_name              | Value |
        +----------------------------+-------+
        | connect_timeout            | 10    |
        | delayed_insert_timeout     | 300   |
        | innodb_lock_wait_timeout   | 120   |
        | innodb_rollback_on_timeout | OFF   |
        | interactive_timeout        | 28800 |        #交互操時時間是28800秒
        | net_read_timeout           | 30    |
        | net_write_timeout          | 60    |
        | slave_net_timeout          | 3600  |
        | table_lock_wait_timeout    | 50    |
        | wait_timeout               | 28800 |        #連接等待時間是28800秒,這2項時間要是過短.有可能會自動解鎖表
        +----------------------------+-------+
        10 rows in set (0.00 sec)
    4.查看主庫的位置狀態,從庫裏面是從此位置開始同步
        mysql> show master status;
        +------------------+----------+--------------+------------------+
        | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
        +------------------+----------+--------------+------------------+
        | mysql-bin.000008 |      832 |              |                  |
        +------------------+----------+--------------+------------------+
        1 row in set (0.00 sec)
    5.導出主庫
        [root@ser200 3306]# mysqldump -uroot -p  -A -B -S /data/3306/mysql.sock |gzip >/server/bak/mysql_bak.$(date +%Y%m%d).sql.gz
        Enter password:
        -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
        [root@ser200 3306]# ll /server/bak/mysql_bak.20141029.sql.gz
        -rw-r--r-- 1 root root 144960 Oct 29 15:48 /server/bak/mysql_bak.20141029.sql.gz
        
        注意事項:
            -A:備份所有庫    -B,備份時增加use db 或者drop
            如果數據庫超過了100G+,備份時允許停機的話.可以直接打包數據文件遷移

    6.在確認下主庫的位置狀態,確保導出過程,沒有數據插入或者sql更新等操作
        [root@ser200 3306]# mysql -uroot -pmybaby3306 -S /data/3306/mysql.sock -e 'show master status';
        +------------------+----------+--------------+------------------+
        | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
        +------------------+----------+--------------+------------------+
        | mysql-bin.000008 |      832 |              |                  |
        +------------------+----------+--------------+------------------+
    7.解鎖主庫,恢復業務    
        mysql> unlock tables;
        Query OK, 0 rows affected (0.00 sec)
二.從庫slave操作
    1.修改從庫配置:
        [root@ser200 3306]# cd /data/3307/
        [root@ser200 3307]# vi my.cnf
        [root@ser200 3307]# egrep "server-id|log-bin" my.cnf
        #log-bin = /data/3307/mysql-bin
        server-id = 2
    2.導入數據庫
        [root@ser200 3307]# /data/3307/mysql start
        [root@ser200 3307]# gunzip /server/bak/mysql_bak.20141029.sql.gz
        [root@ser200 3307]# ll /server/bak/mysql_bak.20141029.sql
        -rw-r--r-- 1 root root 527860 Oct 29 15:48 /server/bak/mysql_bak.20141029.sql
        [root@ser200 3307]# mysql -uroot -pmybaby3307 -S /data/3307/mysql.sock < /server/bak/mysql_bak.20141029.sql

        檢查導入狀態:

        mysql> show databases;
        +--------------------+
        | Database           |
        +--------------------+
        | information_schema |
        | mysql              |
        | test               |
        | test3306           |
        +--------------------+
        4 rows in set (0.00 sec)
    3.登陸從庫.配置從庫同步參數
        change master to \
        master_host="192.168.1.200", \
        master_user="rpe", \
        master_port=3306, \
        master_password="mygirl99", \
        master_log_file="mysql-bin.000008", \
        master_log_pos=832;
    4.啓動從庫同步開關
        mysql> start slave;         
        Query OK, 0 rows affected (0.00 sec)


        報錯
        mysql> show slave status \G;
        *************************** 1. row ***************************
                   Slave_IO_State: Connecting to master
                  Master_Host: 192.168.1.200
                  Master_User: rpe
                  Master_Port: 3306
                Connect_Retry: 60
                  Master_Log_File: mysql-bin.000008
              Read_Master_Log_Pos: 832
                   Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
            Relay_Master_Log_File: mysql-bin.000008
                 Slave_IO_Running: No
                Slave_SQL_Running: Yes
        原因:
            主庫的用戶名設置錯誤,造成連接不上.設置對應的就OK了
        mysql> show slave status \G;
        *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.200
                  Master_User: rpe
                  Master_Port: 3306
                Connect_Retry: 60
                  Master_Log_File: mysql-bin.000008
              Read_Master_Log_Pos: 1329
                   Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 748
            Relay_Master_Log_File: mysql-bin.000008
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB:
              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: 1329
                  Relay_Log_Space: 897
                  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:
        1 row in set (0.00 sec)


三.不停機實現主從庫
    1,原理:備份數據庫記下bin-log的文件名和位置,下次導入時,從該位置開始同步就行了.

更多的mysql安裝教程請點擊

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