linux安裝redis完整步驟


linux安裝redis完整步驟

安裝:
1.獲取redis資源
  wget http://download.redis.io/releases/redis-4.0.8.tar.gz

2.解壓
  tar xzvf redis-4.0.8.tar.gz

3.安裝
  cd redis-4.0.8
  make
  cd src
  make install PREFIX=/usr/local/redis

4.移動配置文件到安裝目錄下
  cd ../
  mkdir /usr/local/redis/etc
  mv redis.conf /usr/local/redis/etc

5.配置redis為后台啟動
  vi /usr/local/redis/etc/redis.conf //將daemonize no 改成daemonize yes

6.將redis加入到開機啟動
  vi /etc/rc.local //在里面添加內容:/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf (意思就是開機調用這段開啟redis的命令)

7.開啟redis
  /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

 

常用命令  
  redis-server /usr/local/redis/etc/redis.conf //啟動redis
  pkill redis //停止redis

  卸載redis:
    rm -rf /usr/local/redis //刪除安裝目錄
    rm -rf /usr/bin/redis-* //刪除所有redis相關命令腳本
    rm -rf /root/download/redis-4.0.4 //刪除redis解壓文件夾

 

讓外網能夠訪問redis
    a.配置防火牆: firewall-cmd --zone=public --add-port=6379/tcp --permanent(開放6379端口)
          systemctl restart firewalld(重啟防火牆以使配置即時生效)
     查看系統所有開放的端口:firewall-cmd --zone=public --list-ports

    
    b.此時 雖然防火牆開放了6379端口,但是外網還是無法訪問的,因為redis監聽的是127.0.0.1:6379,並不監聽外網的請求。
      (一)把文件夾目錄里的redis.conf配置文件里的bind 127.0.0.1前面加#注釋掉
      (二)命令:redis-cli連接到redis后,通過 config get daemonize和config get protected-mode 是不是都為no,如果不是,就用config set 配置名 屬性 改為no。

redis.conf文件
# bind 127.0.0.1
daemonize no
protected-mode no
# redis在最終目標上移動臨時數據庫文件時出錯 將dir ./ 修改為redis配置文件所在目錄:/usr/local/redis/etc/ 
# redis的持久化機制RDB先將內存中的數據集寫入臨時文件(temp-pid.rdb),寫成功后再替換之前的文件(dump.rdb),而現在寫入臨時文件出錯,數據保存不了,導致程序崩潰
dir /usr/local/redis/etc/

# RDB和AOF持久化
#redis有兩種持久化方式:一種是RDB持久化(原理是將Reids在內存中的數據庫記錄定時dump到磁盤上的RDB持久化)
#            一種是AOF持久化(原理是將Reids的操作日志以追加的方式寫入文件)
#RDB是默認開啟的,AOF需要修改配置文件來開啟
appendonly yes
appendfilename "appendonly.aof"

 


12493:M 27 Dec 19:42:23.095 * 10 changes in 300 seconds. Saving...
12493:M 27 Dec 19:42:23.095 * Background saving started by pid 7905
7905:C 27 Dec 19:42:23.106 # Error moving temp DB file temp-7905.rdb on the final destination crontab (in server root dir /etc): Operation not permitted
12493:M 27 Dec 19:42:23.196 # Background saving error

Redis持久化失敗報錯
錯誤:
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1'
to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [7679] 15 Jan 00:27:35.287 *
DB loaded from disk: 19.629 seconds

解決方案:在Linux系統設置一個參數(vm.overcommit_memory)即可解決。
  編輯 sysctl.conf 配置文件:vim /etc/sysctl.conf
    vm.overcommit_memory = 1
  使配置文件生效:sysctl -p
  最后重啟redis


配置之后的控制台日志打印如下:
[root@hz-auto-test-test5-02 etc]$/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
18679:C 27 Dec 19:52:06.898 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18679:C 27 Dec 19:52:06.898 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=18679, just started
18679:C 27 Dec 19:52:06.898 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 18679
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

18679:M 27 Dec 19:52:06.900 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18679:M 27 Dec 19:52:06.900 # Server initialized
18679:M 27 Dec 19:52:06.900 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
18679:M 27 Dec 19:52:06.900 * Ready to accept connections
18679:M 27 Dec 19:57:07.096 * 10 changes in 300 seconds. Saving...
18679:M 27 Dec 19:57:07.096 * Background saving started by pid 6584
6584:C 27 Dec 19:57:07.111 * DB saved on disk
6584:C 27 Dec 19:57:07.112 * RDB: 0 MB of memory used by copy-on-write
18679:M 27 Dec 19:57:07.196 * Background saving terminated with success
18679:M 27 Dec 20:01:20.187 * 10000 changes in 60 seconds. Saving...
18679:M 27 Dec 20:01:20.187 * Background saving started by pid 24886
24886:C 27 Dec 20:01:20.224 * DB saved on disk
24886:C 27 Dec 20:01:20.225 * RDB: 0 MB of memory used by copy-on-write
18679:M 27 Dec 20:01:20.287 * Background saving terminated with success

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM