wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
前面3步應該沒有問題,主要的問題是執行make的時候,出現了異常。
異常一:
make[2]: cc: Command not found
異常原因:沒有安裝gcc
解決方案:yum install gcc-c++
異常二:
zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory
異常原因:一些編譯依賴或原來編譯遺留出現的問題
解決方案:make distclean。清理一下,然後再make。
在make成功以後,需要make test。在make test出現異常。
異常一:
couldn't execute "tclsh8.5": no such file or directory
異常原因:沒有安裝tcl
解決方案:yum install -y tcl。
在make成功以後,會在src目錄下多出一些可執行文件:redis-server,redis-cli等等。
方便期間用cp命令複製到usr目錄下運行。
cp redis-server /usr/local/bin/
cp redis-cli /usr/local/bin/
然後新建目錄,存放配置文件
mkdir /etc/redis
mkdir /var/redis
mkdir /var/redis/log
mkdir /var/redis/run
mkdir /var/redis/6379
在redis解壓根目錄中找到配置文件模板,複製到如下位置。
cp redis.conf /etc/redis/6379.conf
通過vim命令修改
daemonize yes
pidfile /var/redis/run/redis_6379.pid
logfile /var/redis/log/redis_6379.log
dir /var/redis/6379
最後運行redis:
$ redis-server /etc/redis/6379.conf
c調用redis例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis.h>
int main(int argc, char **argv) {
unsigned int j;
redisContext *c;
redisReply *reply;
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
int port = (argc > 2) ? atoi(argv[2]) : 6379;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout(hostname, port, timeout);
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\n", c->errstr);
redisFree(c);
} else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
/* PING server */
reply = (redisReply*)redisCommand(c,"PING");
printf("PING: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key */
reply = (redisReply*)redisCommand(c,"SET %s %s", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key using binary safe API */
reply = (redisReply*)redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);
/* Try a GET and two INCR */
reply = (redisReply*)redisCommand(c,"GET foo");
printf("GET foo: %s\n", reply->str);
freeReplyObject(reply);
reply = (redisReply*)redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* again ... */
reply = (redisReply*)redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* Create a list of numbers, from 0 to 9 */
reply = (redisReply*)redisCommand(c,"DEL mylist");
freeReplyObject(reply);
for (j = 0; j < 10; j++) {
char buf[64];
snprintf(buf,64,"%d",j);
reply = (redisReply*)redisCommand(c,"LPUSH mylist element-%s", buf);
freeReplyObject(reply);
}
/* Let's check what we have inside the list */
reply = (redisReply*)redisCommand(c,"LRANGE mylist 0 -1");
if (reply->type == REDIS_REPLY_ARRAY) {
for (j = 0; j < reply->elements; j++) {
printf("%u) %s\n", j, reply->element[j]->str);
}
}
freeReplyObject(reply);
/* Disconnects and frees the context */
redisFree(c);
return 0;
}
使用Redis啓動腳本設置開機自啓動
啓動腳本
推薦在生產環境中使用啓動腳本方式啓動redis服務。啓動腳本 redis_init_script
位於位於Redis的 /utils/
目錄下。
#大致瀏覽下該啓動腳本,發現redis習慣性用監聽的端口名作爲配置文件等命名,我們後面也遵循這個約定。 #redis服務器監聽的端口 REDISPORT=6379 #服務端所處位置,在make install後默認存放與`/usr/local/bin/redis-server`,如果未make install則需要修改該路徑,下同。 EXEC=/usr/local/bin/redis-server #客戶端位置 CLIEXEC=/usr/local/bin/redis-cli #Redis的PID文件位置 PIDFILE=/var/run/redis_${REDISPORT}.pid #配置文件位置,需要修改 CONF="/etc/redis/${REDISPORT}.conf"
配置環境
1. 根據啓動腳本要求,將修改好的配置文件以端口爲名複製一份到指定目錄。需使用root用戶。
mkdir /etc/redis cp redis.conf /etc/redis/6379.conf
2. 將啓動腳本複製到/etc/init.d目錄下,本例將啓動腳本命名爲redisd(通常都以d結尾表示是後臺自啓動服務)。
cp redis_init_script /etc/init.d/redisd
3. 設置爲開機自啓動
此處直接配置開啓自啓動
chkconfig redisd on
將報錯誤: service redisd does not support chkconfig
在啓動腳本開頭添加如下兩行註釋以修改其運行級別:
#!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database #
再設置即可成功。
#設置爲開機自啓動服務器 chkconfig redisd on #打開服務 service redisd start #關閉服務 service redisd stop