Redis代码可以在Linux, OSX, OpenBSD, NetBSD, FreeBSD系统上进行部署,并且支持大小端CPU类型。学习环境采用了一台Ubuntu14.04 64位的系统,所有代码的学习是基于redis-3.0.7。
请从Redis官网http://redis.io,下载redis-3.0.7.tar.gz源代码
Redis代码编译
32位,64bit的系统需要安装32位系统的一些支持库
# apt-get install libc6-dev-i386 g++-multilib
# make distclean
# make 32bit
64位,64bit的系统,所以一把过。
# tar -zxvf redis-3.0.7.tar.gz
# cd redis-3.0.7
# make
# make test
Redis安装
这里我们学习和测试使用的是64位结果。直接进行安装,这里都采用默认配置。
# make install
cd utils
./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
You must run this script as root. Sorry!
daniel@ubuntu:~/redis-3.0.7/utils$ sudo ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Adding system startup for /etc/init.d/redis_6379 ...
/etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
/etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
/etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
/etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
/etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
/etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
/etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379
Success!
Starting Redis server...
Installation successful!
安装完成后,可以使用redis_6379脚本进行启动redis服务。当然如果需要每次随机启动redis,可以将以下脚本增加到rc.local等脚本中。
/etc/init.d/redis_6379
命令行客户端
服务启动后,可以采用redis-cli进行连接
% cd src
% redis-cli
redis> ping
PONG
redis> set foo bar
OK
redis> get foo
"bar"
redis> incr mycounter
(integer) 1
redis> incr mycounter
(integer) 2
redis>
其他注意事项
==》glibc, jemalloc, tcmalloc编译配置, make时添加MALLOC宏,默认是glibc,比如"make MALLOC=libc"
==》更多的编译输出,make时添加V宏,比如"make V=1"
==》自定义配置启动,比如"redis-server /path/to/redis.conf"
==》主从配置,比如"redis-server --port 9999 --slaveof 127.0.0.1 6379"
==》运行日志记录级别定义,比如"redis-server /etc/redis/6379.conf --loglevel debug"