1. 程式人生 > >centos 6.8安裝redis

centos 6.8安裝redis

res 如果 uil redis 參考 客戶端 啟動 foo lua

技術分享
1. 下載
到redis下載頁面https://redis.io/download下載對應版本的reids安裝包,如:redis-${version}.tar.gz 。

2. 安裝
redis的詳細安裝步驟在安裝包中的README.md文件中有詳細說明,請詳細閱讀。
以安裝redis-4.0.1.tar.gz為例說明。

[[email protected]]# tar xvf redis-4.0.1.tar.gz
[[email protected]]# cd redis-4.0.1
[[email protected] 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。

[[email protected] redis-4.0.1]# yum install gcc -y

安裝完畢gcc之後,繼續安裝redis。

[[email protected] 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。

[[email protected] redis-4.0.1]# make MALLOC=libc
[[email protected] 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

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

[[email protected] redis-4.0.1]# yum install tcl -y
[[email protected] redis-4.0.1]# make test
[[email protected] 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‘

[[email protected] redis-4.0.1]# cd utils
[[email protected] 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服務器已經啟動了!

[[email protected] utils]# cd ..
[[email protected] redis-4.0.1]#


3. 查看狀態/啟動/停止redis

[[email protected] redis-4.0.1]# /etc/init.d/redis_6379 status | start | stop


4. 與redis服務器交互
可以直接使用redis提供的客戶端工具訪問本地redis服務器。

[[email protected] redis-4.0.1]# cd src
[[email protected] 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默認配置不帶任何認證信息,不需要密碼即可訪問!

[[email protected] ~]# vim /etc/redis/6379.conf

打開如下註釋:
requirepass foobared(默認密碼為:foobared,應該設置為一個具備足夠復雜度的認證密碼)

註意:
redis在開啟訪問認證之後,在使用redis-cli客戶端訪問時必須進行認證,否則在執行操作時報錯:(error) NOAUTH Authentication required.
redis-cli認證有2種方式:
方式一:先登錄redis-cli客戶端,在執行操作之前進行認證。

[[email protected] src]# ./redis-cli
127.0.0.1:6379> auth password

方式二:在登錄redis-cli客戶端時就進行認證,這樣登錄成功之後就直接可以執行操作。

[[email protected] src]# ./redis-cli -a password

在認證方式二中,如果傳遞的認證密碼錯誤,會登錄redis-cli成功,但是在執行操作時會提示報錯:(error) NOAUTH Authentication required.
此時就回到認證方式一的情形了。

另外,redis開啟訪問認證之後,停止redis服務時也需要認證,否則報錯:

[[email protected] 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命令,推薦使用。

[[email protected] src]# ./redis-cli -a password
127.0.0.1:6379> SHUTDOWN
not connected> quit

方法三:修改redis服務腳本,加入如下所示的紅色授權信息:
[[email protected] ~]# 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參數,如:

[[email protected] src]# ./redis-cli -a password -h 192.168.80.135
192.168.80.135:6379> 

此時,redis服務只能通過redis-cli停止,如果直接通過服務腳本停止,報錯:

[[email protected] ~]# /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服務,需要在服務腳本中加入如下所示的紅色主機信息:

[[email protected] ~]# 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地址

【參考】
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解決方法

centos 6.8安裝redis