CentOS 6.5下Redis開機啟動配置記錄
CentOS 6.5下Redis開機啟動配置記錄
CentOS 6.5下Redis開機啟動配置記錄說一下。
下載安裝
參考:“CentOS 6.5下Redis安裝記錄”
如果你只是執行了Make,要配置開機啟動還需要執行:
[plain]view plaincopy
- sudomakeinstall
install的時候,redis的命令會被拷貝到/usr/local/bin下面
復制配置文件到 /etc 目錄下
[plain]view plaincopy
- cpredis.conf/etc
建立用戶與日志目錄
建議為Redis單獨建立一個用戶,並新建data和日志文件夾
[plain]view plaincopy
- sudouseraddredis
- sudomkdir-p/var/lib/redis
- sudomkdir-p/var/log/redis
- sudochownredis.redis/var/lib/redis
- sudochownredis.redis/var/log/redis
修改配置文件
[plain]view plaincopy
- vi/etc/redis.conf
修改綁定的IP,解決本機之外其它IP無法訪問的問題(如果需要在其它電腦上訪問);[plain]view plaincopy- ##################################NETWORK#####################################
- #Bydefault,ifno"bind"configurationdirectiveisspecified,Redislistens
- #forconnectionsfromallthenetworkinterfacesavailableontheserver.
- #Itispossibletolistentojustoneormultipleselectedinterfacesusing
- #the"bind"configurationdirective,followedbyoneormoreIPaddresses.
- #
- #Examples:
- #
- #bind192.168.1.10010.0.0.1
- #bind127.0.0.1::1
- #
- #~~~WARNING~~~IfthecomputerrunningRedisisdirectlyexposedtothe
- #internet,bindingtoalltheinterfacesisdangerousandwillexposethe
- #instancetoeverybodyontheinternet.Sobydefaultweuncommentthe
- #followingbinddirective,thatwillforceRedistolistenonlyinto
- #theIPv4lookbackinterfaceaddress(thismeansRediswillbeableto
- #acceptconnectionsonlyfromclientsrunningintothesamecomputerit
- #isrunning).
- #
- #IFYOUARESUREYOUWANTYOURINSTANCETOLISTENTOALLTHEINTERFACES
- #JUSTCOMMENTTHEFOLLOWINGLINE.
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- bind0.0.0.0
默認是“bind 127.0.0.1 ::1”,改為“bind 0.0.0.0”;
修改啟動模式為後台啟動[plain]view plaincopy- #################################GENERAL#####################################
- #BydefaultRedisdoesnotrunasadaemon.Use'yes'ifyouneedit.
- #NotethatRediswillwriteapidfilein/var/run/redis.pidwhendaemonized.
- daemonizeyes
daemonize yes修改數據文件存儲位置
[plain]view plaincopy- #Theworkingdirectory.
- #
- #TheDBwillbewritteninsidethisdirectory,withthefilenamespecified
- #aboveusingthe'dbfilename'configurationdirective.
- #
- #TheAppendOnlyFilewillalsobecreatedinsidethisdirectory.
- #
- #Notethatyoumustspecifyadirectoryhere,notafilename.
- dir/var/lib/redis
注意:是指定一個目錄,不帶文件名;配置init腳本
[plain]view plaincopy- vi/etc/init.d/redis
[plain]view plaincopy- #chkconfig:23459010
- #description:Redisisapersistentkey-valuedatabase
- ###########################
- PATH=/usr/local/bin:/sbin:/usr/bin:/bin
- REDISPORT=6379
- EXEC=/usr/local/bin/redis-server
- REDIS_CLI=/usr/local/bin/redis-cli
- PIDFILE=/var/run/redis.pid
- CONF="/etc/redis.conf"
- case"$1"in
- start)
- if[-f$PIDFILE]
- then
- echo"$PIDFILEexists,processisalreadyrunningorcrashed"
- else
- echo"StartingRedisserver..."
- $EXEC$CONF
- fi
- if["$?"="0"]
- then
- echo"Redisisrunning..."
- fi
- ;;
- stop)
- if[!-f$PIDFILE]
- then
- echo"$PIDFILEdoesnotexist,processisnotrunning"
- else
- PID=$(cat$PIDFILE)
- echo"Stopping..."
- $REDIS_CLI-p$REDISPORTSHUTDOWN
- while[-x${PIDFILE}]
- do
- echo"WaitingforRedistoshutdown..."
- sleep1
- done
- echo"Redisstopped"
- fi
- ;;
- restart|force-reload)
- ${0}stop
- ${0}start
- ;;
- *)
- echo"Usage:/etc/init.d/redis{start|stop|restart|force-reload}">&2
- exit1
- esac
- ##############################
注意開頭的兩句:[plain]view plaincopy- #chkconfig:23459010
- #description:Redisisapersistentkey-valuedatabase
這雖然是注釋,但要是沒有它,就會報錯:service redis does not support chkconfig
添加執行權限[plain]view plaincopy- chmod+x/etc/init.d/redis
設定開機啟動服務
[plain]view plaincopy- sudochkconfigredison
啟動,停止redis
[plain]view plaincopy- serviceredisstart
- serviceredisstop
或者:[plain]view plaincopy- /etc/init.d/redisstart
- /etc/init.d/redisstop
測試redis
[plain]view plaincopy- #redis-cli
- 127.0.0.1:6379>setkey123
- OK
- 127.0.0.1:6379>getkey
- "123"
- 127.0.0.1:6379>exit
你也可以使用Telnet來測試:[plain]view plaincopy- telnet192.168.1.1006379
連接之後執行相同的命令就行。
http://xxxxxx/Linuxjc/1134261.html TechArticle