運維開發技術交流群歡迎大家加入一起學習(QQ:722381733) 一、Redis服務介紹: 二、Redis部署: 1、前往壓縮包存放目錄(下載地址:wget http://download.redis.io/releases/redis-3.2.0.tar.gz): 2、解壓 3、前往解壓出來的r ...
運維開發技術交流群歡迎大家加入一起學習(QQ:722381733)
一、Redis服務介紹:
redis簡單來講就是一個資料庫,一個用來存儲緩存的資料庫容器,主要是讓項目數據能寫進緩存,為用戶提搞更舒適的體驗而設定的。或者也可以理解為,為完成大並非,大訪問量的項目提取資料庫信息緩慢而專門設定的一個軟體。當然Redis還可以做分散式鎖等功能,這裡就不一一介紹了。
二、Redis部署:
1、前往壓縮包存放目錄(下載地址:wget http://download.redis.io/releases/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.tar.gz [root@web1 package]#
2、解壓
[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
3、前往解壓出來的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
4、將解析後的目錄移動到/usr/local/,並創建軟連接
[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
5、現在就可以前往安裝目錄啟動redis了
[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
三、redis簡單優化:
1、redis.conf配置文件介紹(先將文件的註釋去掉)
[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
2、簡單的修改下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
......
##其他的不做變化###
3、在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
4、檢查埠進程
[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
5、部署開機自啟
[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
6、查看啟動項
[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]#
7、重啟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]#
###在此就配置完成了,服務部署有什麼問題,麻煩讀客幫忙提出來,感謝!!!######