linux上安裝redis的踩坑過程


   redis用處很廣泛,我不再啰嗦了,我按照網上教程想在linux上安裝下,開始了踩坑過程,網上買了一個linux centos7.3,滴滴雲的,巨坑無比啊,不建議大家用這家的! redis 為4.0,

1.登錄雲服務並下載安裝:

   首先登錄不是root用戶,需要切換下,執行sudo su切為root用戶,感覺這樣很不方便,於是咨詢下,回復是這樣的:

 

   (1) 然后換回root用戶了,開始wget下載,

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

提示沒有這個命令,好吧,開始安裝它yum -y install wget

  下載完后就解壓, 執行:tar xzf redis-4.0.11.tar.gz

  (2) 進入 cd redis-4.0.11,執行 make,此時有可能報錯: 

 gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c  類似的;那么再去安裝GCC環境, yum -y install gcc automake autoconf libtool make

 參考鏈接:https://blog.csdn.net/ygl6150373/article/details/50984420

 (3)安裝成功,開始啟動前准備,修改redis.conf里 bind的IP,直接注釋掉這一行,讓外網可以訪問,如果是3.2以后版本,還需要修改保護模式 protected-mode yes 改為no,注意修改完這個配置后,下次啟動要指定 redis.conf,不然不起作用!

2.開始啟動

  如果你linux沒什么問題可能就直接啟動成功了,但有些時候會報3個警告,比如我就碰到了,2.8不會,4.0會,猜想可能是4.0對內存要求高了

  第一個警告:The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

  意思是:TCP  backlog設置值,511沒有成功,因為 /proc/sys/net/core/somaxconn這個設置的是更小的128.

 

 臨時解決方法:(即下次啟動還需要修改此值)

 

 echo 511 > /proc/sys/net/core/somaxconn

 

 永久解決方法:(即以后啟動還需要修改此值)

 

 將其寫入/etc/rc.local文件中。

 當然上面這個解決辦法是網上的,我試了,直接改somaxconn下次啟動仍然這樣,而寫到rc.local我不知道怎么寫,直接寫也是沒用的,所以我又找到另外一個方法是可以的

在/etc/sysctl.conf中添加如下

net.core.somaxconn = 2048,此值根據你的內存自己寫定義,大於511即可

 

第二個警告: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.

意思是:overcommit_memory參數設置為0!在內存不足的情況下,后台程序save可能失敗。建議在文件 /etc/sysctl.conf 中將overcommit_memory修改為1。 

 這個警告的處理我忘記了這個是否有效,應該有效的

 

第三個警告:you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix thisissue 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 thesetting after a reboot. Redis must be restarted after THP is disabled.

意思是:你使用的是透明大頁,可能導致redis延遲和內存使用問題。執行 echo never > /sys/kernel/mm/transparent_hugepage/enabled 修復該問題。

臨時解決方法:

echo never > /sys/kernel/mm/transparent_hugepage/enabled。

永久解決方法:

將其寫入/etc/rc.local文件中。

 

 這個解決方案也是網上的,但是根本沒用,至少我這里是沒用的,所以我采取的是這個,修改etc/default/grub  里值,GRUB_CMDLINE_LINUX="console=tty0 crashkernel=auto console=ttyS0,115200 transparent_hugepage=never"

 

 在此行后面加了 transparent_hugepage=never,並執行,參考連接:https://blog.csdn.net/msdnchina/article/details/72968215

親測是有效的.

 

3.遠程連接

  終於順利啟動了, 啟動順序這樣: cd /redis/redis-4.0.9/src ,然后執行 ./redis-server  ../redis.conf,記住,一定要指定conf文件,不然上面修改IP和保護模式都沒用了.

不容易啊,現在開始在另外一個窗口里進入redis, cd /redis/redis-4.0.9/src,執行./redis-cli,進入redis,set個鍵取下看是否正常,然后開始進入遠程連接;

 我本地用的springboot,推薦大家使用,sprinboot自帶有springboot-redis功能,會自動加載redis配置文件,但它使用的redistemplate感覺並不好用,所以此時我用的還是jedis

 但一訪問就報錯,Caused by: java.net.SocketTimeoutException: connect timed out,然后我試着去關閉防火牆,如下:

  firewall-cmd --state #查看默認防火牆狀態(關閉后顯示notrunning,開啟后顯示running)

  systemctl stop firewalld.service #停止

  firewall systemctl disable firewalld.service #禁止firewall開機啟動

  init 6 #重啟虛擬機,然后再輸入第一條命令查看防火牆狀態

 在centos7.2里這樣操作防火牆是沒問題的,然后並沒什么用,還是time out,試了很多方法還是不行,本地可以Ping通,就是訪問不了,此時突然想起我用的坑貨雲了,那里是訪問白名單設置,

 

 

 我早就設置了6379端口的白名單,卻忘了綁定服務器,這里的DC2相當於阿里的ECS,結果一直報超時,無奈,改了這個之后,馬上就可以了.

然后又報下面的錯誤 :

JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error"異常

或者:Unable to validate object

或者可能會有下面大段的錯誤:Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

此時說明redis.conf里設置沒起作用,說明你啟動時忘了指定conf,於是按正確方法啟動  ./redis.server ../redis.conf,終於可以ping 通了

 

 

總結下:在安裝完redis后,修改bindIP,和保護模式,並修改服務器防火牆的端口訪問,在服務器上能訪問redis的情況下,如果遠程不能訪問時,請檢查下是否使用了雲服務器及雲服務器的IP及端口限制是否設置正常.

另外,還有其它設置,如是否使用密碼,超時等待時間,持久化策略等下次再更.

 

 

 

 

 


免責聲明!

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



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