redis的安裝與配置


 

Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作數據庫、緩存和消息中間件。 它支持多種類型的數據結構,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與范圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。 Redis 內置了 復制(replication)LUA腳本(Lua scripting), LRU驅動事件(LRU eviction)事務(transactions) 和不同級別的 磁盤持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)。

 

在傳統JavaWeb項目中,使用數據庫進行存儲數據,但是有一個致命的弊端,這些弊端主要來自於性能方面,由於數據庫持久化的數據主要面向的是磁盤,而磁盤讀寫的速度比較慢,在一般的系統中,由於不牽扯到高並發,其實沒有什么不同。但是在一些高並發的場景下尤其重要,比如搶紅包場景,多人同時訪問一個網頁的數據,成千上萬的人同時請求數據庫,可想而之,數據庫肯定是承受不了的,及其容易造成數據庫系統癱瘓。最終導致服務宕機。

 

為了應用這個高並發的場景,在傳統JavaWeb項目中於是引用了NoSql技術,NoSql工具也是一種簡易的數據庫,它主要是一種基於內存的數據庫。數據主要存儲在內存中。redis目前也是使用最為廣泛的NoSql之一。

 

一般redis在javaWeb中的應用,主要存在兩個主要的場景,一個是緩存常用的數據,另一個是在需要高度讀寫的場合使用它快速讀寫。

 

下面講解redis的安裝、配置、啟動

 

首先進入https://redis.io/官網下載redis最新版本,我下載的是redis-5.0.3.tar.gz

 

然后遠程登錄linux服務器,將下載好的文件拷貝到/usr/local路徑下

 

ssh 登錄linux服務器,並使用scp命令將本地文件上傳至服務器指定文件夾內

ssh xx@localhost

 

 

上傳本地文件到服務器指定文件夾

scp /Users/mac/soft/redis05.0.3.tar.gz  root@localhost:/usr/local/

/Users/mac/soft/redis05.0.3.tar.gz  本地文件路徑

root@localhost:/usr/local/ 服務器指定的文件夾

 

 

 

我用的是Mac版本FinalShell終端進行的遠程登錄,如圖

 

 

 

 

 

解壓redis

使用命令:tar xzvf redis-5.0.3.tar.gz  即可將文件解壓到當前目錄,可以得到redis-5.0.3文件夾

 

 

編譯redis,把源碼編譯成可執行文件

執行命令:cd redis-5.0.3   進入redis-5.0.3文件夾首先要進行make編譯,執行編譯命令:make,下圖是編譯成功的畫面

 

 

 

 

編譯過程中可能出現的錯誤

 

錯誤1:若出現如下提示,則說明未安裝gcc,使用命令安裝

1)yum –y install gcc

2)yum –y install gcc-c++

 

root@localhost redis-5.0.3]# make

cd src && make all

make[1]: Entering directory `/root/redis-5.0.3/src‘

    CC adlist.o

/bin/sh: cc: command not found

make[1]: *** [adlist.o] Error 127 make[1]: Leaving directory `/root/redis-5.0.3/src‘

make: *** [all] Error 2

 

 

 

錯誤2:若出現如下提示,則進入redis下的deps下的運行如下命令,就OK了

 

cd deps

make lua hiredis linenoise

 

cc: error: ../deps/hiredis/libhiredis.a: No such file or directory

cc: error: ../deps/lua/src/liblua.a: No such file or directory

cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory

make: * [redis-server] Error 1

 

 

錯誤3:若出現如下提示,推測是因為編譯庫的問題,執行一下命令進行編譯:

make MALLOC=libc

 

root@localhost redis-5.0.3]# make

cd src && make all

make[1]: Entering directory `/root/redis-5.0.3/src‘

    CC adlist.o

In file included from adlist.c:34:

zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory

zmalloc.h:55:2: error: #error "Newer version of jemalloc required" make[1]: *** [adlist.o] Error 1 make[1]: Leaving directory `/root/ redis-5.0.3/src‘

make: *** [all] Error 2

 

 

錯誤4:運行make test時若出現如下提示,需要安裝tcl

yum install tcl

 

 

[root@openstack-control redis-5.0.3]# make test
cd src && make test
make[1]: Entering directory /root/redis-5.0.3/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory/root/redis-5.0.3/src’
make: * [test] Error 2

 

 

 

安裝編譯后的redis

 

進入到redis-5.0.3同級目錄下,創建一個redis-bin文件夾,用於存放redis可執行文件

執行命令:mkdir redis-bin

進入到redis-5.0.3 目錄下,運行以下命令,將redis的可執行文件安裝到指定的目錄下

執行命令:make install PREFIX=/usr/local/redis-bin/

 

將/usr/local/redis-5.0.3/redis.conf文件拷貝一份至可執行文件的文件夾bin內, myredis.conf是拷貝並重命名為myredis.conf

執行命令:cd redis-bin/bin

執行命令:cp /usr/local/redis-5.0.3/redis.conf  myredis.conf

 

 

 

啟動redis-server

 進入到redis-bin/bin文件夾內:

執行命令:./redis-server

 

如果啟動成功,會出現以下畫面

 

 

 

如果看到上圖,說明啟動成功了,但是沒有在后台運行,只在前台運行,並且阻塞了線程。

 

啟動redis時注意有這么一句話:

Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf

警告:沒有指定配置文件,使用默認配置。要指定配置文件,請使用./redis-server /path/to/redis.conf

 

先關閉redis服務器,按Control+C

 

配置文件的修改

 

進入執行文件目錄

cd /usr/local/redis-bin/bin

先關閉redis服務器

./redis-cli shutdown

然后執行命令:vi myredis.conf 打開剛才拷貝並重命名的配置文件

:$跳至文件末尾

:set nu設置顯示行號

 

輸入/daemonize查找守護,找到這句話

135 # By default Redis does not run as a daemon. Use 'yes' if you need it.

136 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 默認情況下,Redis不作為守護進程運行。如果你需要,用“是”。

將daemonize 設置為yes 將以守護模式運行,后台運行,這樣就不會阻塞線程了

 

 

 

設置遠程訪問,允許所有計算機訪問

第一步:注釋掉#bind 127.0.0.1

# By default protected mode is enabled. You should disable it only if

# you are sure you want clients from other hosts to connect to Redis

# even if no authentication is configured, nor a specific set of interfaces

# are explicitly listed using the "bind" directive.

第二步:把protected-mode yes改為no關閉保護模式

 

 

 

設置redis登錄密碼:

/requirepass 查找

requirepass foobared 將本行取消注釋后,將默認的foobared密碼改為自己需要的密碼即可

 

 

按下esc后,輸入:wq確認保存退出

 

 

指定配置文件啟動redis-server

 

 進入到redis-bin/bin文件夾內:

執行命令:./redis-server myredis.conf

 

使用命令:

ps aux | grep redis

查看redis進程可以看到已經啟動成功

 

也可以使用命令:通過端口號查詢進程

[root@VM_16_16_centos bin]# lsof -i:6379

 

 

 

本機連接redis服務器:在bin目錄下執行命令:./redis-cli,默認的是連接本機127.0.0.1:6379

 

遠程連接redis服務器

[root@VM_16_16_centos bin]# redis-cli -h 192.168.1.14 -p 6379

 

查看數據時,需要驗證密碼,輸入命令:auth 密碼 后即可通過驗證

 

 

使用客戶端關閉服務器

 

第一種:已連接的情況下:直接shutdown

第二種:未連接的情況下:[root@VM_16_16_centos bin]# ./redis-cli shutdown(配置文件設置密碼后,此方法無法使用),可以使用:/redis-cli -a  密碼 shutdown

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM