1. 程式人生 > >linux安裝redis及解決無法遠端連線的問題

linux安裝redis及解決無法遠端連線的問題

首先建立安裝目錄# mkdir /usr/local/redis下載redis壓縮包wget http://download.redis.io/releases/redis-4.0.2.tar.gz解壓redis的壓縮檔案tar -zxvf redis-4.0.2.tar.gz進入安裝資料夾進行編譯[[email protected] redis]# cd redis-4.0.2/[[email protected] redis-4.0.2]# make編譯結束後進行安裝[[email protected] redis-4.0.2]# cd src/[[email protected] src]# make install
安裝成功

啟動redis伺服器,使用預設配置載入

[[email protected] src]# ./redis-server載入制定配置來啟動redis[[email protected] src]# ./redis-server /usr/local/redis/redis-4.0.2/redis.conf  
出現上圖說明安裝成功,但是有可能會導致遠端連線有問題。對於伺服器上的redis配置需要進行以下配置[[email protected] src]# vim ../redis.conf把protected-mode yes改為protected-mode no(在沒有密碼的情況下,關閉保護模式)
註釋掉bind 127.0.0.1     (取消繫結本地地址)把daemonize no改為daemonize yes   (是否為程序守護,關閉ssh視窗後即是否在後臺繼續執行)然後重啟,進行資料測試[[email protected] src]# ./redis-server /usr/local/redis/redis-4.0.2/redis.conf[[email protected] src]# ./redis-cli127.0.0.1:6379> set ball redOK127.0.0.1:6379> get ball"red"127.0.0.1:6379>其中可能遇到的問題有
1.Could not connect to Redis at 127.0.0.1:6379: Connection refused原因是:在安裝好redis擴充套件 嘗試連線redis時,客戶端打不開,原因是需要先開啟服務端,即需要先開啟redis-server,才能進行redis-cli操作2.redis基本操作./redis-server  //啟動redis伺服器./redis-server /usr/local/redis/redis-4.0.2/redis.conf  //啟動伺服器時載入指定配置./redis-cli -h 127.0.0.1 -p 6379  //操作埠號為6379的伺服器./redis-cli -h 127.0.0.1 -p 6379 -a root //當有密碼時,開啟redis的命令3.如何設定redis密碼[[email protected] src]# vim ../redis.conf把requirepass foobared的註釋去掉,將foobared改為需要的密碼把protected-mode no改為protected-mode yes(在有密碼的情況下,開啟保護模式)4.如何登入、退出redis//登入[[email protected] src]# ./redis-server /usr/local/redis/redis-4.0.2/redis.conf14405:C 13 Apr 15:16:49.806 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo14405:C 13 Apr 15:16:49.806 # Redis version=4.0.2, bits=64, commit=00000000,  modified=0, pid=14405, just started14405:C 13 Apr 15:16:49.806 # Configuration loaded[[email protected] src]# ./redis-cli [[email protected] src]#  ps -ef | grep redisroot     13437     1  0 14:17 ?        00:00:01 ./redis-server *:6379root     13825  8950  0 14:45 pts/2    00:00:00 grep --color=auto redis//退出[[email protected] src]# ./redis-cli shutdown[[email protected] src]# ps -ef|grep redis  //檢視狀態root     14379 14246  0 15:15 pts/0    00:00:00 grep --color=auto redis5.有密碼之後如何登入、退出redis[[email protected] src]# ./redis-cli -h 127.0.0.1 -p 6379 -a root  //ip和埠號選填  auth必填[[email protected] src]# ./redis-cli -h 127.0.0.1 -p 6379 -a root shutdown //ip和埠號選填  auth必填6.linux伺服器端可以連線而遠端無法連線的原因首先# ps -ef|grep redis檢視6379前面是否為*  若為127.0.0.1則被限制在本地訪問在redis-conf註釋掉bind 127.0.0.1     (取消繫結本地地址)若還不能訪問  則開啟制定埠號或者關閉防火牆,在這採用關閉防火牆,由於是centos7,防火牆進行了升級systemctl stop firewalld.service           #停止firewallsystemctl disable firewalld.service     #禁止firewall開機啟動