歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux和windows下安裝redis

Linux和windows下安裝redis

日期:2017/3/3 12:48:31   编辑:Linux技術
進入下載目錄
[html] view
plain copy
print?
cd /usr/local
下載redis
[html] view
plain copy
print?
wgethttp://download.redis.io/releases/redis-3.0.5.tar.gz
解壓縮
[html] view
plain copy
print?
tar -zxvf redis-3.0.5.tar.gz
進入安裝目錄
[html] view
plain copy
print?
cd redis-3.0.5/src
編譯安裝
[html] view
plain copy
print?
make install
如何啟動
[html] view
plain copy
print?
redis-server &
檢測客戶端檢測連接是否正常
[html] view
plain copy
print?
redis-cli
停止服務

ctrl+C 跳出
如何開機自啟動
啟動腳本 redis_init_script 位於位於redis的 /utils/目錄下

復制此腳本文件至/etc/init.d目錄下
[html] view
plain copy
print?
cp /usr/local/redis-3.0.5/utils/redis_init_script /etc/init.d/redis
修改文件
[html] view
plain copy
print?
vi /etc/init.d/redis
[html] view
plain copy
print?
#!/bin/sh
# chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/redis-3.0.5/src/redis-server
CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
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
;;
*)
echo "Please use start or stop as first argument"
;;
esac
和原配置文件相比:
1.原文件是沒有以下第2行的內容的,
#chkconfig: 2345 80 90
我自己改寫的文件
[html] view
plain copy
print?
#!/bin/bash
# Startup script for the redis
# chkconfig: 2345 80 90
# description: The redis daemon is a network memory cache service.
# processname: redis
# pidfile: /var/run/redis.pid
REDISPORT=6379
EXEC=/usr/local/redis-3.0.5/src/redis-server
CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
RETVAL=0
prog="redis"
# Source function library.
. /etc/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $EXEC ] || exit 0
# Start redis daemons functions.
start() {
if [ -f $PIDFILE ];then
echo "$PIDFILE exists, process is already running or crashed"
exit 1
fi
echo $"Starting $prog: "
$EXEC $CONF &
RETVAL=$?
return $RETVAL
}
# Stop redis daemons functions.
stop() {
if [ ! -f $PIDFILE ];then
echo "$PIDFILE exists, process is already running or crashed"
exit 1
fi
echo -n $"Stopping $prog: "
PID=$(cat $PIDFILE)
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "redis stopped"
RETVAL=$?
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|restart|reload}"
exit 1
esac
exit $RETVAL
你喜歡的話,也可以用我改寫的噢
2.原文件EXEC、CLIEXEC參數,也是有所更改。
3.redis開啟的命令,以後台運行的方式執行,加上 &
$EXEC $CONF &
ps:注意後面的那個“&”,即是將服務轉到後面運行的意思,否則啟動服務時,redis服務將 占據在前台,占用了主用戶界面,造成其它的命令執行不了。
將redis配置文件拷貝到/etc/redis/${REDISPORT}.conf
[html] view
plain copy
print?
mkdir /etc/redis
[html] view
plain copy
print?
cp /usr/local/redis-3.0.5/redis.conf /etc/redis/6379.conf
修改此配置文件
[html] view
plain copy
print?
daemonize no //修改為yes 使redis以守護進程模式啟動
pidfile /var/run/redis.pid //修改為/var/run/redis_6379.pid 設置redis的PID文件的路徑
賦予redis文件執行權限
[html] view
plain copy
print?
chmod +x /etc/init.d/redis
添加服務
[html] view
plain copy
print?
chkconfig --add redis
設置開機啟動
[html] view
plain copy
print?
chkconfig --level 35 redis on
查看是否設置成功
[html] view
plain copy
print?
chkconfig --list | grep redis

此狀態下表面開機啟動成功
service方式啟動redis服務
[html] view
plain copy
print?
service redis start

service方式停止redis服務
[html] view
plain copy
print?
service redis stop

將redis的命令所在目錄添加到系統參數PATH中
修改profile文件:
[html] view
plain copy
print?
vi /etc/profile
追加
[html] view
plain copy
print?
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/redis-3.0.5/src
使配置文件立即生效
[html] view
plain copy
print?
source /etc/profile
這樣就可以直接調用redis-cli的命令了,如下所示:

退出客戶端輸入quit即可
檢測後台進程是否存在
[html] view
plain copy
print?
ps -ef|grep redis
檢測6379端口是否在監聽
[html] view
plain copy
print?
netstat -tunpl | grep 6379
密碼認證
打開 /etc/redis.conf 修改 requirepass 配置項
[html] view
plain copy
print?
# vim /etc/redis.conf
requirepass test123
[html] view
plain copy
print?
# service redis restart
Stopping redis-server: [ OK ]
Starting redis-server: [ OK ]
# redis-cli
redis 127.0.0.1:6379> set h helloworld
(error) ERR operation not permitted
auth test123
[html] view
plain copy
print?
redis 127.0.0.1:6379> auth test123
OK
redis 127.0.0.1:6379> set h helloworld
OK
redis 127.0.0.1:6379> get h
"helloworld"
redis 127.0.0.1:6379> exit
redis-cli -a 參數指定密碼
[html] view
plain copy
print?
# redis-cli -a test123
redis 127.0.0.1:6379> set h helloworld
OK
redis 127.0.0.1:6379> get h
"helloworld"
redis 127.0.0.1:6379> exit
通過 config 動態改變密碼,無需重新啟動 redis 進程
[html] view
plain copy
print?
# redis-cli -a test123
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
redis 127.0.0.1:6379> config set requirepass passabc
OK
redis 127.0.0.1:6379> auth passabc
OK
redis 127.0.0.1:6379> set h helloworld
OK
redis 127.0.0.1:6379> get h
"helloworld"
redis 127.0.0.1:6379> exit
master/slave 模式, master 有密碼, slave 怎樣配置?
[html] view
plain copy
print?
masterauth password
redis的主從配置
1、選中一台服務器作為master
2、其它服務器作為slave
修改/etc/redis/6379.conf配置文件
找到# slaveof <masterip> <masterport>
把此配置注釋放開,寫上master的ip 端口號即可

[html] view
plain copy
print?
slaveof 192.168.1.101 6379
重啟服務,即可發現master機子中的相關數據已經同步到所有的slave從機中
windows下下載redis安裝包,可以到redis官網下載:http://redis.io或者github上面下載https://github.com/antirez/redis,然後解壓:
如圖

其實這2個版本是一樣的,我用的是最後一個,把最後一個解壓出來:
如圖

redis.windows.conf這個就是redis在windows下面的配置文件,解壓了redis不會把服務安裝到計算機裡面的服務列表裡面,不安裝在服務裡面很蛋疼的,每次都要開一個黑窗口!所以我們得把redis安裝到我們自己windows服務器裡面,用如下命令把redis服務安裝到計算機裡面:
進入redis的安裝目錄運行 redis-server --service-install
redis.windows.conf
把redis發布到windows服務裡面
打開計算機服務列表就可以看到了:

這樣我們在windows下就安裝完成了。
接下來我們要安裝一個客戶端來連接redis,推薦使用

perfect!!!
Copyright © Linux教程網 All Rights Reserved