天天看點

linux 安裝配置redislinux環境下安裝redis

linux環境下安裝redis

環境

  • 系統:Linux
  • 版本: Red Hat 4.8.5-16
  • 服務商:阿裡雲
  • 核心:3.10.0-693.2.2.el7.x86_64

step 1: 在使用者級程式目錄下建立 /usr/local/redis

cd /usr/local/
mkdir redis
           

step 2: 擷取redis資源,伺服器下載下傳源檔案

wget http://download.redis.io/releases/redis-5.0.5.tar.gz
           

step 3: 進行解壓資源,并編譯

tar -xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make 
           

step 4:測試安裝是否完成

./redis-server
// 重開一個視窗
./redis-cli
           

執行完以上步驟就安裝成功了,但是隻能是簡單安裝,革命尚未成功

配置

我們在項目中使用redis,可能要設定密碼,通路端口等配置,這些配置是在redis.conf中配置的。是以,我們接下來就要進行配置工作。

  • 我們從官網下載下傳一份配置檔案https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf,在該基礎上進行配置工作
  • 增加配置檔案,redis支援啟動時指定配置檔案,同時也支援多個配置檔案的方式。多配置檔案通過我們啟動時指定的配置檔案來綁定。
# 啟動時指定配置檔案
./redis-server /path/to/redis.conf
           
# 如果你需要覆寫掉我們啟動時配置檔案的設定,你可以在下面進行指定配置檔案路徑來實作(最後一個配置會覆寫之前的),同時這也是多配置檔案的實作方式
  31 # If instead you are interested in using includes to override configuration
  32 # options, it is better to use include as the last line.
  33 #
  34 # include /path/to/local.conf
  35 # include /path/to/other.conf
           
  • 通路控制,此處可以注釋,注釋後為全網通路,如果保持預設 127.0.0.1 則隻能本機通路
65 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
  66 # JUST COMMENT THE FOLLOWING LINE.
  67 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68 bind 127.0.0.1
           
  • 端口監聽,系統預設為6379,再進行主從配置的時候需要修改此處配置,也可以通過啟動時參數指定端口
89 # Accept connections on the specified port, default is 6379 (IANA #815344).
  90 # If port 0 is specified Redis will not listen on a TCP socket.
  91 port 6379
           
  • 連接配接數,在高并發的環境下,你需要設定更高的backlog來避免用戶端連接配接緩慢。

    注意,Linux系統會自動的修改這個值為/proc/sys/net/core/somaxconn的值(這個值預設為128),是以請保證你同時修改了這兩個值

93 # TCP listen() backlog.
  94 #
  95 # In high requests-per-second environments you need an high backlog in order
  96 # to avoid slow clients connections issues. Note that the Linux kernel
  97 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
  98 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
  99 # in order to get the desired effect.
 100 tcp-backlog 511
           
  • 逾時斷開連接配接,在用戶端閑置 N秒時,主動關閉該連接配接。如果設定為0,則不主動關閉
111 # Close the connection after a client is idle for N seconds (0 to disable)
 112 timeout 0
           
  • 指定日志檔案名稱,在多節點時可以用于區分
167 # Specify the log file name. Also the empty string can be used to force
 168 # Redis to log on the standard output. Note that if you use standard
 169 # output for logging but daemonize, logs will be sent to /dev/null
 170 logfile ""
           
  • 指定databases,如果我們在不同的程式中使用相同的databases會造成髒資料的情況,同時也可能會串讀。比如A系統有一個token,B系統也有一個,這個時候就不能區分了,會進行覆寫操作。不同的系統應該指定不同的databases,無上限,指令行可以通過select N 進行切換。注意,不同的databases并不是完全隔離,FLUSHALL 指令就會清除所有記錄,而不是目前databases中的記錄。建議使用時不同程式使用不同redis執行個體。
182 # Set the number of databases. The default database is DB 0, you can select
 183 # a different one on a per-connection basis using SELECT <dbid> where
 184 # dbid is a number between 0 and 'databases'-1
 185 databases 16
           
  • 持久化,這裡可以設定我們持久化到硬碟的規則,因為redis是基于記憶體,在斷電或者當機的情況後如果沒有持久化進行資料恢複,這就可能會造成資料的丢失。是以此處是持久化到硬碟,規則為900 秒中至少有一次修改,或者300秒有10次修改,或者60秒内有10000次修改
195 ################################ SNAPSHOTTING  ################################
 196 #
 197 # Save the DB on disk:
 198 #
 199 #   save <seconds> <changes>
 200 #
 201 #   Will save the DB if both the given number of seconds and the given
 202 #   number of write operations against the DB occurred.
 203 #
 204 #   In the example below the behaviour will be to save:
 205 #   after 900 sec (15 min) if at least 1 key changed
 206 #   after 300 sec (5 min) if at least 10 keys changed
 207 #   after 60 sec if at least 10000 keys changed
 208 #
 209 #   Note: you can disable saving completely by commenting out all "save" lines.
 210 #
 211 #   It is also possible to remove all the previously configured save
 212 #   points by adding a save directive with a single empty string argument
 213 #   like in the following example:
 214 #
 215 #   save ""
 216 
 217 save 900 1
 218 save 300 10
 219 save 60 10000
           
  • 如果快照失敗,則停止寫入
221 # By default Redis will stop accepting writes if RDB snapshots are enabled
 222 # (at least one save point) and the latest background save failed.
 223 # This will make the user aware (in a hard way) that data is not persisting
 224 # on disk properly, otherwise chances are that no one will notice and some
 225 # disaster will happen.
 226 #
 227 # If the background saving process will start working again Redis will
 228 # automatically allow writes again.
 229 #
 230 # However if you have setup your proper monitoring of the Redis server
 231 # and persistence, you may want to disable this feature so that Redis will
 232 # continue to work as usual even if there are problems with disk,
 233 # permissions, and so forth.
 234 stop-writes-on-bgsave-error yes
           
  • 壓縮字元串寫入rdb,如果關閉RBD程序可能會快一些,但是最後的rdb檔案可能會更大
236 # Compress string objects using LZF when dump .rdb databases?
 237 # For default that's set to 'yes' as it's almost always a win.
 238 # If you want to save some CPU in the saving child set it to 'no' but
 239 # the dataset will likely be bigger if you have compressible values or keys.
 240 rdbcompression yes
           
  • 指定持久化檔案寫入的路徑
254 # The working directory.
255 #
256 # The DB will be written inside this directory, with the filename specified
257 # above using the 'dbfilename' configuration directive.
258 #
259 # The Append Only File will also be created inside this directory.
260 #
261 # Note that you must specify a directory here, not a file name.
262 dir ./
           
  • 主從節點配置,此處為從節點配置。配置後在啟動子節點時會主動連接配接主節點進行資料同步
266 # Master-Slave replication. Use slaveof to make a Redis instance a copy of
 267 # another Redis server. A few things to understand ASAP about Redis replication.
 268 #
 269 # 1) Redis replication is asynchronous, but you can configure a master to
 270 #    stop accepting writes if it appears to be not connected with at least
 271 #    a given number of slaves.
 272 # 2) Redis slaves are able to perform a partial resynchronization with the
 273 #    master if the replication link is lost for a relatively small amount of
 274 #    time. You may want to configure the replication backlog size (see the next
 275 #    sections of this file) with a sensible value depending on your needs.
 276 # 3) Replication is automatic and does not need user intervention. After a
 277 #    network partition slaves automatically try to reconnect to masters
 278 #    and resynchronize with them.
 279 #
 280 # slaveof <masterip> <masterport>
           
  • 主從節點配置,主節點連接配接密碼
282 # If the master is password protected (using the "requirepass" configuration
 283 # directive below) it is possible to tell the slave to authenticate before
 284 # starting the replication synchronization process, otherwise the master will
 285 # refuse the slave request.
 286 #
 287 # masterauth <master-password>
           
  • 子節點異常丢失,或者主從同步時,設定為yes,則表示子節點任然可以繼續響應請求,如果為no則slave會回複"正在從master同步(SYNC with master in progress)"來處理各種請求,除了 INFO 和 SLAVEOF 指令。
289 # When a slave loses its connection with the master, or when the replication
 290 # is still in progress, the slave can act in two different ways:
 291 #
 292 # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
 293 #    still reply to client requests, possibly with out of date data, or the
 294 #    data set may just be empty if this is the first synchronization.
 295 #
 296 # 2) if slave-serve-stale-data is set to 'no' the slave will reply with
 297 #    an error "SYNC with master in progress" to all the kind of commands
 298 #    but to INFO and SLAVEOF.
 299 #
 300 slave-serve-stale-data yes
           
  • 将記憶體中的資料寫入磁盤,redis提供了三種政策,always:每次有修改新增時,everysec:每秒寫入,no: 不主動寫入。預設每秒,這樣對系統資源使用較為友好,資料如果丢失也僅限于秒級資料。
681 # Redis supports three different modes:
 682 #
 683 # no: don't fsync, just let the OS flush the data when it wants. Faster.
 684 # always: fsync after every write to the append only log. Slow, Safest.
 685 # everysec: fsync only one time every second. Compromise.
 686 #
 687 # The default is "everysec", as that's usually the right compromise between
 688 # speed and data safety. It's up to you to understand if you can relax this to
 689 # "no" that will let the operating system flush the output buffer when
 690 # it wants, for better performances (but if you can live with the idea of
 691 # some data loss consider the default persistence mode that's snapshotting),
 692 # or on the contrary, use "always" that's very slow but a bit safer than
 693 # everysec.
 694 #
 695 # More details please check the following article:
 696 # http://antirez.com/post/redis-persistence-demystified.html
 697 #
 698 # If unsure, use "everysec".
 699 
 700 # appendfsync always
 701 appendfsync everysec
 702 # appendfsync no
           
  • 重寫本地檔案,當新寫入檔案達到上一次寫入大小的100%,或者記憶體為64mb時,重寫
725 # Automatic rewrite of the append only file.
 726 # Redis is able to automatically rewrite the log file implicitly calling
 727 # BGREWRITEAOF when the AOF log size grows by the specified percentage.
 728 #
 729 # This is how it works: Redis remembers the size of the AOF file after the
 730 # latest rewrite (if no rewrite has happened since the restart, the size of
 731 # the AOF at startup is used).
 732 #
 733 # This base size is compared to the current size. If the current size is
 734 # bigger than the specified percentage, the rewrite is triggered. Also
 735 # you need to specify a minimal size for the AOF file to be rewritten, this
 736 # is useful to avoid rewriting the AOF file even if the percentage increase
 737 # is reached but it is still pretty small.
 738 #
 739 # Specify a percentage of zero in order to disable the automatic AOF
 740 # rewrite feature.
 741 
 742 auto-aof-rewrite-percentage 100
 743 auto-aof-rewrite-min-size 64mb
           

以上隻是對配置檔案部分設定做了說明,其中還有很多未列出。詳情請檢視redis.conf 檔案

繼續閱讀