MySql主從複製配置樣本,mysql主從樣本

來源:互聯網
上載者:User

MySql主從複製配置樣本,mysql主從樣本

    最近研究mysql,從oracle官網下了一個最新的5.6的mysql,壓縮版的。今天嘗試一下Mysql的複製功能,花了好久才整出來,把心得分享出來。

    用的是我們開發用的庫,部署在測試環境,測試環境的mysql版本居然是5.1的。

    1.主伺服器配置

     cd /etc,找到my.cnf,然後再這個設定檔加上複製要用的配置

server-id       = 1log-bin=mysql-binbinlog-do-db=wx

    主伺服器需要一個獨一無二的server id,log-bin表示啟用二進位日誌,binlog-do-db表示選擇記錄日誌的資料庫

    2.從機配置

    開啟my.ini,加上和伺服器類似的配置

server-id=2log-bin=mysql-bin replicate-do-db=wx

    3.將主機上的wx資料庫拷給從機。這步一定要執行,不然後面一定會報錯。我就是忽略了這一步,導致試了好幾次都沒有成功!當然,這一步可以用任何方法做,用工具,用mysqldump後從機執行都可以。

    4.主機上建複製帳號

    主機的mysql命令列下執行如下命令,建一個用來複製的帳號,和密碼

grant replication slave  on *.*  to 'replication'@'%' identified by '123'; 

    然後查主機的狀態

mysql> show master status\G*************************** 1. row ***************************            File: mysql-bin.000003        Position: 256    Binlog_Do_DB: wxBinlog_Ignore_DB: 1 row in set (0.00 sec)

    Position和File都是後面有用的

    5.從機關聯主機操作

    從機上執行關聯主機的命令,並重啟從機。

change master to  master_host = '192.168.146.120',  master_user = 'replication',  master_password = '123',  master_log_file = 'mysql-bin.000003',  master_log_pos = 256;  start slave; 

    6.驗證操作

    主機上wx庫找張表,插入一條語句,從機立刻複製過來。

    查看從機狀態。

mysql> show slave status\G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.146.120                  Master_User: replication                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000003          Read_Master_Log_Pos: 708               Relay_Log_File: CDYJY-LVSHENG1-relay-bin.000004                Relay_Log_Pos: 265        Relay_Master_Log_File: mysql-bin.000003             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB: wx          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: 708              Relay_Log_Space: 588              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: 0Master_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                  Master_UUID:             Master_Info_File: D:\DevelopTools\DB\mysql-5.6.26-winx64\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: 01 row in set (0.00 sec)


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.