1. 下载并安装 redis 2.6.16版
sudo mkdir /usr/local/src/Redis
cd /usr/local/src/Redis
sudo wget http://download.redis.io/releases/redis-2.6.16.tar.gz
tar -zxf redis-2.6.16.tar.gz
cd redis-2.6.16
sudo make
2. 检验编译是否 正确
sudo make test
[cpp] view plaincopyprint?
- <span style="font-size:14px;">sounion@ubuntu:/usr/local/src/redis-2.6.16$ make test
- cd src && make test
- make[1]: Entering directory `/usr/local/src/redis-2.6.16/src'
- You need tcl 8.5 or newer in order to run the Redis test
- make[1]: *** [test] Error 1
- make[1]: Leaving directory `/usr/local/src/redis-2.6.16/src'
- make: *** [test] Error 2
- </span>
安装 tcl 8.5 后可以解决上述编译错误。
[html] view plaincopyprint?
- <span style="font-size:14px;">sounion@ubuntu:/usr/local/src/redis-2.6.16$ sudo apt-get install tcl
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- The following package was automatically installed and is no longer required:
- tdb-tools
- Use 'apt-get autoremove' to remove them.
- The following extra packages will be installed:
- tcl8.5
- Suggested packages:
- tclreadline
- The following NEW packages will be installed:
- tcl tcl8.5
- 0 upgraded, 2 newly installed, 0 to remove and 565 not upgraded.
- Need to get 1,102 kB of archives.
- After this operation, 3,862 kB of additional disk space will be used.
- Do you want to continue [Y/n]? y
- Get:1 http://mirrors.sohu.com/ubuntu/ precise/main tcl8.5 amd64 8.5.11-1ubuntu1 [1,098 kB]
- Get:2 http://mirrors.sohu.com/ubuntu/ precise/main tcl all 8.5.0-2 [4,690 B]
- Fetched 1,102 kB in 3s (303 kB/s)
- Selecting previously unselected package tcl8.5.
- (Reading database ... 199470 files and directories currently installed.)
- Unpacking tcl8.5 (from .../tcl8.5_8.5.11-1ubuntu1_amd64.deb) ...
- Selecting previously unselected package tcl.
- Unpacking tcl (from .../archives/tcl_8.5.0-2_all.deb) ...
- Processing triggers for man-db ...
- Setting up tcl8.5 (8.5.11-1ubuntu1) ...
- update-alternatives: using /usr/bin/tclsh8.5 to provide /usr/bin/tclsh (tclsh) in auto mode.
- Setting up tcl (8.5.0-2) ...
- update-alternatives: using /usr/bin/tclsh-default to provide /usr/bin/tclsh (tclsh) in auto mode.
- Processing triggers for libc-bin ...
- ldconfig deferred processing now taking place
- </span>
3. 手工启动redis,测试redis是否运行正常
a)执行命令手工启动redis server: 该命令将使用缺省参数
./src/redis-server
b)检查进程是否有Redis了:ps aux | grep redis,可以看见进程列表中有一个叫“src/redis-server”的进程了
[html] view plaincopyprint?
- <span style="font-size:14px;">sounion@ubuntu:~/Desktop$ ps aux | grep redis
- sounion 65350 0.0 0.0 35024 1960 pts/1 Sl+ 07:28 0:00 ./src/redis-server
- sounion 65441 0.0 0.0 13584 916 pts/4 S+ 07:31 0:00 grep --color=auto redis
- </span>
c) 启动redis自带的客户端,进行测试:
# src/redis-cli -- 启动客户端并连接本地Redis
# set foo bar -- 提示 “ok”说明设置键值正常。
# get foo -- 能够提示返回正确的“bar”,说明运行正常。
# quit -- 退出客户端
[html] view plaincopyprint?
- <span style="font-size:14px;">sounion@ubuntu:/usr/local/src/redis-2.6.16$ ./src/redis-cli
- redis 127.0.0.1:6379> set foo bar
- OK
- redis 127.0.0.1:6379> get foo
- "bar"
- redis 127.0.0.1:6379> quit
- </span>
d) 如果希望使用自己的 redis.conf 配置文件,可以使用如下命令:
./src/redis-server /path/to/redis.conf
e) 如果希望额外覆盖 redis.conf 配置文件中的某些参数,可以直接在命令行中使用参数:
./src/redis-server /etc/redis.conf --loglevel debug
./sr/redis-server --port 9999 --slaveof 127.0.0.1 6379
4. 开启多个redis 服务器实例
一台Redis服务器,分成多个节点,每个节点分配一个端口(6380,6381…),默认端口是6379。
如果已经开启了 一个redis 服务实例,再次执行 ./src/redis-server 会产生如下错误
[html] view plaincopyprint?
- <span style="font-size:14px;">sounion@ubuntu:/usr/local/src/redis-2.6.16$ sudo ./src/redis-server
- [347] 19 Apr 07:47:00.859 # Warning: no config file specified, using the default config. In order to specify a config file use ./src/redis-server /path/to/redis.conf
- [347] 19 Apr 07:47:00.861 * Max number of open files set to 10032
- [347] 19 Apr 07:47:00.861 # Opening port 6379: bind: Address already in use
- </span>
每个节点对应一个Redis配置文件,如:redis6380.conf、redis6381.conf
cp redis.conf redis6380.conf
vi redis6380.conf
pidfile : pidfile/var/run/redis/redis_6380.pid
port 6380
logfile : logfile/var/log/redis/redis_6380.log
rdbfile : dbfilenamedump_6380.rdb
启动多个redis实例:
./src/redis-server /usr/local/redis/redis6380.conf
./src/redis-server /usr/local/redis/redis6381.conf
5、安装 redis-server
如果只是偶尔测试使用,可以使用 ./src/redis-server 方式直接启动。
但是如果是生产环境,需要进行 redis-server 安装部署。
命令:
cd /us/local/src/redis-2.6.16//utils
sudo ./install_server.sh
这个悲催的install_server.sh 命令必须在进入 utils 目录后再执行!
因为它的脚本写法以来一些相对目录路径!
[cpp] view plaincopyprint?
- sounion@ubuntu:/usr/local/src/redis-2.6.16/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/src/redis-2.6.16/src/redis-server
- s#^port [0-9]{4}$#port 6379#;s#^logfile .+$#logfile /var/log/redis_6379.log#;s#^dir .+$#dir /var/lib/redis/6379#;s#^pidfile .+$#pidfile /var/run/redis_6379.pid#;s#^daemonize no$#daemonize yes#;
- Copied /tmp/6379.conf => /etc/init.d/redis_6379
- Installing service...
- update-rc.d: warning: /etc/init.d/redis_6379 missing LSB information
- update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
- 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!
6、添加到开机启动
如果进行了上述生产环境安装,则不再需要手动添加开机启动。
否则,需要修改
sudo vim /etc/rc.local
添加 /usr/local/src/redis-2.6.16/src/redis-server redis.conf
7、修改redis配置
如果 vm.overcommit_memory=0, 则在低内存状态时,后台redis 保存服务可能会失败。
需要修改配置文件/etc/sysctl.conf 添加 vm.overcommit_memory=1 然后重新启动使之生效。
刷新配置生效 sysctl vm.overcommit_memory=1
补充介绍
如果内存情况比较紧张的话,需要设定内核参数: /proc/sys/vm/overcommit_memory
这里说一下这个配置的含义:
/proc/sys/vm/overcommit_memory
该文件指定了内核针对内存分配的策略,其值可以是0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存