2017-02-16 15:01:50 点击量:135 标签: 收藏本文
下载地址:http://redis.io/download,下载最新文档版本。 本文使用的最新文档版本为 3.2.7,下载,解压缩和编译安装Redis:
稳定 Redis 3.2包含对API的重大更改和Redis的实现。
Redis 3.2发行说明
wget http://download.redis.io/releases/redis-3.2.7.tar.gz
tar xzf redis-3.2.7.tar.gz
cd redis-3.2.7
make
可手动拷贝redis-server、redis-cli、redis-check-aof、redis-check-dump等至/usr/local/bin目录下,也可执行make install,此处执行make install. 完成后可查看可查看,/usr/local/bin下已有这些文件 注意:若此时执行redis-server –v (查看版本命令),若提示redis-server command not found,则需要将/usr/local/bin目录加到环境变量,如何添加,此处不做详细介绍,可查看修改/etc/profile,(查看环境变量命令:echo $PATH)
可以看出默认端口是6379,并且现在的启动不是后台启动。
vi redis.conf 设置 daemonize no–>daemonize yes
[root@souyunku redis-3.2.7]# ./bin/redis-server ./redis.conf
8258:M 08 Feb 12:26:54.279 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8258
`-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' 8258:M 08 Feb 12:26:54.293 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 8258:M 08 Feb 12:26:54.293 # Server started, Redis version 3.2.7 8258:M 08 Feb 12:26:54.293 # 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. 8258:M 08 Feb 12:26:54.294 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 8258:M 08 Feb 12:26:54.294 * The server is now ready to accept connections on port 6379
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默认情况下,Redis不作为守护程序运行。 如果需要,使用“yes”。
#注意,Redis在守护进程时会在/var/run/redis.pid中写一个pid文件。 守护进程 yes
vim redis.conf 设置 daemonize no 把‘no’修改>daemonize yes
[root@souyunku redis-3.2.7]# vi redis.conf [root@souyunku redis-3.2.7]# ./bin/redis-server ./redis.conf
[root@souyunku redis-3.2.7]# ps -ef | grep red root 8264 1 0 12:34 ? 00:00:00 ./bin/redis-server 127.0.0.1:6379 root 8268 8023 0 12:34 pts/1 00:00:00 grep red
[root@souyunku redis-3.2.7]# cd bin/ [root@souyunku bin]# ./redis-cli 127.0.0.1:6379> set name yanpenglei
OK 127.0.0.1:6379> get name "yanpenglei" 127.0.0.1:6379>
打开redis.conf文件
修改绑定的ip为本机真实的IP bind 10.10.130.140 修改端口号 port 6379 守护进程运行 daemonize yes
修改进程文件 pidfile /var/run/redis_6380.pid 修改日志文件 logfile "/home/software/redis-3.2.7/log/redis.log" 修改持久化文件 dir /usr/software/redis-3.2.7/datas
vim /etc/rc.d/init.d/redisd #!/bin/sh #chkconfig: 345 86 14 #description: Startup and shutdown script for Redis PROGDIR=/opt/redis/redis-3.2.7 #安装路径 PROGNAME=bin/redis-server
DAEMON=$PROGDIR/$PROGNAME CONFIG=/opt/redis/redis-3.2.7/redis.conf
PIDFILE=/var/run/redis_6379.pid
DESC="redis daemon" SCRIPTNAME=/etc/rc.d/init.d/redisd start()
{ if test -x $DAEMON then echo -e "Starting $DESC: $PROGNAME" if $DAEMON $CONFIG then echo -e "OK" else echo -e "failed" fi else echo -e "Couldn't find Redis Server ($DAEMON)" fi } stop()
{ if test -e $PIDFILE then echo -e "Stopping $DESC: $PROGNAME" if kill `cat $PIDFILE` then echo -e "OK" else echo -e "failed" fi else echo -e "No Redis Server ($DAEMON) running" fi } restart()
{ echo -e "Restarting $DESC: $PROGNAME" stop
start
} list()
{
ps aux | grep $PROGNAME } case $1 in start)
start
;;
stop)
stop
;;
restart)
restart
;;
list)
list
;;
*) echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2 exit 1 ;; esac exit 0
redis.conf配置文件中指定的pid路径地址,这里说明一下,在 redis.conf配置文件中需要将
daemonize这个参数项设置为
yes才会在redis启动时生成pid文件,很多新人不知道,没有生成pid文件,所以脚本里根据pid文件关闭redis就失败。
如果正常关闭就会删除这个pid文件
设置redis.conf中daemonize为yes,确保守护进程开启。
cd /etc/rc.d/init.d/
chmod 755 redisd
cd /etc/rc.d/init.d/
chkconfig redisd on
reboot
然后在用redis-cli测试即可。
[root@souyunku ~]# cd /opt/redis/redis-3.2.7/bin/ [root@souyunku bin]# ./redis-cli 127.0.0.1:6379> get name
(nil)
#启动 [root@souyunku ]# service redisd start Starting redis daemon: bin/redis-server OK #重启 [root@souyunku ]# service redisd restart Restarting redis daemon: bin/redis-server Stopping redis daemon: bin/redis-server OK
Starting redis daemon: bin/redis-server OK #查看进程 [root@souyunku ]# service redisd list root 8885 0.0 0.7 38776 7580 ? Ssl 14:36 0:00 /opt/redis//bin/redis-server 127.0.0.1:6379 root 8895 0.0 0.0 6380 692 pts/1 S+ 14:36 0:00 grep bin/redis-server #停止 [root@souyunku ]# service redisd stop Stopping redis daemon: bin/redis-server OK #查看进程 [root@souyunku ]# service redisd list root 8916 0.0 0.0 6380 688 pts/1 S+ 14:38 0:00 grep bin/redis-server
问题1:make时可能会报如下错误
[root@souyunku redis-3.2.7]# make gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
make[3]: gcc: Command not found
[3]:gcc:命令没有找到
make[3]: *** [net. o] Error 127 [3]:* * * 错误127
yum install -y gcc g++ gcc-c++ make
[root@souyunku redis-3.2.7]# make
../deps/jemalloc/lib/libjemalloc.a(nstime.o): In function `nstime_get':
/opt/redis/redis-3.2.7/deps/jemalloc/src/nstime.c:120: undefined reference to `clock_gettime' collect2: ld returned 1 exit status
make: *** [redis-server] Error 1
查找实时库librt所在路径:
在src下的Makefile文件中的函数,加 FINAL_LIBS+= /usr/lib64/librt.so #此路径加上librt.so,即可,在编译就成功了
大概在 105 ~110 行
ifeq ($(MALLOC),jemalloc)
DEPENDENCY_TARGETS+= jemalloc
FINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include
FINAL_LIBS+= ../deps/jemalloc/lib/libjemalloc.a
FINAL_LIBS+= /usr/lib64/librt.so #此路径加上librt.so endif
[root@souyunku redis-3.2.7]# make
cd src && make all
make[1]: Entering directory `/opt/redis/redis-3.2.7/src’
Hint: It’s a good idea to run ‘make test’ ;)
make[1]: Leaving directory `/opt/redis/redis-3.2.7/src’
[root@souyunku redis-3.2.7]# make PREFIX=/opt/redis/redis-3.2.7 install
cd src && make install
make[1]: Entering directory `/opt/redis/redis-3.2.7/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 `/opt/redis/redis-3.2.7/src' [root@souyunku redis-3.2.7]#
bin下的命令是什么意思呢?下面我们来说一说~
[root@souyunku bin]# ll
total 30824
-rwxr-xr-x. 1 root root 6725486 Feb 8 12:18 redis-benchmark -rwxr-xr-x. 1 root root 22193 Feb 8 12:18 redis-check-aof -rwxr-xr-x. 1 root root 8974545 Feb 8 12:18 redis-check-rdb -rwxr-xr-x. 1 root root 6853618 Feb 8 12:18 redis-cli lrwxrwxrwx. 1 root root 12 Feb 8 12:18 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8974545 Feb 8 12:18 redis-server redis-benchmark ---->redis性能测试工具
redis-check-aof ---->检查aof日志工具,如果日志损坏能检查出来
redis-check-dump ---->检查rdb日志工具
redis-cli ---->连接用的客户端
redis-server ---->redis服务区进程
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室