Linux环境下redis安装与配置
Linux环境下redis安装与配置
1、 下载redis
下载地址:http://redis.io/
根据需要选择合适的redis版本和符合系统配置的包。
这边以redis-3.2.3.tar.gz 为例,(以上传到云盘上地址如下:http://pan.baidu.com/s/1hs0mIQK)先将redis包通过FTP工具(如:FileZilla等)上传到Linux服务器上(目录为:/usr/local/soft)
2、 安装redis
[root@iZ23l73bebkZ ~]# cd /usr/local/soft/
[root@iZ23l73bebkZ soft]# ll
total 1508
-rw-r--r-- 1 root root 1541401 Sep 22 11:39 redis-3.2.3.tar.gz
[root@iZ23l73bebkZ soft]# tar -zxvf redis-3.2.3.tar.gz
redis-3.2.3/utils/hyperloglog/hll-gnuplot-graph.rb
redis-3.2.3/utils/install_server.sh
redis-3.2.3/utils/lru/
redis-3.2.3/utils/lru/README
redis-3.2.3/utils/lru/test-lru.rb
redis-3.2.3/utils/redis-copy.rb
redis-3.2.3/utils/redis-sha1.rb
redis-3.2.3/utils/redis_init_script
redis-3.2.3/utils/redis_init_script.tpl
redis-3.2.3/utils/releasetools/
redis-3.2.3/utils/releasetools/01_create_tarball.sh
redis-3.2.3/utils/releasetools/02_upload_tarball.sh
redis-3.2.3/utils/releasetools/03_test_release.sh
redis-3.2.3/utils/releasetools/04_release_hash.sh
redis-3.2.3/utils/speed-regression.tcl
redis-3.2.3/utils/whatisdoing.sh
[root@iZ23l73bebkZ soft]# cd redis-3.2.3/
[root@iZ23l73bebkZ redis-3.2.3]# make && make install
CC scripting.o
CC bio.o
CC rio.o
CC rand.o
CC memtest.o
CC crc64.o
CC bitops.o
CC sentinel.o
CC notify.o
CC setproctitle.o
CC blocked.o
CC hyperloglog.o
CC latency.o
CC sparkline.o
CC redis-check-rdb.o
CC geo.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
CC redis-check-aof.o
LINK redis-check-aof
Hint: It's a good idea to run 'make test' 😉
make[1]: Leaving directory `/usr/local/soft/redis-3.2.3/src'
cd src && make install
make[1]: Entering directory `/usr/local/soft/redis-3.2.3/src'
Hint: It's a good idea to run 'make test' 😉
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/soft/redis-3.2.3/src'
3、调整内存
/etc/sysctl.conf
添加
vm.overcommit_memory=1
刷新配置使之生效
sysctl vm.overcommit_memory=1
如果内存情况比较紧张的话,需要设定内核参数:
echo 1 > /proc/sys/vm/overcommit_memory
这里说一下这个配置的含义:
/proc/sys/vm/overcommit_memory
该文件指定了内核针对内存分配的策略,其值可以是0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
设置可选部分:拷贝文件
cp redis.conf /etc/ #这个文件时redis启动的配置文件
cp redis-benchmark redis-cli redis-server /usr/bin/ #这个倒是很有用,这样就不用再执行时加上./了,而且可以在任何地方执行
[root@iZ23l73bebkZ redis-3.2.3]# cp redis.conf /etc/
[root@iZ23l73bebkZ redis-3.2.3]# cd src/
[root@iZ23l73bebkZ src]# ll
[root@iZ23l73bebkZ src]# cp redis-benchmark redis-cli redis-server /usr/bin/
3、 编辑redis.conf
daemonize yes #转为守护进程,否则启动时会每隔5秒输出一行监控信息
save 60 1000 #减小改变次数,其实这个可以根据情况进行指定
maxmemory 1024000000 #分配256M内存
[root@iZ23l73bebkZ redis-3.2.3]# vim redis.conf
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised no
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
5、服务管理
5.1 启动服务
redis-server redis.conf #启动服务
5.2 存储值
redis-cli set name zhoufeng redis-cli get name
5.3 关闭服务
redis-cli shutdown
5.3.1 如果端口变化可以指定端口:
redis-cli -p 6380 shutdown