Redis的安裝與持久化

本文主要是對於redis安裝與持久化中非常淺顯的知識進行介紹,並不深入瞭解,只接觸表面,對一些較複雜的內容也不過多描述。如文中有錯誤之處,望不吝賜教,謝謝~

一、Redis簡介

Redis(Remote Dictionary Server ),即遠程字典服務,是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日誌型、Key-Value數據庫,並提供多種語言的API。

Redis是一款高性能的NOSQL系列的非關係型數據庫,redis在Java開發中主要用來存儲緩存用的數據以及在需要高速讀/寫的場合使用它快速讀/寫。

NoSQL技術,這是一種基於內存的數據庫,並且提供一定的持久化功能。
Redis和MongoDB是當前使用最廣泛的NoSQL,而就Redis技術而言,它的性能十分優越,可以支持每秒十幾萬此的讀/寫操作,其性能遠超數據庫,並且還支持集羣、分佈式、主從同步等配置,十分強大。

在一般情況下,不區分redis技術和redis數據庫,都稱作redis。
redis作爲一種非關係型數據庫,其是基於內存,不同於關係數據庫基於硬盤,其速度遠遠大於關係數據庫。但是由於內存的侷限性,所以對何種數據何時使用redis我們需要做出取捨。
一般對於讀操作較多且數據量較小的情況可考慮使用redis,例如“好友列表查詢”“排行榜”等等。

二、安裝redis

(1)進入github-redis下載redis壓縮包

在這裏插入圖片描述
(2)解壓下載下來的redis-64-3.0.503.zip ,直接打開便可使用。(其實這是綠色版本,不用安裝,下載下來解壓就好)。

  • redis.windows.conf:配置文件
  • redis-cli.exe:redis的客戶端
  • redis-server.exe:redis服務器端

在這裏插入圖片描述

一般啓動好服務器端再打開客戶端就可以使用redis了。

三、redis持久化

由於redis的數據存儲在內存中,不便於保存(當服務器和客戶端關閉或電腦關機時,數據會丟失),爲了將redis中的數據保存在硬盤中,便需要持久化技術。
redis持久化分爲兩種方式一種是RDB(默認方式),一種是AOF方式。

(1)RDB

redis.windows.conf配置文件中,有專門對RDB持久化的配置。

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

*“sava 900 1”*表示900秒內至少有一個鍵值發生改變,就執行持久化操作一次,相應的我們可以根據需要自行修改持久化條件。
每次持久化後的rdb文件會保存在redis的解壓目錄下。

(2)AOF:日誌記錄的方式,可以記錄每一次命令的操作並持久化

redis.windows.conf配置文件中,有專門對AOF持久化的配置。

############################## APPEND ONLY MODE ###############################

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly no

"appendonly no"表示默認AOF方式是關閉的,若要打開,將no改爲yes即可。

# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log . Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".

# appendfsync always
appendfsync everysec
# appendfsync no

"appendfsync always"表示每一次操作都進行持久化,“appendfsync everysec”表示每隔一秒進行一次持久化操作(默認是這樣),“appendfsync no”表示不進行持久化操作。
每次持久化後的aof文件會保存在redis的解壓目錄下。

在這裏插入圖片描述
2020.03.07

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