运维开发技术交流群欢迎大家加入一起学习(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..tar.gz jdk-8u131-linux-x64.tar.gz redis-3.2..tar.gz
[root@web1 package]#

  2、解压

[root@web1 package]# tar xf redis-3.2..tar.gz
[root@web1 package]# ls
apache-tomcat-8.5..tar.gz jdk-8u131-linux-x64.tar.gz redis-3.2. redis-3.2..tar.gz

  3、前往解压出来的redis目录并解析

[root@web1 package]# cd redis-3.2.
[root@web1 redis-3.2.]# make
cd src && make all
make[]: Entering directory `/package/redis-3.2./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[]: Entering directory `/package/redis-3.2./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.]# mv /package/redis-3.2. /usr/local/
[root@web1 redis-3.2.]# ln -s /usr/local/redis-3.2./ /usr/local/redis
[root@web1 redis-3.2.]# cd /usr/local/
[root@web1 local]# ls
apache-tomcat-8.5. bin etc games include jdk jdk1..0_131 lib lib64 libexec redis redis-3.2. sbin share src tomcat

  5、现在就可以前往安装目录启动redis了

[root@web1 local]# cd /usr/local/redis
[root@web1 redis]# src/redis-server ./redis.conf
:M May ::54.657 * Increased maximum number of open files to (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 4610
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :M May ::54.698 # WARNING: The TCP backlog setting of cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of .
:M May ::54.698 # Server started, Redis version 3.2.
:M May ::54.698 # WARNING overcommit_memory is set to ! 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.
:M May ::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.
:M May ::54.698 * The server is now ready to accept connections on port

三、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
timeout 0  #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能。
tcp-keepalive
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 1  #指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合,redis默认提供如下三个save的配置。
save
save 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
repl-disable-tcp-nodelay no
slave-priority
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
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz

  2、简单的修改下redis.conf配置文件

[root@web1 redis]# vim redis.conf

protected-mode yes
port
tcp-backlog
timeout
tcp-keepalive
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
>>> 'logfile "./log/redis.log"'
Can't open the log file: No such file or directory  #注:这里是没有创建log目录
[root@web1 redis]# ls
-RELEASENOTES CONTRIBUTING deps INSTALL MANIFESTO redis.conf runtest runtest-sentinel src utils
BUGS COPYING dump.rdb Makefile README.md redis.conf.-- 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:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser root 4u IPv4 0t0 TCP localhost: (LISTEN)
[root@web1 redis]# ps -ef|grep
root : ? :: ./src/redis-server 127.0.0.1:
root : pts/ :: grep --color=auto
[root@web1 redis]# netstat -lnutp|grep
tcp 127.0.0.1: 0.0.0.0:* LISTEN /./src/redis-se

  5、部署开机自启

[root@web1 redis]# cd /etc/init.d/
[root@web1 init.d]# vim redis.sh
#!/bin/sh
# chkconfig:
# description: Start and Stop redis REDISPORT=
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
done
echo "Redis stopped"
fi
;;
restart)
"$0" stop
sleep
"$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 :off :off :off :off :off :off :off
network :off :off :on :on :on :on :off
redis.sh :off :off :on :on :on :on :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:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser root 4u IPv4 0t0 TCP localhost: (LISTEN)
[root@web1 init.d]#

###在此就配置完成了,服务部署有什么问题,麻烦读客帮忙提出来,感谢!!!######

05-27 13:42