運維開發技術交流羣歡迎你們加入一塊兒學習(QQ:722381733)html
1、Redis服務介紹:node
redis簡單來說就是一個數據庫,一個用來存儲緩存的數據庫容器,主要是讓項目數據能寫進緩存,爲用戶提搞更溫馨的體驗而設定的。或者也能夠理解爲,爲完成大並不是,大訪問量的項目提取數據庫信息緩慢而專門設定的一個軟件。固然Redis還能夠作分佈式鎖等功能,這裏就不一一介紹了。
2、Redis部署:linux
一、前往壓縮包存放目錄(下載地址:wget http://download.redis.io/releases/redis-3.2.0.tar.gz):web
[root@web1 package]# ls apache-tomcat-8.5.39.tar.gz jdk-8u131-linux-x64.tar.gz redis-3.2.0.tar.gz [root@web1 package]#
二、解壓redis
[root@web1 package]# tar xf redis-3.2.0.tar.gz [root@web1 package]# ls apache-tomcat-8.5.39.tar.gz jdk-8u131-linux-x64.tar.gz redis-3.2.0 redis-3.2.0.tar.gz
三、前往解壓出來的redis目錄並解析數據庫
[root@web1 package]# cd redis-3.2.0 [root@web1 redis-3.2.0]# make cd src && make all make[1]: Entering directory `/package/redis-3.2.0/src' rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html (cd ../deps && make distclean) make[2]: Entering directory `/package/redis-3.2.0/deps' (cd hiredis && make clean) > /dev/null || true (cd linenoise && make clean) > /dev/null || true (cd lua && make clean) > /dev/null || true (cd geohash-int && make clean) > /dev/null || true (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
四、將解析後的目錄移動到/usr/local/,並建立軟鏈接apache
[root@web1 redis-3.2.0]# mv /package/redis-3.2.0 /usr/local/ [root@web1 redis-3.2.0]# ln -s /usr/local/redis-3.2.0/ /usr/local/redis [root@web1 redis-3.2.0]# cd /usr/local/ [root@web1 local]# ls apache-tomcat-8.5.39 bin etc games include jdk jdk1.8.0_131 lib lib64 libexec redis redis-3.2.0 sbin share src tomcat
五、如今就能夠前往安裝目錄啓動redis了vim
[root@web1 local]# cd /usr/local/redis [root@web1 redis]# src/redis-server ./redis.conf 4610:M 23 May 00:15:54.657 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.0 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 4610 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 4610:M 23 May 00:15:54.698 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 4610:M 23 May 00:15:54.698 # Server started, Redis version 3.2.0 4610:M 23 May 00:15:54.698 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 4610:M 23 May 00:15:54.698 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 4610:M 23 May 00:15:54.698 * The server is now ready to accept connections on port 6379
3、redis簡單優化:緩存
一、redis.conf配置文件介紹(先將文件的註釋去掉)tomcat
[root@web1 redis]# cp redis.conf redis.conf.`date +%F` [root@web1 redis]# sed -ri '/#|^$/d' redis.conf [root@web1 redis]# cat redis.conf bind 127.0.0.1 #綁定的主機地址(這裏就是本主機的IP地址,若是想作「單點」讓多臺機子訪問,直接{bind ip ip ...})。 protected-mode yes port 6379 #指定Redis監聽端口,默認端口爲6379。 tcp-backlog 511 timeout 0 #當客戶端閒置多長時間後關閉鏈接,若是指定爲0,表示關閉該功能。 tcp-keepalive 0 daemonize no #Redis默認不是以守護進程的方式運行,能夠經過該配置項修改,使用yes啓用守護進程。(注:這個要是沒選yes,若是直接把啓動命令寫進/etc/rc.local,將直接卡死在環境變量。)。 supervised no pidfile /var/run/redis.pid #當Redis以守護進程方式運行時,Redis默認會把pid寫入/var/run/redis.pid文件,能夠經過pidfile指定。 loglevel notice #指定日誌記錄級別,Redis總共支持四個級別:debug、verbose、notice、warning,默認爲notice。 logfile "" #配置啓動時的日誌存放路徑,空着表示不要日誌。 databases 16 #設置數據庫的數量,默認數據庫爲0,可使用SELECT <dbid>命令在鏈接上指定數據庫id save 900 1 #指定在多長時間內,有多少次更新操做,就將數據同步到數據文件,能夠多個條件配合,redis默認提供以下三個save的配置。 save 300 10 save 60 10000 #分別表示900秒(15分鐘)內有1個更改,300秒(5分鐘)內有10個更改以及60秒內有10000個更改。 stop-writes-on-bgsave-error yes rdbcompression yes #指定存儲至本地數據庫時是否壓縮數據,默認爲yes,Redis採用LZF壓縮,若是爲了節省CPU時間,能夠關閉該選項,但會致使數據庫文件變的巨大。 rdbchecksum yes dbfilename dump.rdb #指定本地數據庫文件名,默認值爲dump.rdb。 dir ./ #指定本地數據庫存放目錄。 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 #由於 redis自己同步數據文件是按上面save條件來同步的,因此有的數據會在一段時間內只存在於內存中。默認爲no appendfilename "appendonly.aof" #指定更新日誌文件名,默認爲appendonly.aof appendfsync everysec #指定更新日誌條件,共有3個可選值:no:表示等操做系統進行數據緩存同步到磁盤(快)、always:表示每次更新操做後手動調用fsync()將數據寫到磁盤(慢,安全)、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
二、簡單的修改下redis.conf配置文件
[root@web1 redis]# vim redis.conf protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 daemonize yes supervised no pidfile /var/run/redis.pid loglevel notice logfile "/usr/local/redis/log/redis.log" databases 16
......
##其餘的不作變化###
三、在redis目錄裏建立了個log目錄用來保存啓動日誌,而後啓動redis
[root@web1 redis]# ./src/redis-server ./redis.conf *** FATAL CONFIG FILE ERROR *** Reading the configuration file, at line 11 >>> 'logfile "./log/redis.log"' Can't open the log file: No such file or directory #注:這裏是沒有建立log目錄 [root@web1 redis]# ls 00-RELEASENOTES CONTRIBUTING deps INSTALL MANIFESTO redis.conf runtest runtest-sentinel src utils BUGS COPYING dump.rdb Makefile README.md redis.conf.2019-05-23 runtest-cluster sentinel.conf tests [root@web1 redis]# [root@web1 redis]# mkdir log [root@web1 redis]# ./src/redis-server ./redis.conf
四、檢查端口進程
[root@web1 redis]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 4634 root 4u IPv4 27306 0t0 TCP localhost:6379 (LISTEN) [root@web1 redis]# ps -ef|grep 6379 root 4634 1 0 00:51 ? 00:00:00 ./src/redis-server 127.0.0.1:6379 root 4641 1286 0 00:55 pts/0 00:00:00 grep --color=auto 6379 [root@web1 redis]# netstat -lnutp|grep 6379 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 4634/./src/redis-se
五、部署開機自啓
[root@web1 redis]# cd /etc/init.d/ [root@web1 init.d]# vim redis.sh #!/bin/sh # chkconfig: 2345 10 90 # description: Start and Stop redis REDISPORT=6379 EXEC=/usr/local/redis/src/redis-server CLIEXEC=/usr/local/redis/src/redis-cli PIDFILE=/run/redis.pid CONF="/usr/local/redis/redis.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 ;; restart) "$0" stop sleep 3 "$0" start ;; *) echo "Please use start or stop or restart as first argument" ;; esac
####################保存後################
[root@web1 init.d]# chmod +x redis.sh
[root@web1 init.d]# chkconfig --add redis.sh
六、查看啓動項
[root@web1 init.d]# chkconfig --list Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@web1 init.d]#
七、重啓redis
[root@web1 init.d]# /etc/init.d/redis.sh restart Stopping ... Redis stopped Starting Redis server... [root@web1 init.d]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 4777 root 4u IPv4 29529 0t0 TCP localhost:6379 (LISTEN) [root@web1 init.d]#
###在此就配置完成了,服務部署有什麼問題,麻煩讀客幫忙提出來,感謝!!!######