1. 程式人生 > >ubuntu上安裝redis

ubuntu上安裝redis

from my typora

redis

linux下redis安裝

下載

  • 首先到redis官網下載安裝包:

下載可選安裝包unstablestable版本

分別對應不穩定版和穩定版

在這裡插入圖片描述

編譯

然後執行tar -zxvf 命令

[email protected]:~/redis-5.0.0$ pwd
/home/lsnu/redis-5.0.0

進入資料夾進行make,編譯操作:前提要先安裝gcc

最後編譯完成之後,會產生
在這裡插入圖片描述

安裝

首先在usr/local下建立一個資料夾:

[email protected]:/$ cd /usr/local
[email protected]:/usr/local$ sudo
mkdir redis [email protected]:/usr/local$ ls bin etc games include lib man redis sbin share src

回到我們編譯(make)過的redis目錄下:

[email protected]:~/redis-5.0.0$ pwd
/home/lsnu/redis-5.0.0

然後安裝redis:

$ sudo make PREFIX=/usr/local/redis install
cd src && make install
make[1]: Entering directory '/home/lsnu/redis-5.0.0/src'
Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory '/home/lsnu/redis-5.0.0/src'

檢查是否安裝成功,是否存在bin目錄:

[email protected]:~/redis-5.0.0$ cd /usr/local/redis/
[email protected]:/usr/local/redis$ ls
bin

將redis原始碼(/home/lsnu/redis-5.0.0)中的配置檔案拷貝到我們安裝目錄(/usr/local/redis)下:

[email protected]:~/redis-5.0.0$ sudo cp ./redis.conf /usr/local/redis/
[email protected]:~/redis-5.0.0$ ll /usr/local/redis/
總用量 76
drwxr-xr-x  3 root root  4096 11月  9 18:11 ./
drwxr-xr-x 11 root root  4096 11月  9 18:02 ../
drwxr-xr-x  2 root root  4096 11月  9 18:06 bin/
-rw-r--r--  1 root root 62155 11月  9 18:11 redis.conf

啟動redis

前臺啟動

前臺啟動(不能保留程序,shell關閉就關閉)

[email protected]:~/redis-5.0.0$ sudo /usr/local/redis/bin/redis-server 

...
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 15502
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'           
...

後臺啟動

先關閉前臺啟動的redis:

ctrl + c

Redis is now ready to exit, bye bye…

首先要修改配置檔案(我們剛才拷貝到/usr/local/redis目錄下的):

[email protected]:/$ cd /usr/local/redis/
[email protected]:/usr/local/redis$ ls
bin  redis.conf
[email protected]:/usr/local/redis$ sudo vim redis.conf

# i 
# 修改其中的daemonize 從no 變成 yes
# daemonize no -> daemonize yes
# wq

後臺啟動redis:

[email protected]:/usr/local/redis$ pwd
/usr/local/redis

[email protected]:/usr/local/redis$ ./bin/redis-server ./redis.conf 
15545:C 09 Nov 2018 18:26:36.140 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15545:C 09 Nov 2018 18:26:36.140 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=15545, just started
15545:C 09 Nov 2018 18:26:36.140 # Configuration loaded

通過服務檢視redis是否啟動:

[email protected]:/usr/local/redis$ ps -ef | grep -i redis
lsnu     15546     1  0 18:26 ?        00:00:00 ./bin/redis-server 127.0.0.1:6379
lsnu     15552 15247  0 18:27 pts/14   00:00:00 grep --color=auto -i redis

以上第一個:15546表示我們系統中的redis的程序

第二個表示:一個使用者通過虛擬終端(pts)pts/14執行了grep --color=auto -i redis的命令

redis的埠是6379,通過埠檢視redis是否啟動:

[email protected]:/usr/local/redis$ sudo lsof -i:6379
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 15546 lsnu    6u  IPv4  53859      0t0  TCP localhost:6379 (LISTEN)

在我們的ubuntu上開啟6379埠:

[email protected]:/usr/local/redis$ sudo ufw allow 6379
防火牆規則已更新
規則已更新(v6)

停止redis

# redis自己的方式
/usr/local/redis/bin/redis-cli shutdown

# 或者:linux程序管理方式
pkill redis-server

開始使用

確認當前路徑:

[email protected]:/usr/local/redis$ pwd
/usr/local/redis 

簡單使用redis:

# 通過 bin下的redis-cli進入終端操作
[email protected]:/usr/local/redis$ ./bin/redis-cli 
# set key value 
127.0.0.1:6379> set name mzy
OK
# get key 
127.0.0.1:6379> get name
"mzy"
127.0.0.1:6379> 

退出:exit

總結

# 在redis安裝目錄的根目錄下
# 啟動redis
./bin/redis-server ./redis.conf
# 啟動客戶端
./bin/redis-cli
# 這樣直接操作的都是記憶體,不是資料庫,資料庫會慢半拍

# 測試資料庫(redis)是否連通
# 除了我們通過set key value,get key的方式可以測試
# 還可以ping,證明連通了
127.0.0.1:6379> ping
PONG

測試redis

set、get、del

127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name mzy
OK
127.0.0.1:6379> get name
"mzy"
127.0.0.1:6379> set name zxr
OK
127.0.0.1:6379> get name
"zxr"
127.0.0.1:6379> del name 
(integer) 1
127.0.0.1:6379> get name
(nil)

keys *:拿到所有鍵值對

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key1 aaa
OK
127.0.0.1:6379> set key2 bbb
OK
127.0.0.1:6379> set key3 ccc
OK
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key3"

redis 設定密碼

# pwd 為 /usr/local/redis 根目錄下
# 以下newPassword處為你要設定的新密碼
# 方式一:在redis命令列客戶端進行設定
127.0.0.1:6379> config set requirepass newPassword

# 在redis配置檔案,redis.conf中尋找到 requirepass foobared  
vim redis.conf -> esc -> /requirepass -> 
# 選擇一個合適的空白位置新增
requirepass newPassword


# 重啟服務
/usr/local/bin/redis-cli shutdown
/usr/local/bin/redis-server /usr/local/redis.conf

# 嘗試登陸
$ ./bin/redis-cli -h 127.0.0.1 -p 6379 
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.

# 加上密碼:可使用-a 或者 -u
$ ./bin/redis-cli -h 127.0.0.1 -p 6379 -a newPassword
127.0.0.1:6379> keys *
1) "name"
2) "x"
3) "num"
4) "name1"

# 以上的-h和-p可以不寫;不用專門指定IP和埠號(配置檔案中都是有的)
$ ./bin/redis-cli -a newPassword # -a或者-u

# 除了以上的登陸方式,還可以在進入客戶端命令列之後,再進行校驗
127.0.0.1:6379>auth newPassword # 同樣可以

# ------------------------------
# 如果Redis伺服器,使用了叢集。除了在master中配置密碼外,也需要在slave中進行相應配置。
# 在slave的配置檔案中找到如下行,去掉註釋並修改與master相同的密碼即可:
# masterauth master-password
masterauth newPassword

redis設定區域網或外網訪問

# 同樣在redis.conf下,找到 bind 127.0.0.1 將其修改為 0.0.0.0
vim redis.cnf -> esc -> /127.0.0.1 -> bind 0.0.0.0

redis 遇到的第一個問題

(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

redis預設是通過RDB方式進行持久化的,以上錯誤就是redis不能將資料持久化到外存中造成的錯誤!

(我猜測我出現這個原因是因為我在我的ubuntu上啟動我的redis的時候,沒有加上sudo,導致redis沒有許可權往硬碟上刷資料,我重啟加上sudo就好了)

網上的方法,如果你執意要使用,就是關閉這個提醒,掩耳盜鈴而已,但是如果你的資料不重要,沒有持久化的必要,也可以強行使用,拋棄持久化的功能。

執行:

127.0.0.1:6379> config set stop-writes-on-bgsave-error no