centos 6.8安装redis


1. 下载
到redis下载页面https://redis.io/download下载对应版本的reids安装包,如:redis-${version}.tar.gz 。

2. 安装
redis的详细安装步骤在安装包中的README.md文件中有详细说明,请详细阅读。
以安装redis-4.0.1.tar.gz为例说明。

[root@centosx64]# tar xvf redis-4.0.1.tar.gz
[root@centosx64]# cd redis-4.0.1
[root@centosx64 redis-4.0.1]# make
cd src && make all
make[1]: Entering directory `/root/redis-4.0.1/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/redis-4.0.1/src'
make[1]: Entering directory `/root/redis-4.0.1/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
(cd ../deps && make distclean)
make[2]: Entering directory `/root/redis-4.0.1/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
make[2]: Leaving directory `/root/redis-4.0.1/deps'
(rm -f .make-*)
echo STD=-std=c99 -pedantic -DREDIS_STATIC='' >> .make-settings
echo WARN=-Wall -W -Wno-missing-field-initializers >> .make-settings
echo OPT=-O2 >> .make-settings
echo MALLOC=jemalloc >> .make-settings
echo CFLAGS= >> .make-settings
echo LDFLAGS= >> .make-settings
echo REDIS_CFLAGS= >> .make-settings
echo REDIS_LDFLAGS= >> .make-settings
echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -DREDIS_STATIC='' -Wall -W -Wno-missing-field-initializers -O2 -g -ggdb   -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings
echo PREV_FINAL_LDFLAGS=  -g -ggdb -rdynamic >> .make-settings
(cd ../deps && make hiredis linenoise lua jemalloc)
make[2]: Entering directory `/root/redis-4.0.1/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
(echo "" > .make-ldflags)
(echo "" > .make-cflags)
MAKE hiredis
cd hiredis && make static
make[3]: Entering directory `/root/redis-4.0.1/deps/hiredis'
gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c
make[3]: gcc:命令未找到
make[3]: *** [net.o] 错误 127
make[3]: Leaving directory `/root/redis-4.0.1/deps/hiredis'
make[2]: *** [hiredis] 错误 2
make[2]: Leaving directory `/root/redis-4.0.1/deps'
make[1]: [persist-settings] 错误 2 (忽略)
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] 错误 127
make[1]: Leaving directory `/root/redis-4.0.1/src'
make: *** [all] 错误 2

显然,gcc没有安装。
说明:在进行linux系统安装时,尤其是进行linux服务器安装时,系统工程师往往会最小化安装相应的linux系统。
那么,在这样的linux系统上进行源码文件编译安装时,通常都会出现cc: Command not found,这说明系统上没有安装C语言环境,需要安装。
在linux系统上的C环境是gcc,因此需要安装gcc。

[root@centosx64 redis-4.0.1]# yum install gcc -y

安装完毕gcc之后,继续安装redis。

[root@centosx64 redis-4.0.1]# make
cd src && make all
make[1]: Entering directory `/root/redis-4.0.1/src'
    CC adlist.o
在包含自 adlist.c:34 的文件中:
zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
zmalloc.h:55:2: 错误:#error "Newer version of jemalloc required"
make[1]: *** [adlist.o] 错误 1
make[1]: Leaving directory `/root/redis-4.0.1/src'
make: *** [all] 错误 2

原因:redis默认使用内存分配器jemalloc,在未安装jemalloc时就会报错:jemalloc/jemalloc.h:没有那个文件或目录。
在README中存在如下描述:

Allocator
---------

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

    % make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

    % make MALLOC=jemalloc

解决办法:
1. 安装时明确指定分配器类型:make MALLOC=libc。
2. 先安装jemalloc之后再安装redis。

[root@centosx64 redis-4.0.1]# make MALLOC=libc
[root@centosx64 redis-4.0.1]# make test
cd src && make test
make[1]: Entering directory `/root/redis-4.0.1/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] 错误 1
make[1]: Leaving directory `/root/redis-4.0.1/src'
make: *** [test] 错误 2

执行make test时提示需要安装tcl 8.5及以上版本:You need tcl 8.5 or newer in order to run the Redis test

[root@centosx64 redis-4.0.1]# yum install tcl -y
[root@centosx64 redis-4.0.1]# make test
[root@centosx64 redis-4.0.1]# make install
cd src && make install
make[1]: Entering directory `/root/redis-4.0.1/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 `/root/redis-4.0.1/src'

[root@centosx64 redis-4.0.1]# cd utils
[root@centosx64 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] [回车]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] [回车]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] [回车]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] [回车]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] [回车]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort. [回车]
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

安装完毕!注意,蓝色字体部分即为对应的redis安装配置。
并且redis服务器已经启动了!

[root@centosx64 utils]# cd ..
[root@centosx64 redis-4.0.1]#


3. 查看状态/启动/停止redis

[root@centosx64 redis-4.0.1]# /etc/init.d/redis_6379 status | start | stop

除了可以通过服务脚本启动redis,还可以直接使用redis-server。

[root@centosx64 redis-4.0.1]# cd src
[root@centosx64 src]# ./redis-server /path/to/redis.conf                     # 指定配置文件,redis配置参数在配置文件中
[root@centosx64 src]# ./redis-server /etc/redis/6379.conf --loglevel debug   # 指定redis配置文件,同时指定日志级别
[root@centosx64 src]# ./redis-server --port 9999 --slaveof 127.0.0.1 6379    # redis配置参数通过启动参数设置


4. 与redis服务器交互
可以直接使用redis提供的客户端工具访问本地redis服务器。

[root@centosx64 redis-4.0.1]# cd src
[root@centosx64 src]# ./redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> incr mycounter
(integer) 1
127.0.0.1:6379> get mycounter
"1"
127.0.0.1:6379> incr mycounter
(integer) 2
127.0.0.1:6379> get mycounter
"2"
127.0.0.1:6379> quit


5. 配置
(1)redis默认配置不带任何认证信息,不需要密码即可访问!

[root@centosx64 ~]# vim /etc/redis/6379.conf

打开如下注释:

requirepass foobared(默认密码为:foobared,应该设置为一个具备足够复杂度的认证密码)

注意:
redis在开启访问认证之后,在使用redis-cli客户端访问时必须进行认证,否则在执行操作时报错:(error) NOAUTH Authentication required.
redis-cli认证有2种方式:
方式一:先登录redis-cli客户端,在执行操作之前进行认证。

[root@centosx64 src]# ./redis-cli
127.0.0.1:6379> auth password

方式二:在登录redis-cli客户端时就进行认证,这样登录成功之后就直接可以执行操作。

[root@centosx64 src]# ./redis-cli -a password

在认证方式二中,如果传递的认证密码错误,会登录redis-cli成功,但是在执行操作时会提示报错:(error) NOAUTH Authentication required.
此时就回到认证方式一的情形了。

另外,redis开启访问认证之后,停止redis服务时也需要认证,否则报错:

[root@centosx64 redis-4.0.1]# /etc/init.d/redis_6379 stop
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

此时,redis服务进程是无法正常退出的。
有3种解决办法:

方式一:通过ps命令查看redis进程,然后直接kill掉。
这种方式如果redis运行在deamon下还需要去删除pid文件,有点繁琐,不推荐使用。

方法二:通过redis-cli客户端登录,然后执行SHUTDOWN命令,推荐使用。

[root@centosx64 src]# ./redis-cli -a password
127.0.0.1:6379> SHUTDOWN
not connected> quit
[root@centosx64 src]# ./redis-cli -h 127.0.0.1 -p 6379 shutdown # 或者使用这种方式停止

方法三:修改redis服务脚本,加入如下所示的蓝色授权信息:

[root@centosx64 ~]# vim /etc/init.d/redis_6379
$CLIEXEC -a "password" -p $REDISPORT shutdown

虽然这种方式看似很方便,但是需要将认证信息配置在服务脚本中,更加容易导致认证信息泄露,不推荐使用!

(2)redis默认配置仅允许本地访问,只允许redis-cli在本地访问,其他应用程序客户端无法从外部访问。

bind 127.0.0.1

如果希望redis允许通过指定IP地址访问,则修改为绑定到指定IP地址。如:绑定redis只允许通过192.168.80.135访问。

bind 192.168.80.135

如果配置为绑定指定IP地址,那么通过redis-cli访问时必须携带-h参数,如:

[root@centosx64 src]# ./redis-cli -a password -h 192.168.80.135
192.168.80.135:6379> 

此时,redis服务只能通过redis-cli停止,如果直接通过服务脚本停止,报错:

[root@centosx64 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

如果需要一定要通过服务脚本停止redis服务,需要在服务脚本中加入如下所示的主机信息:

[root@centosx64 ~]# vim /etc/init.d/redis_6379
$CLIEXEC -a password -p $REDISPORT -h 192.168.80.135 shutdown

但是,不推荐这样操作!最好是通过redis-cli停止服务。

如果允许redis既可以通过本地访问,外部应用程序也可以访问,最直接方式就是不绑定任何IP地址,即:

#bind 127.0.0.1


总结,
1. 安装redis时注意如下组件的安装情况:
(1) gcc
(2) jemalloc
(3) tcl
2. 配置redis时注意2个方面:
(1) 配置访问认证密码
(2) 是否需要绑定到指定IP地址

 

附:

如果在安装的时候遇到如下报错:

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

注释下面的代码:

#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#       echo "This systems seems to use systemd."
#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#       exit 1
#fi

然后重新运行 ./install_server.sh即可,参考: https://www.cnblogs.com/strive-for-life/p/13194306.html 。



【参考】
http://blog.csdn.net/bugall/article/details/45914867 Redis 安装报错 error: jemalloc/jemalloc.h: No such file or directory解决方法
http://www.ywnds.com/?p=6957 Redis安装报错error:jemalloc/jemalloc.h:No such file or directory解决方法

 

posted @ 2017-07-30 22:21  nuccch  阅读(548)  评论(0编辑  收藏  举报