redis是一種內存型的NoSQL數據庫,優勢是快,經常使用來作緩存用
redis存儲數據的方法是以key-value的形式
value類型支持字符串,列表,哈希等多種類型linux
環境 : CnetOS7 192.168.94.11c++
關閉SElinux和防火牆redis
安裝Redis數據庫
[root@redis01 ~]# yum -y install wget gcc gcc-c++ make tar openssl openssl-devel cmake [root@redis01 ~]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz [root@redis01 ~]# tar xf redis-4.0.1.tar.gz -C /usr/local/src/ [root@redis01 ~]# cd /usr/local/src/redis-4.0.1/ [root@redis01 redis-4.0.1]# make & make test [root@redis01 redis-4.0.1]# make PREFIX=/usr/local/redis install [root@redis01 redis]# cd /usr/local/redis/ [root@redis01 redis]# ls bin [root@redis01 redis]# mkdir /usr/local/redis/conf [root@redis01 redis]# cp /usr/local/src/redis-4.0.1/redis.conf /usr/local/redis/conf/ [root@redis01 redis]# cp /usr/local/src/redis-4.0.1/sentinel.conf /usr/local/redis/conf/ [root@redis01 redis]# ln -s /usr/local/redis/bin/* /usr/local/bin/ [root@redis01 redis]# redis-server --version Redis server v=4.0.1 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=d789e4152c8567db
簡化redis配置文件,建立redis數據目錄vim
[root@redis01 redis]# grep -Ev '^#|^$' conf/redis.conf.bak > conf/redis.conf [root@redis01 redis]# mkdir /data/redis -p
修改redis配置文件緩存
修改如下參數 [root@redis01 redis]# vim conf/redis.conf bind 0.0.0.0 #監聽地址 port 6379 #監聽端口 tcp-backlog 1024 #tcp鏈接數 daemonize yes #是否後臺啓動 pidfile /data/redis/redis.pid #pid存放目錄 logfile "/data/redis/redis.log" #日誌存放目錄 dir /data/redis/ #工做目錄
redis服務啓動和關閉tcp
啓動redis服務 [root@redis01 redis]# redis-server /usr/local/redis/conf/redis.conf [root@redis01 redis]# netstat -antup|grep redis tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 54109/redis-server 鏈接redis服務 [root@redis01 redis]# redis-cli -h 127.0.0.1 127.0.0.1:6379> quit 關閉redis服務 [root@redis01 redis]# redis-cli -h 127.0.0.1 shutdown [root@redis01 redis]# netstat -antup|grep redis
redis系統參數優化優化
啓動redis以後產看redis日誌 [root@redis01 redis]# cat /data/redis/redis.log 54108:C 08 Oct 15:45:46.095 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 54108:C 08 Oct 15:45:46.095 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=54108, just started 54108:C 08 Oct 15:45:46.095 # Configuration loaded 54109:M 08 Oct 15:45:46.097 * Increased maximum number of open files to 10032 (it was originally set to 1024). # 系統文件描述符設置爲1024,過小,最好設置爲10032 _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 54109 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 54109:M 08 Oct 15:45:46.099 # WARNING: The TCP backlog setting of 1024 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
# TCP的值爲128過小了
54109:M 08 Oct 15:45:46.100 # Server initialized 54109:M 08 Oct 15:45:46.100 # 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. # overcommit_memory=0爲不容許超額搶佔內存,可是,rdb保存可能會失敗。建議將vm.overcommit_memory = 1進行修改
54109:M 08 Oct 15:45:46.100 # 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. # 你的內核中啓用了巨大內存頁的支持,這將與redis的延遲內存使用衝突
54109:M 08 Oct 15:45:46.100 * Ready to accept connections
下面來逐個調整ui
調整系統文件描述符 [root@redis01 redis]# echo "* - nofile 10240" >> /etc/security/limits.conf [root@redis01 redis]# ulimit -n 1024 [root@redis01 redis]# su -l # 從新登陸 , 使配置生效 上一次登陸:一 10月 8 15:15:35 CST 2018從 192.168.94.1pts/1 上 [root@redis01 ~]# ulimit -n 10240 調整TCP鏈接數 [root@redis01 ~]# sysctl -a | grep soma net.core.somaxconn = 128 sysctl: reading key "net.ipv6.conf.all.stable_secret" sysctl: reading key "net.ipv6.conf.default.stable_secret" sysctl: reading key "net.ipv6.conf.ens33.stable_secret" sysctl: reading key "net.ipv6.conf.lo.stable_secret" [root@redis01 ~]# echo "net.core.somaxconn = 10240" >> /etc/sysctl.conf [root@redis01 ~]# sysctl -p net.core.somaxconn = 10240 調整系統內存分配策略 [root@redis01 ~]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf [root@redis01 ~]# sysctl -p net.core.somaxconn = 10240 vm.overcommit_memory = 1 [root@redis01 ~]# sysctl -a | grep commit sysctl: reading key "net.ipv6.conf.all.stable_secret" sysctl: reading key "net.ipv6.conf.default.stable_secret" sysctl: reading key "net.ipv6.conf.ens33.stable_secret" sysctl: reading key "net.ipv6.conf.lo.stable_secret" vm.nr_overcommit_hugepages = 0 vm.overcommit_kbytes = 0 vm.overcommit_memory = 1 # 修改爲功 vm.overcommit_ratio = 50 關閉系統內核的巨大內存頁支持 [root@redis01 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@redis01 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled always madvise [never] [root@redis01 ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag [root@redis01 ~]# cat /sys/kernel/mm/transparent_hugepage/defrag always madvise [never] # 添加到rc.local [root@redis01 ~]# echo -e 'echo never > /sys/kernel/mm/transparent_hugepage/enabled\necho never > /sys/kernel/mm/transparent_hugepage/defrag'>> /etc/rc.local [root@redis01 ~]# tail -2 /etc/rc.local echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag
重啓redis-serverthis
[root@redis01 ~]# redis-cli -h 127.0.0.1 shutdown # 關閉redis服務 [root@redis01 ~]# > /data/redis/redis.log # 清空日誌 [root@redis01 ~]# redis-server /usr/local/redis/conf/redis.conf # 啓動redis服務 [root@redis01 ~]# cat /data/redis/redis.log # 查看日誌 54720:C 08 Oct 16:08:17.474 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 54720:C 08 Oct 16:08:17.474 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=54720, just started 54720:C 08 Oct 16:08:17.474 # Configuration loaded _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 54721 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 54721:M 08 Oct 16:08:17.478 # Server initialized 54721:M 08 Oct 16:08:17.479 * DB loaded from disk: 0.000 seconds 54721:M 08 Oct 16:08:17.479 * Ready to accept connections