ubuntu 安装redis实践
2013-05-09 15:40
148 查看
本文参考红薯的bloghttp://www.oschina.net/question/12_18065
目前最新版本为[b]tcl8.6.0-src.tar.gz[/b]
目前最新版本为redis-2.6.13.tar.gz
/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
一般操作系统默认是0
值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)
由于是在内网,红薯所说的对iptable的配置,我们就忽略吧,如果需要,请参考红薯的blog
会出现如下输出
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1634
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[2215] 09 May 15:24:51.164 # Server started, Redis version 2.6.13
[2215] 09 May 15:24:51.164 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[2215] 09 May 15:24:51.165 * The server is now ready to accept connections on port 6379
可以看到,由于没有对内核的内存分配测略做优化,redis会提示warning
我们根据提示操作一下
#echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
#sysctl vm.overcommit_memory=1
再次启动redis,就不会报warning了
[19550] 09 May 15:27:50.684 # Server started, Redis version 2.6.13
[19550] 09 May 15:27:50.684 * The server is now ready to accept connections on port 6379
补充:
redis默认是运行在console状态需要更改配置文件/etc/redis.conf把daemonize no 改成yes,重新启动
#update-rc.d redis-server defaults
脚本
redis 127.0.0.1:6379> set name wilelm
OK
redis 127.0.0.1:6379> get name
"wilelm"
测试ok
或者
# redis-cli shutdown
强制备份数据到磁盘,使用如下命令
redis-cli -p 6379 save
1.准备
1.1 下载最新版的tcl
http://www.tcl.tk/software/tcltk/download.html目前最新版本为[b]tcl8.6.0-src.tar.gz[/b]
1.2 下载最新版本的redis
http://code.google.com/p/redis/downloads/list目前最新版本为redis-2.6.13.tar.gz
2.安装
2.1 编译安装tcl
tar zxvf tcl8.6.0-src.tar.gz cd tcl8.6.0/unix ./configure #默认安装 make make test make install
2.2 编译安装redis
tar zxvf redis-2.6.13.tar.gz cd redis-2.6.13 make make test #如果不安装tcl,此步骤会报错 cp redis.conf /etc/ cd src cp redis-benchmark redis-cli redis-server /usr/bin/
3.配置优化
3.1 内存配置
设置内存分配策略(可选,根据服务器的实际情况进行设置)/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
一般操作系统默认是0
值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)
由于是在内网,红薯所说的对iptable的配置,我们就忽略吧,如果需要,请参考红薯的blog
3.2 启动redis
#redis-server /etc/redis.conf会出现如下输出
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1634
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[2215] 09 May 15:24:51.164 # Server started, Redis version 2.6.13
[2215] 09 May 15:24:51.164 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[2215] 09 May 15:24:51.165 * The server is now ready to accept connections on port 6379
可以看到,由于没有对内核的内存分配测略做优化,redis会提示warning
我们根据提示操作一下
#echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
#sysctl vm.overcommit_memory=1
再次启动redis,就不会报warning了
[19550] 09 May 15:27:50.684 # Server started, Redis version 2.6.13
[19550] 09 May 15:27:50.684 * The server is now ready to accept connections on port 6379
补充:
redis默认是运行在console状态需要更改配置文件/etc/redis.conf把daemonize no 改成yes,重新启动
3.3 开机自启动
把下面的脚本复制到/etc/init.d/redis-server#update-rc.d redis-server defaults
脚本
#!/bin/bash ### BEGIN INIT INFO # Provides: apache2 # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # X-Interactive: true # Short-Description: Start/stop apache2 web server ### END INIT INFO # # apache2 This init.d script is used to start apache2. # It basically just calls apache2ctl. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/redis-server NAME=redis-server DESC="Redis key-value db" test -x $DAEMON || exit 0 set -e startdaemon() { echo -n "Starting $DESC:" start-stop-daemon --start --verbose --exec $DAEMON /etc/redis.conf echo "$NAME start succ" } stopdaemon() { echo -n "Stoppping $DESC:" start-stop-daemon --stop --verbose --oknodo --name $NAME echo "$NAME stop succ" } daemonstatus() { echo -n "Checking $DESC status:" start-stop-daemon --status --verbose --name $NAME case $? in 0) echo "Programming is runnning" exit 0 ;; 1|3) echo "Programming is not running" exit 1 ;; *) echo "Unknow status" exit 2 esac } case "$1" in start) startdaemon ;; stop) stopdaemon ;; restart|force-reload) stopdaemon startdaemon ;; status) daemonstatus ;; *) name=/etc/init.d/$NAME echo "Usage: $name (start|stop|restart|force-reload)" exit 1 ;; esac exit 0
4.简单测试
4.1 redis命令行工具
#redis-cliredis 127.0.0.1:6379> set name wilelm
OK
redis 127.0.0.1:6379> get name
"wilelm"
测试ok
4.2 redis关闭
redis 127.0.0.1:6379> shutdown或者
# redis-cli shutdown
4.3 redis保存数据
redis服务关闭后,缓存数据会自动dump到硬盘上,硬盘地址为redis.conf中的配置项dbfilename dump.rdb所设定强制备份数据到磁盘,使用如下命令
redis-cli -p 6379 save
相关文章推荐
- Ubuntu下redis安装及其操作
- Ubuntu16.04下安装redis并实现helloworld
- ubuntu下安装tomcat(亲身实践,不知道是否适合大多数人)
- Ubuntu16.04 Redis的安装及设置redis开机启动
- 【Redis笔记】1.简介、安装启动(Ubuntu)
- 用uefi安装windows8.1和ubuntu16.04LTS双系统(成功实践)
- 在Ubuntu14.04下安装Redis
- Docker + Ubuntu 安装 nodejs,redis 生成新镜像
- ubuntu 安装redis
- Ubuntu中安装启用Redis以及对应PHP扩展安装
- 安装、配置文件-ubuntu 12 install redis-by小雨
- ubuntu 中安装 Redis
- Ubuntu下如何安装与运行Redis
- Redis进阶实践之一Redis在Linux安装
- Redis下载安装启动官方版(ubuntu)
- Ubuntu与Centos安装redis教程,及其设置开机自动启动
- linux(ubuntu)安装PHP7以及memcache、redis等扩展
- ubuntu环境下redis安装
- ubuntu下安装redis拓展、windows下安装redis拓展
- ubuntu下安装php redis