天天看點

redis+最新版本+linux,ubuntu 17.04安裝最新版本redis

記錄使用apt-get在ubuntu server上安裝最新版本Redis的過程。在ubuntu安裝redis最簡單的方式,就是使用apt-get

sudo apt-get install redis-server

但是這個方式有個問題,就是倉庫中的redis-server很可能不是最新的版本。

在之前的Blog裡面,因為使用這個原因,我采用了手工安裝的方式。

今天在準備另外一個環境時,想了想apt-get應該更友善一些,隻要倉庫中的redis-server版本足夠新就好了。

準備工作

要拿到最新的redis-server版本,就必須将redis的倉庫加入到源。

ubuntu搜尋ppa的位址

https://launchpad.net/ubuntu/+ppas

https://launchpad.net/ubuntu/+ppas?name_filter=redis-server

方法有兩種:

方式一:修改source檔案 (預設位址:/etc/apt/resources.list)

deb http://ppa.launchpad.net/chris-lea/redis-server/ubuntu trusty main

deb-src http://ppa.launchpad.net/chris-lea/redis-server/ubuntu trusty main

方式二:用add-apt-repository指令

sudo add-apt-repository  ppa:chris-lea/redis-server

這個方式無疑要友善很多。

不過 add-apt-repository 指令一般系統是沒有自帶的,是以還需要自己安裝一下。

這個指令的安裝有點麻煩,ubuntu不同版本中這個指令的安裝方式不同:

對于12.04以及以下版本,需要安裝python-software-properties

sudo apt-get install python-software-properties

對于12.10以及以上版本,需要安裝software-properties-common

sudo apt-get install software-properties-common

比較爽快而無需費腦的方法是兩個都安裝一下。

安裝

首次安裝Redis

安裝過程簡單,update再install就好了,加上前面準備add-apt-repository,指令依次如下:

sudo apt-get install -y python-software-properties

sudo apt-get install software-properties-common

sudo add-apt-repository  ppa:chris-lea/redis-server

sudo apt-get update

sudo apt-get install -y redis-server

使用下面的源

Adding this PPA to your system

You can update your system with unsupported packages from this untrusted PPA by adding ppa:chris-lea/redis-server to your system's Software Sources. (Read about installing)

https://launchpad.net/~chris-lea/+archive/ubuntu/redis-server

sudo add-apt-repository ppa:chris-lea/redis-server

sudo apt-get update

如果是第一次用apt-get安裝redis-server,那麼這樣就搞定了。

更新舊版redis

如果之前已經用apt-get安裝過redis-server的舊版本,再執行apt-get install時就有可能遇到問題。

我遇到的錯誤資訊如下:

[email protected]:~$ sudo apt-get install -y redis-server

[sudo] password for zilaike:

Reading package lists... Done

Building dependency tree

Reading state information... Done

redis-server is already the newest version (2:3.2.1-1).

The following packages were automatically installed and are no longer required:

bbswitch-dkms dkms iucode-tool lib32gcc1 libc6-i386 libvdpau1 libxnvctrl0

linux-headers-4.10.0-19 linux-headers-4.10.0-19-generic

linux-image-4.10.0-19-generic linux-image-extra-4.10.0-19-generic

mesa-vdpau-drivers nvidia-prime nvidia-settings python3-pyinotify

screen-resolution-extra vdpau-driver-all xserver-xorg-legacy

Use 'sudo apt autoremove' to remove them.

0 upgraded, 0 newly installed, 0 to remove and 116 not upgraded.

為了解決問題,決定先uninstall掉老版本的redis

apt-get remove redis-server

apt-get autoremove

再次執行apt-get install就可以順利安裝了

設定開機啟動:

systemctl enable redis-server.service

(發現并不生效,systemctl指令的一個bug,需要用下載下傳源碼包編譯的方式才能開機啟動!源碼包裝完了啥也不用幹就開機啟動了,另外如果給redis增加了登陸密碼,需要把redis_6379這個配置檔案的關機指令修改一下,增加密碼參數,稍後會有說明)

systemctl start redis-server.service(啟動指令可以生效)

編譯安裝的方法

1].Redis源碼擷取

1、進入Redis官網擷取Redis最新穩定版下載下傳位址

2、通過wget指令下載下傳 Redis 源代碼。

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

2].Redis編譯

1、通過tar -xzvf redis-4.0.2.tar.gz指令解壓下載下傳Redis源碼壓縮包redis-4.0.2.tar.gz;

2、編譯Redis。通過cd redis-4.0.2/進入Redis源碼目錄内,執行make編譯Redis;

注意:make指令執行完成編譯後,會在src目錄下生成6個可執行檔案,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel。

3].Redis安裝配置

1、安裝Redis,執行 make install 會将make編譯生成的可執行檔案拷貝到/usr/local/bin目錄下;

2、執行 ./utils/install_server.sh 配置Redis配置之後Redis能随系統啟動。

關于ubuntu17.04安裝redis_6379.service後systemctl不能關閉,機器關閉緩慢等問題的解決方案

故事是這個樣子滴,我在ubuntu17.04上編譯安裝了redis,并用redis自帶配置工具配置了開機啟動,啟動很完美。不過關機時候卡半天才能關掉,一查資料發現應該是某個程式的程序沒關掉,ubuntu在等待關閉程序才能下一步。一想可能是最近安裝的redis啟動保護程序出的問題。

然後做實驗。看了看狀态,好啦。找到問題了。

/etc/init.d/redis_6379 stop

肯定是這玩意執行的關機操作,粘貼下來執行試了試,不停的等待。死循環。

無用的錯誤提示。單獨執行後發現,是沒有認證,也就是redis加了密碼,配置腳本檔案沒有加對應的密碼認證。于是問題就簡單了。

redis+最新版本+linux,ubuntu 17.04安裝最新版本redis

解決方案:

找到這個配置檔案 ,執行如下指令:

gedit /etc/init.d/redis_6379

redis+最新版本+linux,ubuntu 17.04安裝最新版本redis

敲過代碼的看到這個東西,猜也猜到啥情況了。無非就是開局聲明了幾個常量,然後下文中case when語句根據外部傳入的第一個參數$1,區分接下來的操作是start stop status 還是restart....

注意選中的這個stop。也就是關鍵點。這不就是redis-cli這個用戶端關閉伺服器嘛,沒密碼認證吧。在這個shutdown 前面加 -a yourpassword 就好了啦。不怕費事的也聲明個變量存密碼,偷懶的直接寫裡面就好啦。OK。大功告成。