天天看點

redis的三種啟動方式

redis的啟動方式

1.直接啟動

  進入redis根目錄,執行指令:

  #加上‘&’号使redis以背景程式方式運作

1

.

/redis-server

&

 2.通過指定配置檔案啟動

  可以為redis服務啟動指定配置檔案,例如配置為/etc/redis/6379.conf

  進入redis根目錄,輸入指令:

.

/redis-server

/etc/redis/6379

.conf

  #如果更改了端口,使用`redis-cli`用戶端連接配接時,也需要指定端口,例如:

redis-cli -p 6380

3.使用redis啟動腳本設定開機自啟動

  啟動腳本 redis_init_script 位于位于Redis的 /utils/ 目錄下,redis_init_script腳本代碼如下: 

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

#!/bin/sh

#

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

#redis伺服器監聽的端口

REDISPORT=6379

#服務端所處位置

EXEC=

/usr/local/bin/redis-server

#用戶端位置

CLIEXEC=

/usr/local/bin/redis-cli

#redis的PID檔案位置,需要修改

PIDFILE=

/var/run/redis_

${REDISPORT}.pid

#redis的配置檔案位置,需将${REDISPORT}修改為檔案名

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

 根據啟動腳本,将修改好的配置檔案複制到指定目錄下,用root使用者進行操作:

mkdir

/etc/redis

cp

redis.conf 

/etc/redis/6379

.conf

 将啟動腳本複制到/etc/init.d目錄下,本例将啟動腳本命名為redisd(通常都以d結尾表示是背景自啟動服務)。

cp

redis_init_script 

/etc/init

.d

/redisd

設定為開機自啟動,直接配置開啟自啟動 chkconfig redisd on 發現錯誤: service redisd does not support chkconfig

解決辦法,在啟動腳本開頭添加如下注釋來修改運作級别:

#!/bin/sh

# chkconfig:   2345 90 10

 再設定即可

#設定為開機自啟動伺服器

chkconfig redisd on

#打開服務

service redisd start

#關閉服務

service redisd stop

.

/redis-server

&

.

/redis-server

/etc/redis/6379

.conf

redis-cli -p 6380

#!/bin/sh

#

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

#redis伺服器監聽的端口

REDISPORT=6379

#服務端所處位置

EXEC=

/usr/local/bin/redis-server

#用戶端位置

CLIEXEC=

/usr/local/bin/redis-cli

#redis的PID檔案位置,需要修改

PIDFILE=

/var/run/redis_

${REDISPORT}.pid

#redis的配置檔案位置,需将${REDISPORT}修改為檔案名

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

mkdir

/etc/redis

cp

redis.conf 

/etc/redis/6379

.conf

cp

redis_init_script 

/etc/init

.d

/redisd

#!/bin/sh

# chkconfig:   2345 90 10

#設定為開機自啟動伺服器

chkconfig redisd on

#打開服務

service redisd start

#關閉服務

service redisd stop