linux下安裝redis使用

選取當前最新版本3.2.1下載,上傳到linux上,進行解壓縮:
[root@mongodb1 redis]# ls
redis-3.2.1 redis-3.2.1.tar.gz

2.編譯安裝

進入redis-3.2.1目錄下,運行make進行安裝編譯:
[root@mongodb1 redis-3.2.1]# ls
00-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]# make
cd src && make all
make[1]: Entering directory `/root/redis/redis-3.2.1/src'
CC adlist.o
CC quicklist.o
CC ae.o
In file included from ae.c:53:
ae_epoll.c: In function 'aeApiAddEvent':
ae_epoll.c:75: warning: missing initializer
ae_epoll.c:75: warning: (near initialization for 'ee.data')
ae_epoll.c: In function 'aeApiDelEvent':
ae_epoll.c:92: warning: missing initializer
ae_epoll.c:92: warning: (near initialization for 'ee.data')
CC anet.o
anet.c: In function 'anetSockName':
anet.c:640: warning: dereferencing pointer 's' does break strict-aliasing rules
anet.c:638: note: initialized from here
anet.c:644: warning: dereferencing pointer 's' does break strict-aliasing rules
anet.c:642: note: initialized from here
anet.c: In function 'anetPeerToString':
anet.c:584: warning: dereferencing pointer 's' does break strict-aliasing rules
anet.c:582: note: initialized from here
anet.c:588: warning: dereferencing pointer 's' does break strict-aliasing rules
anet.c:586: note: initialized from here
anet.c: In function 'anetTcpAccept':
anet.c:555: warning: dereferencing pointer 's' does break strict-aliasing rules
anet.c:553: note: initialized from here
anet.c:559: warning: dereferencing pointer 's' does break strict-aliasing rules
anet.c:557: note: initialized from here
CC dict.o
CC server.o
CC sds.o
CC zmalloc.o
CC lzf_c.o
CC lzf_d.o
CC pqsort.o
CC zipmap.o
CC sha1.o
CC ziplist.o
CC release.o
CC networking.o
CC util.o
CC object.o
CC db.o
CC replication.o
CC rdb.o
CC t_string.o
CC t_list.o
CC t_set.o
CC t_zset.o
CC t_hash.o
CC config.o
CC aof.o
CC pubsub.o
CC multi.o
CC debug.o
CC sort.o
CC intset.o
CC syncio.o
CC cluster.o
CC crc16.o
CC endianconv.o
CC slowlog.o
CC scripting.o
CC bio.o
CC rio.o
CC rand.o
CC memtest.o
CC crc64.o
CC bitops.o
CC sentinel.o
CC notify.o
CC setproctitle.o
CC blocked.o
CC hyperloglog.o
CC latency.o
CC sparkline.o
CC redis-check-rdb.o
CC geo.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
CC redis-check-aof.o
LINK redis-check-aof
 
Hint: 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 install
cd src && make install
make[1]: Entering directory `/root/redis/redis-3.2.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/redis-3.2.1/src'
將redis可執行目錄添加到環境變量中,編輯~/.bash_profile添加redis環境變量:
[root@mongodb1 bin]# cat ~/.bash_profile
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
 
# User specific environment and startup programs
 
PATH=/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.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] /data/redis/log/redis_6378.log
Please select the data directory for this instance [/var/lib/redis/6379] /data/redis/6379
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /data/redis/log/redis_6378.log
Data dir : /data/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/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_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 status
Redis is running (19280)
[root@mongodb1 init.d]# service redis_6379 stop
Stopping ...
Redis stopped
[root@mongodb1 init.d]# service redis_6379 start
Starting Redis server...
使用redis-cli連接redis:
[root@mongodb1 init.d]# redis-cli
127.0.0.1:6379>

4.redis服務解析

其實做完以上幾步,我們已經可以正常使用redis了,下面我們來解析一下redis的啓動停止過程.我們解析/etc/init.d/redis_6379文件:
#!/bin/sh
#Configurations injected by install_server below....
 
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/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 INFO
 
 
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "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.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /data/redis/log/redis_6379.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/6379
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
其中主要的參數:
bind:綁定的ip地址
port:監聽端口號
pidfile:pid文件名
dir:數據文件目錄
logfile:日誌文件地址

 最後我分享一下我遇到的坑:
第一:我發現別的服務器不可以訪問redis  
開始以爲是iptables  然後各種設置不中 

程序連接 connection timetout 本地telnet也不通  原因是 etc/redis/6379.conf  裏面bind 127.0.0.1  把所有的bind去掉 這樣就可以任意訪問了 不用設置具體的ip。

第二:第一步設置完之後 用程序連接發現報如下錯誤:
redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only

 accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
at redis.clients.jedis.Protoc
大致意思是開啓了保護模式  訪問只能通過backloop地址訪問
 解決辦法依舊是修改7379_conf  把protected-mode 修改爲no          然後              daemonize no設置爲yes 註冊爲服務在後臺跑  

重新啓動redis就ok
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章