下載最新穩定版本redis源碼包:
liang@bogon:~/redis3$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
liang@bogon:~/redis3$ ls
編譯安裝redis:
下載成功,解壓redis源碼包
liang@bogon:~/redis3$ tar zxvf redis-3.0.5.tar.gz
liang@bogon:~/redis3/redis-3.0.5$ ls
解壓縮後,進入redis源碼目錄
liang@bogon:~/redis3$ cd redis-3.0.5/
liang@bogon:~/redis3/redis-3.0.5$ ls
liang@bogon:~/redis3/redis-3.0.5$ make
liang@bogon:~/redis3/redis-3.0.5$ sudo make install
liang@bogon:~/redis3/redis-3.0.5$ make test
出錯提示:
You need tcl 8.5 or newer in order to run the Redis test
Makefile:211: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/home/liang/redis3/redis-3.0.5/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2
安裝tcl 8.5支持redis
liang@bogon:~/redis3/redis-3.0.5$ sudo apt-get install tcl
再次測試redis安裝是否成功
liang@bogon:~/redis3/redis-3.0.5$ make test
不再提示需要依賴,redis可以正常運行了
redis的配置文件在redis-3.0.5目錄下的redis.conf,不指定配置文件redis使用默認配置
在配置文件可以設置port、pidfile、daemonize、bind等等配置項
測試redis功能:
啓動redis服務器 liang@bogon:~/redis3/redis-3.0.5$ src/redis-server
啓動redis命令行
liang@bogon:~/redis3/redis-3.0.5$ redis-cli
設置一個鍵值
127.0.0.1:6379> set joy yes
查詢鍵值joy的值是否爲yes
127.0.0.1:6379> get joy
2、C客戶端連接redis
下載redis客戶端C語言源碼包:
安裝github下載工具
liang@bogon:~$ sudo apt-get install git
從github下載C客戶端源碼包
liang@bogon:~$ git clone https://www.github.com/redis/hiredis
編譯安裝redis客戶端:
下載後進入,hredis目錄
liang@bogon:~$ cd hiredis/
liang@bogon:~/hiredis$ make
liang@bogon:~/hiredis$ sudo make install
拷貝庫文件到系統默認的庫目錄
liang@bogon:~/hiredis$ sudo cp libhiredis.so /usr/lib
重新加載庫
sudo idconfig
進入examples目錄
liang@bogon:~/hiredis$ cd examples/
編譯生成一個客戶端
liang@bogon:~/hiredis/examples$ gcc example.c -o example -lhiredis
若提示一些如read.h等頭文件error,進入hiredis,執行下面命令,重新編譯
liang@bogon:~/hiredis$ sudo cp *.h /usr/include/
測試redis客戶端功能:
運行example
liang@bogon:~/hiredis/examples$ ./example
出現下面結果,即C客戶端操作redis成功
PING: PONG
SET: OK
SET (binary API): OK
GET foo: hello world
INCR counter: 1
INCR counter: 2
0) element-9
1) element-8
2) element-7
3) element-6
4) element-5
5) element-4
6) element-3
7) element-2
8) element-1
9) element-0
然後就可以開始真正的用C編寫操作redis數據庫了,最後附上example.c源碼,在hiredis目錄也有
#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 = redisCommand(c,"PING");
printf("PING: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key */
reply = redisCommand(c,"SET %s %s", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key using binary safe API */
reply = 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 = redisCommand(c,"GET foo");
printf("GET foo: %s\n", reply->str);
freeReplyObject(reply);
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* again ... */
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* Create a list of numbers, from 0 to 9 */
reply = redisCommand(c,"DEL mylist");
freeReplyObject(reply);
for (j = 0; j < 10; j++) {
char buf[64];
snprintf(buf,64,"%u",j);
reply = redisCommand(c,"LPUSH mylist element-%s", buf);
freeReplyObject(reply);
}
/* Let's check what we have inside the list */
reply = 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;
}