redis环境部署

2019-05-23 09:53:28来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

运维开发技术交流群欢迎大家加入一起学习(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]# 

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


原文链接:https://www.cnblogs.com/wangguangtao/p/10906762.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Linux从入门到精通总结

下一篇:linux下安装google-chrome浏览器和chromedriver