1. 程式人生 > >linux下搭建redis並解決無法連接redis的問題

linux下搭建redis並解決無法連接redis的問題

linu 啟動 str telnet 模式 itl 缺點 oob 2.0

首先是搭建redis,這個比較簡單。

1、檢查安裝依賴程序

yum install gcc-c++
yum install -y tcl
yum install wget

2、獲取安裝文件

wget http://download.redis.io/releases/
這裏面有很多版本,自己選擇需要的下載

3、解壓文件

自己新建一個目錄將redis解壓到裏面

tar -zxvf redis-3.2.01.tar.gz
mv redis-3.2.01 /usr/local/redis

4、進入目錄

cd /usr/local/redis

5、編譯安裝

make
make install

6、設置配置文件路徑

mkdir -p /etc/redis
cp redis.conf/etc/redis

7、修改配置文件

 redis.conf是redis的配置文件,redis.conf在redis源碼目錄。

 註意修改port作為redis進程的端口,port默認6379。如果需要搭建redis集群,千萬別忘了修改端口號。

redis有兩種啟動方式,直接運行bin/redis-server將以前端模式啟動,前端模式啟動的缺點是ssh命令窗口關閉則redis-server程序結束,不推薦使用此方法。如下圖:

技術分享圖片

後端模式啟動

修改redis.conf配置文件, daemonize yes 以後端模式啟動。推薦!

打開redis.conf,使用命令 :/ daemonize 快速查找到daemonize然後修改。

vi /etc/redis/redis.conf
僅修改: daemonize yes (no-->yes)

8、啟動

/usr/local/bin/redis-server /etc/redis/redis.conf

9、查看啟動

ps -ef | grep redis 

10、使用客戶端

redis-cli
>set name david
OK
>get name
"david"

11.關閉客戶端

redis-cli shutdown

12、開機啟動配置

echo "/usr/local/bin/redis-server /etc/redis/redis.conf &" >> /etc/rc.local

開機啟動要配置在 rc.local 中,而 /etc/profile 文件,要有用戶登錄了,才會被執行。

13、設置密碼

因為這是給局域網內的很多人使用,所以設置一個訪問密碼很有必要。

修改redis.conf文件配置

使用命令 :/ requirepass 快速查找到 # requirepass foobared 然後去掉註釋,這個foobared改為自己的密碼。然後wq保存。

14、重啟redis

sudo service redis restart 這個時候嘗試登錄redis,發現可以登上,但是執行具體命令是提示操作不允許

  1. redis-cli -h 127.0.0.1 -p 6379
  2. redis 127.0.0.1:6379>
  3. redis 127.0.0.1:6379> keys *
  4. (error) ERR operation not permitted

嘗試用密碼登錄並執行具體的命令看到可以成功執行

  1. redis-cli -h 127.0.0.1 -p 6379 -a password
  2. redis 127.0.0.1:6379> keys *
  3. 1) "myset"
  4. 2) "mysortset"
  5. redis 127.0.0.1:6379> select 1
  6. OK

如果是自己在本機上使用現在已經可以了,因為我這是局域網內提供給大家使用的所以還需要最後的配置。

當時修改開發的配置文件後,啟動項目報錯。

org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:162) ~[spring-data-redis-1.5.0.RELEASE.jar:1.5.0.RELEASE]
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251) ~[spring-data-redis-1.5.0.RELEASE.jar:1.5.0.RELEASE]
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58) ~[spring-data-redis-1.5.0.RELEASE.jar:1.5.0.RELEASE]
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128) ~[spring-data-redis-1.5.0.RELEASE.jar:1.5.0.RELEASE]

打開cmd 然後使用 telnet ip 端口 來ping 配置的redis(要保證redis已啟動),發現無法ping通。

這是因為在redis.conf中有個配置 bind 127.0.0.1 這個是默認只有本機訪問,把這個註釋掉就好了,註釋以後查看redis進程就變為下面這樣:

[root@localhost redis]# ps -ef | grep redis
root 5655 1 0 11:40 ? 00:00:23 ./redis-server *:6379
root 21184 18040 0 17:33 pts/1 00:00:00 grep --color=auto redis

這個*號就表示允許其它用戶訪問了。然後在用打開本機的 cmd使用 telnet ip 端口 就能ping通了。

以上是全部內容,不足之處歡迎指出,互相交流才有進步!

linux下搭建redis並解決無法連接redis的問題