歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

CentOS 6.5下Redis開機啟動配置記錄

CentOS 6.5下Redis開機啟動配置記錄


CentOS 6.5下Redis開機啟動配置記錄


CentOS 6.5下Redis開機啟動配置記錄說一下。

下載安裝

參考:“CentOS 6.5下Redis安裝記錄”

如果你只是執行了Make,要配置開機啟動還需要執行:

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. sudomakeinstall
install的時候,redis的命令會被拷貝到/usr/local/bin下面

復制配置文件到 /etc 目錄下

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. cpredis.conf/etc

建立用戶與日志目錄

建議為Redis單獨建立一個用戶,並新建data和日志文件夾

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. sudouseraddredis
  2. sudomkdir-p/var/lib/redis
  3. sudomkdir-p/var/log/redis
  4. sudochownredis.redis/var/lib/redis
  5. sudochownredis.redis/var/log/redis

修改配置文件

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. vi/etc/redis.conf
修改綁定的IP,解決本機之外其它IP無法訪問的問題(如果需要在其它電腦上訪問);

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. ##################################NETWORK#####################################
  2. #Bydefault,ifno"bind"configurationdirectiveisspecified,Redislistens
  3. #forconnectionsfromallthenetworkinterfacesavailableontheserver.
  4. #Itispossibletolistentojustoneormultipleselectedinterfacesusing
  5. #the"bind"configurationdirective,followedbyoneormoreIPaddresses.
  6. #
  7. #Examples:
  8. #
  9. #bind192.168.1.10010.0.0.1
  10. #bind127.0.0.1::1
  11. #
  12. #~~~WARNING~~~IfthecomputerrunningRedisisdirectlyexposedtothe
  13. #internet,bindingtoalltheinterfacesisdangerousandwillexposethe
  14. #instancetoeverybodyontheinternet.Sobydefaultweuncommentthe
  15. #followingbinddirective,thatwillforceRedistolistenonlyinto
  16. #theIPv4lookbackinterfaceaddress(thismeansRediswillbeableto
  17. #acceptconnectionsonlyfromclientsrunningintothesamecomputerit
  18. #isrunning).
  19. #
  20. #IFYOUARESUREYOUWANTYOURINSTANCETOLISTENTOALLTHEINTERFACES
  21. #JUSTCOMMENTTHEFOLLOWINGLINE.
  22. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. bind0.0.0.0
默認是“bind 127.0.0.1 ::1”,改為“bind 0.0.0.0”;
修改啟動模式為後台啟動

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #################################GENERAL#####################################
  2. #BydefaultRedisdoesnotrunasadaemon.Use'yes'ifyouneedit.
  3. #NotethatRediswillwriteapidfilein/var/run/redis.pidwhendaemonized.
  4. daemonizeyes
daemonize yes

修改數據文件存儲位置

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #Theworkingdirectory.
  2. #
  3. #TheDBwillbewritteninsidethisdirectory,withthefilenamespecified
  4. #aboveusingthe'dbfilename'configurationdirective.
  5. #
  6. #TheAppendOnlyFilewillalsobecreatedinsidethisdirectory.
  7. #
  8. #Notethatyoumustspecifyadirectoryhere,notafilename.
  9. dir/var/lib/redis
注意:是指定一個目錄,不帶文件名;

配置init腳本

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. vi/etc/init.d/redis
[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #chkconfig:23459010
  2. #description:Redisisapersistentkey-valuedatabase
  3. ###########################
  4. PATH=/usr/local/bin:/sbin:/usr/bin:/bin
  5. REDISPORT=6379
  6. EXEC=/usr/local/bin/redis-server
  7. REDIS_CLI=/usr/local/bin/redis-cli
  8. PIDFILE=/var/run/redis.pid
  9. CONF="/etc/redis.conf"
  10. case"$1"in
  11. start)
  12. if[-f$PIDFILE]
  13. then
  14. echo"$PIDFILEexists,processisalreadyrunningorcrashed"
  15. else
  16. echo"StartingRedisserver..."
  17. $EXEC$CONF
  18. fi
  19. if["$?"="0"]
  20. then
  21. echo"Redisisrunning..."
  22. fi
  23. ;;
  24. stop)
  25. if[!-f$PIDFILE]
  26. then
  27. echo"$PIDFILEdoesnotexist,processisnotrunning"
  28. else
  29. PID=$(cat$PIDFILE)
  30. echo"Stopping..."
  31. $REDIS_CLI-p$REDISPORTSHUTDOWN
  32. while[-x${PIDFILE}]
  33. do
  34. echo"WaitingforRedistoshutdown..."
  35. sleep1
  36. done
  37. echo"Redisstopped"
  38. fi
  39. ;;
  40. restart|force-reload)
  41. ${0}stop
  42. ${0}start
  43. ;;
  44. *)
  45. echo"Usage:/etc/init.d/redis{start|stop|restart|force-reload}">&2
  46. exit1
  47. esac
  48. ##############################
注意開頭的兩句:

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #chkconfig:23459010
  2. #description:Redisisapersistentkey-valuedatabase
這雖然是注釋,但要是沒有它,就會報錯:service redis does not support chkconfig
添加執行權限

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. chmod+x/etc/init.d/redis

設定開機啟動服務

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. sudochkconfigredison

啟動,停止redis

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. serviceredisstart
  2. serviceredisstop
或者:

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. /etc/init.d/redisstart
  2. /etc/init.d/redisstop

測試redis

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #redis-cli
  2. 127.0.0.1:6379>setkey123
  3. OK
  4. 127.0.0.1:6379>getkey
  5. "123"
  6. 127.0.0.1:6379>exit
你也可以使用Telnet來測試:

[plain]view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. telnet192.168.1.1006379
連接之後執行相同的命令就行。

http://xxxxxx/Linuxjc/1134261.html TechArticle

Copyright © Linux教程網 All Rights Reserved