https://www.cnblogs.com/herblog/p/9305668.html
1.基礎(chǔ)知識(shí)
redis是用C語言開發(fā)的一個(gè)開源的高性能鍵值對(duì)(key-value)數(shù)據(jù)庫奏路。它通過提供多種鍵值數(shù)據(jù)類型來適應(yīng)不同場(chǎng)景下的存儲(chǔ)需求拴魄,目前為止redis支持的鍵值數(shù)據(jù)類型如下字符串邮屁、列表(lists)、集合(sets)翘瓮、有序集合(sorts sets)剑鞍、哈希表(hashs)
2.redis的應(yīng)用場(chǎng)景
緩存(數(shù)據(jù)查詢翔冀、短連接摊阀、新聞內(nèi)容、商品內(nèi)容等等)吭从。(最多使用)
分布式集群架構(gòu)中的session分離番刊。
聊天室的在線好友列表。
任務(wù)隊(duì)列影锈。(秒殺芹务、搶購、12306等等)?
應(yīng)用排行榜鸭廷。?
網(wǎng)站訪問統(tǒng)計(jì)枣抱。?
數(shù)據(jù)過期處理(可以精確到毫秒)
3.安裝redis
下面介紹在CentOS環(huán)境下,Redis的安裝與部署辆床,使用redis-3.0穩(wěn)定版佳晶,因?yàn)閞edis從3.0開始增加了集群功能。
- 可以通過官網(wǎng)下載 地址:http://download.redis.io/releases/redis-3.0.0.tar.gz
- 使用linux wget命令
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
步驟如下:
將redis-3.0.0.tar.gz拷貝到/usr/local下讼载,然后解壓
> cp redis-3.0.0.rar.gz /user/local
>
> tar -zxvf redis-3.0.0.tar.gz
由于Redis是用C語言編寫轿秧,所以編譯時(shí)需要gcc
> `yum install gcc -y`
進(jìn)入解壓后的目錄進(jìn)行編譯中跌,指定目錄安裝 如 /usr/local/redis
cd /usr/local/redis-3.0.0make PREFIX=/usr/local/redis install
可能報(bào)如下錯(cuò)誤:
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 `/data0/src/redis-2.6.2/src’
make: *** [all] Error 2
原因分析
在README中有這么一段話:
Allocator
————Selecting a non-default memory allocator when building Redis is done by setting the
MALLOC
environment variable. Redis is compiled and linked against libc malloc by default, with the exception of jemalloc being the default on Linux systems. This default was picked because jemalloc has proven to have fewer fragmentation problems than libc malloc.To force compiling against libc malloc, use:
% make MALLOC=libcTo compile against jemalloc on Mac OS X systems, use:
% make MALLOC=jemalloc
意思是說關(guān)于分配器allocator, 若有MALLOC 這個(gè) 環(huán)境變量菇篡, 會(huì)有用這個(gè)環(huán)境變量的 去建立Redis漩符。
而且libc 并不是默認(rèn)的分配器, 默認(rèn)是 jemalloc, 因?yàn)?jemalloc 被證明有比libc更少的 fragmentation problems 驱还。
但是如果你又沒有jemalloc 而只有 libc 當(dāng)然 make 出錯(cuò)嗜暴。
所以在編譯的時(shí)候需要加一個(gè)參數(shù),即:MALLOC=libc
解決辦法
make MALLOC=libc
綜上议蟆,執(zhí)行如下命令完成安裝:
make PREFIX=/usr/local/redis MALLOC=libc install
4.配置Redis
redis.conf是redis的配置文件闷沥,redis.conf在redis源碼目錄。
拷貝配置文件到安裝目錄下
進(jìn)入源碼目錄咐容,里面有一份配置文件 redis.conf舆逃,然后將其拷貝到安裝路徑下
> cd /usr/local/redis
>
> cp /usr/local/redis-3.0.0/redis.conf /usr/local/redis/bin
>
> cd /usr/local/redis/bin
進(jìn)入安裝目錄bin下,此時(shí)的目錄結(jié)構(gòu)是這樣的
redis-benchmark redis性能測(cè)試工具`
redis-check-aof AOF文件修復(fù)工具
redis-check-rdb RDB文件修復(fù)工具
redis-cli redis命令行客戶端
redis.conf redis配置文件
redis-sentinal redis集群管理工具
redis-server redis服務(wù)進(jìn)程
5.啟動(dòng)Redis
1.前端模式啟動(dòng)
直接運(yùn)行 ./redis-server
將以前端模式啟動(dòng)戳粒,前端模式啟動(dòng)的缺點(diǎn)是ssh命令窗口關(guān)閉則redis-server程序結(jié)束路狮,故不推薦使用此方法。
2.后端模式啟動(dòng)
修改redis.conf配置文件享郊, daemonize yes 以后端模式啟動(dòng)
vim /usr/local/redis/bin/redis.conf
執(zhí)行如下命令啟動(dòng)redis:
cd /usr/local/redis/bin./redis-server ./redis.conf
連接redis:
`<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
5.關(guān)閉redis
強(qiáng)行終止redis進(jìn)程可能會(huì)導(dǎo)致redis持久化數(shù)據(jù)丟失。
正確停止Redis的方式應(yīng)該是向Redis發(fā)送SHUTDOWN命令孝鹊,
命令為:
> cd /usr/local/redis
> ./bin/redis-cli shutdown
強(qiáng)行終止redis
pkill redis-server
讓redis開機(jī)自啟
> vim /etc/rc.local
> //添加
> /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-conf
至此redis完成安裝炊琉。
修改密碼和默認(rèn)端口
找到redis配置文件:*.conf,找到port修改為你想要的端口
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6380
# 增加密碼
requirepass newpassword
連接測(cè)試
//開啟redis
$ redis-server /配置路徑/*.conf
//客戶端連接:指定端口
$ redis-cli -p 6380
127.0.0.1:6380> set key value
(error) NOAUTH Authentication required.
//因?yàn)樵O(shè)置了密碼又活,需要認(rèn)證
127.0.0.1:6380> auth newpassword
OK
127.0.0.1:6380> set key value
OK
redis 6379端口不通解決方法
編輯配置文件vim /usr/local/redis/bin/redis.conf
bind 127.0.0.1修改為bind 0.0.0.0