首页 > 系统 > CentOS > 正文

centos6.5 下安装 Redis

2024-06-28 16:01:47
字体:
来源:转载
供稿:网友

linux 下安装

下载地址: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.gztar xzf redis-3.2.7.tar.gzcd redis-3.2.7make

安装时候可能出错,本文末尾有我自己遇到的错误

启动redis

可以看出默认端口是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.78258: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

可以看出默认端口是6379,并且现在的启动不是后台启动。

下面我们改redis配置文件让其后台运行

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 redroot 8264 1 0 12:34 ? 00:00:00 ./bin/redis-server 127.0.0.1:6379root 8268 8023 0 12:34 pts/1 00:00:00 grep red

进入client测试简单命令

[root@souyunku redis-3.2.7]# cd bin/[root@souyunku bin]# ./redis-cli 127.0.0.1:6379> set name yanpengleiOK127.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

贴出redis启动脚本方便管理

vim /etc/rc.d/init.d/redisd#!/bin/sh#chkconfig: 345 86 14#description: Startup and shutdown script for RedisPROGDIR=/opt/redis/redis-3.2.7 #安装路径PROGNAME=bin/redis-serverDAEMON=$PROGDIR/$PROGNAMECONFIG=/opt/redis/redis-3.2.7/redis.confPIDFILE=/var/run/redis_6379.pidDESC="redis daemon"SCRIPTNAME=/etc/rc.d/init.d/redisdstart(){ 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 ;;esacexit 0

注意:PIDFILE=/var/run/redis_6379.pid

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 startStarting redis daemon: bin/redis-serverOK#重启[root@souyunku ]# service redisd restartRestarting redis daemon: bin/redis-serverStopping redis daemon: bin/redis-serverOKStarting redis daemon: bin/redis-serverOK#查看进程[root@souyunku ]# service redisd listroot 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 stopStopping redis daemon: bin/redis-serverOK#查看进程[root@souyunku ]# service redisd listroot 8916 0.0 0.0 6380 688 pts/1 S+ 14:38 0:00 grep bin/redis-server

安装报错

1、不能编译没有GCC 编译工具 :

问题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.cmake[3]: gcc: Command not found[3]:gcc:命令没有找到make[3]: *** [net. o] Error 127[3]:* * * 错误127

解决方法:安装gcc

yum install -y gcc g++ gcc-c++ make

2、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 statusmake: *** [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.aFINAL_LIBS+= /usr/lib64/librt.so #此路径加上librt.soendif

3.错如下:

[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 installmake[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 installmake[1]: Leaving directory `/opt/redis/redis-3.2.7/src'[root@souyunku redis-3.2.7]#

非常简洁,只是在安装目录下多了bin目录

bin下的命令是什么意思呢?下面我们来说一说~

[root@souyunku bin]# lltotal 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-clilrwxrwxrwx. 1 root root 12 Feb 8 12:18 redis-sentinel -> redis-server-rwxr-xr-x. 1 root root 8974545 Feb 8 12:18 redis-serverredis-benchmark ---->redis性能测试工具redis-check-aof ---->检查aof日志工具,如果日志损坏能检查出来redis-check-dump ---->检查rdb日志工具redis-cli ---->连接用的客户端redis-server ---->redis服务区进程
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表