mysql主從設定筆記

來源:互聯網
上載者:User

CREATE TABLE Persons
(
Id int unsigned not null AUTO_INCREMENT,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
created datetime,
primary key (id)
)type=MyISAM AUTO_INCREMENT = 1;
INSERT INTO Persons(LastName,FirstName,created) VALUES (’Huaming’,'Yue’,NOW());

//主伺服器
[mysqld]
log-bin=mysql-bin
server-id=1
binlog-ignore-db=mysql, test
#binlog-do-db=vbb

//建立帳號供從伺服器使用
GRANT ALL ON *.* TO rep_slave@’%’ IDENTIFIED BY ‘joeyue’;
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO rep_slave@’%’ IDENTIFIED BY ‘joeyue’;
FLUSH PRIVILEGES;

從伺服器
log-bin=mysql-bin
server-id=2
master-host=192.168.1.104
master-user=rep_slave
master-password=joeyue
master-port=3306
master-connect-retry=60
log-slave-updates
#replicate-do-db=foxshare  //指定同步的資料庫
INSERT INTO Persons(Id,LastName,FirstName,created) VALUES (2,’Huaming’,'Yue’,NOW());

//注意
Share the same database and table structure

<?php
 $link=mysql_connect(’192.168.1.104:3306′,’user’,'password’);
 if(!$link) {
  echo ‘failed’;
 } else {
  mysql_select_db(’Persons’, $link);
  for($i=0; $i<50;$i++) {
   mysql_query(”INSERT INTO Persons(Id,LastName,FirstName,created) VALUES (2,’Huaming’,'Yue’,NOW());”);
  }
 }
 mysql_close($link);
?>

下面是具體配置過程:
Master:
1.為從機(Slave)建立一個複製許可權賬戶
mysql > GRANT REPLICATION SLAVE ON *.* TO ’slave’@'172.20.92.110′ IDENTIFIED BY ’slave’;
Ps:replication slave 為單一的複製許可權 使用者名稱slave 密碼slave
2.鎖定主機資料庫 匯出當前鎖定狀態下所有資料到從機中 做好複製前的基本資料同步工作
mysql > FLUSH TABLES WITH READ LOCK;
mysql > exit
[root@%%] # mysqldump -uroot -p test > test.sql
3.查詢當前主機資料庫檔案和位置
mysql > show master status;
——————+———-+————–+——————+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000041 |     1509 |              |                  |
+——————+———-+————–+——————+
Ps:記錄下這兩個值(檔案名稱、當前位置),一會兒配置從機串連主機時要用到。

Slave:
1.建立資料庫 test ,將主機Master產生的sql檔案匯入到test資料庫中。
2.關掉Slave,將主機記錄檔和位置資訊讀入從機
mysql > stop slave;
mysql > change master to
       ->master_log_file=’mysql-bin.000041′,
       ->master_log_pos=1509;
mysql > start slave;
mysql > show slave status \G
mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 172.20.92.108              ##主機ip
                Master_User: slave                      ##複製帳號
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000041           ##主機記錄檔
        Read_Master_Log_Pos: 1509                       ##主機記錄檔位置
             Relay_Log_File: mysqld-relay-bin.000007    ##從機中繼記錄檔
              Relay_Log_Pos: 235                        ##中繼記錄檔位置
      Relay_Master_Log_File: mysql-bin.000041
           Slave_IO_Running: Yes                        ##從機IO線程 串連主機
          Slave_SQL_Running: Yes                        ##從機sql線程 處理更新的日誌
            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: 1509
            Relay_Log_Space: 235
            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
1 row in set (0.00 sec)

接下來,解鎖Master,允許主機資料庫更新。

Master:

mysql > unlock tables;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.