1、下载安装包: wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
2、解压: tar axvf redis-2.6.13.tar.gz
3、进入文件夹:cd redis-2.6.13/
4、编译:make
5、编译安装: make install
6、进入源码文件夹: cd src
7、编译安装: sudo make install
8、回到上一级目录: cd ..
9、打开redis.conf文件: vim redis.conf
10、修改daemonize为yes (后台运行)之后保存关闭
11、将redis.conf拷贝到/etc目录下: sudo cp redis.conf /etc/
12、 在/etc/sysctl.conf中增加一行: vm.overcommit_memory=1
13、执行 sudo sysctl vm.overcommit_memory=1
14、至此已经安装好了,启动redis服务: sudo /usr/local/bin/redis-server /etc/redis.conf
15、查看redis-server是否启动: ps -e | grep redis-server
16、打开客户端: redis-cli -a icc_redis -p 6379
17、redis-server停止:/usr/local/bin/redis-cli -p 6379 shutdown
下面说用C++代码访问redis:
1、cd redis-2.6.13/deps/hiredis
2、编译: make
3、编译安装: sudo make install
4、sudo mkdir /usr/lib/hiredis
5、sudo cp libhiredis.so /usr/lib/hiredis/
6、sudo mkdir /usr/include/hiredis
7、sudo cp hiredis.h /usr/include/hiredis/
8、sudo ldconfig /usr/lib/
9、自己写一个testRedis.cpp:功能就是插name=MengLei
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
/***************************** * FileName: testRedis.cpp * Author: MengChunlei * CreateTime: 2014-6-3 ******************************/ #include <stdio.h> #include <stdlib.h> #include "/usr/include/hiredis/hiredis.h" int
main() { redisContext *c; redisReply *reply; struct
timeval timeout = {1, 500000}; c = redisConnectWithTimeout(( char *) "127.0.0.1" , 6379, timeout); if (c->err) { printf( "Connection error: %s\n" , c->errstr); exit(1); } reply = (redisReply*)redisCommand(c, "SET name MengLei" ); printf( "SET: %s\n" , reply->str); freeReplyObject(reply); } |
10、编译: g++ testRedis.cpp -o testRedis -lhiredis
11、运行: ./testRedis
12、查看是否插入成功:
linux下redis安装和C++连接,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/jianglangcaijin/p/3765959.html