redis主從複製

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   使用   ar   for   

Redis 是一個高效能的key-value資料庫。 redis的出現,很大程度補償了memcached這類keyvalue儲存的不足,在部分場合可以對關聯式資料庫起到很好的補充作用。它提供了Python,Ruby,Erlang,PHP用戶端,使用很方便。

效能測試結果:

SET操作每秒鐘 110000 次,GET操作每秒鐘 81000 次,伺服器配置如下:

Linux 2.6, Xeon X3320 2.5Ghz.

stackoverflow 網站使用 Redis 做為快取服務器。

redis可以做一個主從複製的設定,見redis.conf

################################# REPLICATION ################################## Master-Slave replication. Use slaveof to make a Redis instance a copy of# another Redis server. Note that the configuration is local to the slave# so for example it is possible to configure the slave to save the DB with a# different interval, or to listen to another port, and so on.## slaveof <masterip> <masterport> #主服務ip和連接埠# If the master is password protected (using the "requirepass" configuration# directive below) it is possible to tell the slave to authenticate before# starting the replication synchronization process, otherwise the master will# refuse the slave request.## masterauth <master-password> #如果master設定了密碼,slave需要進行認證# When a slave loses its connection with the master, or when the replication# is still in progress, the slave can act in two different ways:## 1) if slave-serve-stale-data is set to ‘yes‘ (the default) the slave will#    still reply to client requests, possibly with out of date data, or the#    data set may just be empty if this is the first synchronization.## 2) if slave-serve-stale-data is set to ‘no‘ the slave will reply with#    an error "SYNC with master in progress" to all the kind of commands#    but to INFO and SLAVEOF.#
#當slave失去與master的串連,或正在拷貝中,如果為yes,slave會響應用戶端請求,資料可能不同步甚至沒有資料,如果為no,slave會返回錯誤"SYNC with master in progress"

slave-serve-stale-data yes
# You can configure a slave instance to accept writes or not. Writing against# a slave instance may be useful to store some ephemeral data (because data# written on a slave will be easily deleted after resync with the master) but# may also cause problems if clients are writing to it because of a# misconfiguration.## Since Redis 2.6 by default slaves are read-only.## Note: read only slaves are not designed to be exposed to untrusted clients# on the internet. It‘s just a protection layer against misuse of the instance.# Still a read only slave exports by default all the administrative commands# such as CONFIG, DEBUG, and so forth. To a limited extend you can improve# security of read only slaves using ‘rename-command‘ to shadow all the# administrative / dangerous commands.
slave-read-only yes #從伺服器唯讀,不可寫# Slaves send PINGs to server in a predefined interval. It‘s possible to change# this interval with the repl_ping_slave_period option. The default value is 10# seconds.## repl-ping-slave-period 10# The following option sets a timeout for both Bulk transfer I/O timeout and# master data or ping response timeout. The default value is 60 seconds.## It is important to make sure that this value is greater than the value# specified for repl-ping-slave-period otherwise a timeout will be detected# every time there is low traffic between the master and the slave.## repl-timeout 60# Disable TCP_NODELAY on the slave socket after SYNC?## If you select "yes" Redis will use a smaller number of TCP packets and# less bandwidth to send data to slaves. But this can add a delay for# the data to appear on the slave side, up to 40 milliseconds with# Linux kernels using a default configuration.## If you select "no" the delay for data to appear on the slave side will# be reduced but more bandwidth will be used for replication.## By default we optimize for low latency, but in very high traffic conditions# or when the master and slaves are many hops away, turning this to "yes" may# be a good idea.repl-disable-tcp-nodelay no# The slave priority is an integer number published by Redis in the INFO output.# It is used by Redis Sentinel in order to select a slave to promote into a# master if the master is no longer working correctly.# A slave with a low priority number is considered better for promotion, so# for instance if there are three slaves with priority 10, 100, 25 Sentinel will# pick the one wtih priority 10, that is the lowest.## However a special priority of 0 marks the slave as not able to perform the# role of master, so a slave with priority of 0 will never be selected by# Redis Sentinel for promotion.## By default the priority is 100.slave-priority 100 #如果master不能再正常工作,那麼會在多個slave中,選擇優先值最小的一個slave提升為master,優先值為0表示不能提升為master

看了上面的預設配置,其實也很容易理解,修改下就可以配置主從複製了,有 slaveof <masterip> <masterport> ,就成了從伺服器 ,沒有就是主伺服器

主伺服器,從伺服器,配置一樣,只不過 修改/etc/redis.conf 中 

slaveof 192.168.1.33 6379

然後開啟從伺服器的redis服務。

測試

#主伺服器redis-cli -p 6379 set hello world#從伺服器redis-cli -p 6379 get hello"world"#主伺服器redis-cli -p 6379 set hello world2#從伺服器redis-cli -p 6379 get hello"world2"redis-cli -p 6379 set hello world(error) READONLY You can‘t write against a read only slave.#成功 配置主從redis 伺服器。好簡單啊。

由於配置中有一條 從伺服器 是唯讀,所以從伺服器 沒法設定資料,只可以讀取資料

redis主從複製

相關文章

聯繫我們

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