linux下安装redis3.2
1.下载
选取当前最新版本3.2.1下载,上传到linux上,进行解压缩:
进入redis-3.2.1目录下,运行make进行安装编译:
[root@mongodb1 redis-3.2.1]# ls00-RELEASENOTES BUGS CONTRIBUTING COPYING deps INSTALL Makefile MANIFESTO README.md redis.conf runtest runtest-cluster runtest-sentinel sentinel.conf src tests utils
make需要安装编译器,默认为gcc.
[root@mongodb1 redis-3.2.1]# makecd src && make allmake[1]: Entering directory `/root/redis/redis-3.2.1/src'CC adlist.oCC quicklist.oCC ae.oIn file included from ae.c:53:ae_epoll.c: In function 'aeApiAddEvent':ae_epoll.c:75: warning: missing initializerae_epoll.c:75: warning: (near initialization for 'ee.data')ae_epoll.c: In function 'aeApiDelEvent':ae_epoll.c:92: warning: missing initializerae_epoll.c:92: warning: (near initialization for 'ee.data')CC anet.oanet.c: In function 'anetSockName':anet.c:640: warning: dereferencing pointer 's' does break strict-aliasing rulesanet.c:638: note: initialized from hereanet.c:644: warning: dereferencing pointer 's' does break strict-aliasing rulesanet.c:642: note: initialized from hereanet.c: In function 'anetPeerToString':anet.c:584: warning: dereferencing pointer 's' does break strict-aliasing rulesanet.c:582: note: initialized from hereanet.c:588: warning: dereferencing pointer 's' does break strict-aliasing rulesanet.c:586: note: initialized from hereanet.c: In function 'anetTcpAccept':anet.c:555: warning: dereferencing pointer 's' does break strict-aliasing rulesanet.c:553: note: initialized from hereanet.c:559: warning: dereferencing pointer 's' does break strict-aliasing rulesanet.c:557: note: initialized from hereCC dict.oCC server.oCC sds.oCC zmalloc.oCC lzf_c.oCC lzf_d.oCC pqsort.oCC zipmap.oCC sha1.oCC ziplist.oCC release.oCC networking.oCC util.oCC object.oCC db.oCC replication.oCC rdb.oCC t_string.oCC t_list.oCC t_set.oCC t_zset.oCC t_hash.oCC config.oCC aof.oCC pubsub.oCC multi.oCC debug.oCC sort.oCC intset.oCC syncio.oCC cluster.oCC crc16.oCC endianconv.oCC slowlog.oCC scripting.oCC bio.oCC rio.oCC rand.oCC memtest.oCC crc64.oCC bitops.oCC sentinel.oCC notify.oCC setproctitle.oCC blocked.oCC hyperloglog.oCC latency.oCC sparkline.oCC redis-check-rdb.oCC geo.oLINK redis-serverINSTALL redis-sentinelCC redis-cli.oLINK redis-cliCC redis-benchmark.oLINK redis-benchmarkINSTALL redis-check-rdbCC redis-check-aof.oLINK redis-check-aofHint: It's a good idea to run 'make test' ;)make[1]: Leaving directory `/root/redis/redis-3.2.1/src'
make完成之后,进行install,默认安装路径为/usr/local/bin下,这里我们把他安装目录放到/usr/local/redis下,使用PREFIX指定目录:
[root@mongodb1 redis-3.2.1]# mkdir /usr/local/redis[root@mongodb1 redis-3.2.1]# make PREFIX=/usr/local/redis installcd src && make installmake[1]: Entering directory `/root/redis/redis-3.2.1/src'Hint: It's a good idea to run 'make test' ;)INSTALL installINSTALL installINSTALL installINSTALL installINSTALL installmake[1]: Leaving directory `/root/redis/redis-3.2.1/src'
将redis可执行目录添加到环境变量中,编辑~/.bash_profile添加redis环境变量:
[root@mongodb1 bin]# cat ~/.bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=/usr/local/redis/bin:/usr/local/mongodb/bin:$PATH:$HOME/bin
3.创建redis服务
此时其实就可以启动redis服务了,例如:
% ./redis-server --port 9999 --slaveof 127.0.0.1 6379% ./redis-server /etc/redis/6379.conf --loglevel debug
但是我们一般还是把redis做成服务来启动,进入到utils目录,然后运行install_server.sh,运行这个会询问你几个问题,包括
指定redis的端口号
指定redis的配置文件
指定redis的日志文件
指定redis的数据目录文件
指定redis的可执行目录文件.
[root@mongodb1 utils]# ./install_server.shWelcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379]Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf]Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log] /data/redis/log/redis_6378.logPlease select the data directory for this instance [/var/lib/redis/6379] /data/redis/6379Please select the redis executable path [/usr/local/redis/bin/redis-server]Selected config:Port : 6379Config file : /etc/redis/6379.confLog file : /data/redis/log/redis_6378.logData dir : /data/redis/6379Executable : /usr/local/redis/bin/redis-serverCli Executable : /usr/local/redis/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort.Copied /tmp/6379.conf => /etc/init.d/redis_6379Installing service...Successfully added to chkconfig!Successfully added to runlevels 345!Starting Redis server...Installation successful!
完成之后,redis的服务就添加完毕了,服务名为redis_6379:
[root@mongodb1 init.d]# ls -l re*-rwxr-xr-x 1 root root 1714 Jul 1 11:13 redis_6379-rwxr-xr-x. 1 root root 1822 Jan 16 2013 restorecond
启动和关闭redis服务:
[root@mongodb1 init.d]# service redis_6379 statusRedis is running (19280)[root@mongodb1 init.d]# service redis_6379 stopStopping ...Redis stopped[root@mongodb1 init.d]# service redis_6379 startStarting Redis server...
使用redis-cli连接redis:
其实做完以上几步,我们已经可以正常使用redis了,下面我们来解析一下redis的启动停止过程.我们解析/etc/init.d/redis_6379文件:
#!/bin/sh#Configurations injected by install_server below....EXEC=/usr/local/redis/bin/redis-serverCLIEXEC=/usr/local/redis/bin/redis-cliPIDFILE=/var/run/redis_6379.pidCONF="/etc/redis/6379.conf"REDISPORT="6379"################ SysV Init Information# chkconfig: - 58 74# description: redis_6379 is the redis daemon.### BEGIN INIT INFO# Provides: redis_6379# Required-Start: $network $local_fs $remote_fs# Required-Stop: $network $local_fs $remote_fs# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Should-Start: $syslog $named# Should-Stop: $syslog $named# Short-Description: start and stop redis_6379# Description: Redis daemon### END INIT INFOcase "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;status)PID=$(cat $PIDFILE)if [ ! -x /proc/${PID} ]thenecho 'Redis is not running'elseecho "Redis is running ($PID)"fi;;restart)$0 stop$0 start;;*)echo "Please use start, stop, restart or status as first argument";;esac
可以发现,其实启动redis的语法就是:
/usr/local/redis/bin/redis-server /etc/redis/6379.conf
关闭redis的语法就是:
/usr/local/redis/bin/redis-server -p 6379 shutdown
检查redis是否运行,就是检查redis的pid文件下的进程是否存在.
查看redis的配置文件/etc/redis/6379.conf,里面有很多注释,去除注释:
[root@mongodb1 utils]# grep -E -v "^#" /etc/redis/6379.conf |sed '/^$/d'bind 127.0.0.1protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile /data/redis/log/redis_6379.logdatabases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /data/redis/6379slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100appendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
其中主要的参数:
bind:绑定的ip地址
port:监听端口号
pidfile:pid文件名
dir:数据文件目录
logfile:日志文件地址

浙公网安备 33010602011771号