1. 程式人生 > >linux下redis安裝、啟動與停止,redis做成服務

linux下redis安裝、啟動與停止,redis做成服務

如果的linux可以連結網路,那麼可以直接聯網下載。直接輸入程式碼:

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

我們會看到在該目錄下多了一個檔案,redis-3.0.2 ,

我們進入該目錄,然後編譯執行命令make,等待10左右,我們就會看到已經編譯完了,我們看到如下介面,說明我們安裝成功了。

    


測試

  1. # make test  
  1. yum install tcl  

錯誤1:

You need tcl 8.5 or newer in order to run the Redis test

解決:

# yum install tcl

錯誤2:

[exception]: Executing test client: NOREPLICAS Not enough good slaves to write..
NOREPLICAS Not enough good slaves to write.

......

Killing still running Redis server 63439
Killing still running Redis server 63486
Killing still running Redis server 63519
Killing still running Redis server 63546
Killing still running Redis server 63574
Killing still running Redis server 63591
I/O error reading reply

......

解決:

vim tests/integration/replication-2.tcl

- after 1000

+ after 10000

(其實就是將1000改為10000)

錯誤3:

[err]: Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
Replication not started.

解決:

遇見過一次,重試make test就ok了。

錯誤4:

[err]: Test replication partial resync: ok psync (diskless: yes, reconnect: 1) in tests/integration/replication-psync.tcl

解決:

vim tests/integration/replication-psync.tcl

- after 100

+ after 1000


make PREFIX=/usr/local/redis install    

會在/usr/local/redis下生產bin資料夾  bin下面有

redis-benchmark  redis效能檢測工具

 redis-check-rdb 檢查rbd日誌的工具

 redis-sentinel  Redis叢集的管理工具 監控多個master-slave叢集,發現master宕機後能進行自動切換
redis-check-aof  檢查aof日誌的 工具

redis-cli        redis-server

等檔案

三、設定服務

    可以看到如果我們啟動redis服務的話,每次都要進入到安裝目錄,這樣是不是很繁瑣,所以我們將redis做成一個服務,我們直接啟動。

(設定服務前如果redis服務在開著 要先關閉redis服務 不然後面生成不了redis-6379.pid,可以檢視redis服務程序 關閉殺死resid服務)

     首先將utils/redis_init_script檔案複製到/etc/init.d下,同時易名為redis。執行命令

     cp  /usr/local/redis-3.0.2/utils/redis_init_script   /etc/rc.d/init.d/redis

     然後在執行命令,vi  /etc/rc.d/init.d/redis,並在文件第二行新增# chkconfig: 2345 80 90。如果/usr/local/redis/bin中沒有redis-server和redis-cli檔案的話,我們要在我們的安裝目錄下複製一個過來。否則服務啟動時找不到redis-server。下圖第二個紅框所示。並且在第三個$EXEC $CONF 後邊加一個&,表示讓redis作為後臺服務啟動,否則該服務會獨佔輸入屏。


可以看到在/etc/init.d/redis檔案中有這麼一行:CONF="/etc/redis/${REDISPORT}.conf"。於是將redis配置檔案拷貝到/etc/redis/下 。 

mkdir /etc/rediscp/usr/local/redis-3.0.2/redis.conf /etc/redis/6379.conf

 完成上面的操作之後,即可註冊服務:

?
1chkconfig --add redis

     然後啟動redis服務

?
1service redis start

    這樣我們就將redis服務註冊好了,

如果service redis stop ,你會發現他提示/var/run/redis_6379.pid does not exist, process is not running  這說明在run下沒有這個檔案,我們進入到/var/run下發先真的沒有這個檔案,但是有redis.pid檔案。

     我們我們要將/etc/rc.d/init.d/redis 檔案中的redis_${REDISPORT}.pid修改為redis.pid


     這樣我們重啟服務後,在用該命令關閉就可以了。到這裡我們的redis服務就安裝好了,並做好了一個服務。

以上就是Linux下安裝Redis的全過程,希望對大家安裝Redis有所幫助。

java 客戶端連結不上redis解決方案

telnet 192.168.0.104 6379

發現埠不能拼通,但是linu下其他的埠能拼通,說明就是redis自身配置的問題了

看了下配置檔案,發現配置檔案綁定了本地ip,該配置會導致,其他ip登入被拒絕,所以我們把它註釋掉

這裡寫圖片描述

接下來再測試執行,發先已經連結上了,說明問題解決,但是又出現新的問題 :DENIED Redis is running in protected mode because protected mode is enabled…

這裡寫圖片描述

該問題說,redis目前處於受保護模式,不允許非本地客戶端連結,我們可以通過給redis設定密碼,然後客戶端連結的時候,寫上密碼就可以解決了

這裡寫圖片描述

配置redis的命令
config set requirepass 123 ->123是密碼

現在java客戶端通過配置主機ip號,密碼就能連結上了


redis關閉

到redis節點目錄下執行如下命令

redis-cli -p 埠號 shutdown

開配置檔案把下面對應的註釋掉

# bind 127.0.0.1 
  • 1

Redis預設不是以守護程序的方式執行,可以通過該配置項修改,使用yes啟用守護程序

daemonize yes
  • 1

保護模式

protected-mode no