MySQL 主從複製----之基礎

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   檔案   

1 資料庫伺服器 一主一備,是很常見的備用模型

利用資料庫主備模型的好處是:

  從伺服器上面 可以做停掉 冷備份  提供高可用的功能 做一些簡單的修複  異地容災  scale out: 分攤負載  (進階特性)     主負責 寫 多個從伺服器負責 讀 2 原理 包含的記錄檔1 bin-log 記錄任何可能改變資料庫操作的SQL語句2 relay-log 中繼日誌  Slave 從Master得到的日誌 簡單介紹原理就是 通過Master 伺服器記錄的bin-log記錄檔,被發送到 Slave伺服器,作為Slave的中繼日誌(relay-log),Slave服伺服器,再將這個日誌作為SQL語句重新執行一遍,便改變了資料庫內容,寫入date中! 即: 主伺服器的操作,被同步的寫入了 Slave伺服器,但可能會比主伺服器慢。(非同步模式下)  Master 開啟一個 dump線程Slave 開啟兩個線程一個  IO線程負責log讀取,一個SQL線程負責從先log中的SQL語句執行  3 實驗主Master Server: 192.168.204.5備Slave  Server:  192.168.204.6  mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz   通用格式二進位包 一 、主Master 配置解壓步驟省略...... 指定資料目錄 /data/mysql  chown -R mysql.mysql /data/mysql確定存放日誌的檔案mkdir /data/mysql/logschown -R mysql.mysql /data/mysql/logs 初始化[[email protected] mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql複製設定檔和開機檔案[[email protected] mysql]# cp support-files/my-default.cnf /etc/my.cnf[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld 修改設定檔vim /etc/my.cnfdatadir = /data/mysql  啟動必須的 針對Master 伺服器的配置server-id =100  必須唯一log-bin=/data/mysql/logs/master-bin  肯定要啟動bin-log日誌功能log-bin-index=/data/mysql/logs/master-bin.index 最佳化的配置 開啟滿日誌查詢

slow_query_log = ON
slow_query_log_file = /data/mysql/logs/master_slow.log

 

 

3、建立一個具有複製許可權的使用者REPLICATION SLAVEREPLICATION CLIENT mysql> grant replication slave on *.* to ‘repluser‘@‘192.168.204.6‘ identified by ‘replpass‘;

mysql> flush privileges;

 4 記住當前的binlog file 座標positon

mysql> flush tables;show master status;
Query OK, 0 rows affected (0.00 sec)

+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master_bin.000003 | 488 | | | |
+-------------------+----------+-

 

二、Slave 伺服器配置

1 同上初始化

[[email protected] mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

2 修改vim /etc/my.cnf

datadir= /data/mysql

server-id =200
relay_log=relay_log
relay_log_index=relay_log.index

 

注: 由於Slave 只作為從伺服器,不提供讀寫,所以不需要記錄binlog 日誌,節省資源

3 啟動mysql,串連主伺服器

/etc/init.d/mysqld start

mysql> change master to
-> master_host= ‘192.168.204.5‘, master_port=3306,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘master_bin.000003‘,master_log_pos=488;

 

4 讓從伺服器啟動線程複製

mysql> start slave;

 

5 查看Slave 工作狀態

show slave status\G;

 

6 測試

主伺服器上

建立一個庫

mysql> create database mydb;

查看從伺服器上是否更新

show databases;

和查看show slave status\G; 是否一些參數已經改變

 

 

擴充知識點

1 master.info 檔案

  從伺服器把change master 語句所給出的參數儲存在其資料目錄中的一個名為master.info 的檔案裡記錄初始複製狀態,並隨著複製工作的進展而重新整理這個檔案。

2 如何不讓從伺服器複製主服務的特定庫

  replication-ignore-db=databesename

3 Master 和 Slave 啟動了哪些線程 參與複製

   複製線程    master: dump    slave: IO_Tread,SQL_Tread    mysql> START SLAVE; 等於啟動這兩個線程IO線程負責與主伺服器進行通訊,接收來自主伺服器的資訊,把接收到的資料修改命令寫入從伺服器的中繼日誌SQL線程負責從中繼日誌讀取出資料修改命令並執行STOP SLAVE START SLAVE 後面可以接 {IO_Tread|SQL_Tread}  

 

 

MySQL 主從複製----之基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.