欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

redis环境部署

程序员文章站 2022-06-22 22:14:38
运维开发技术交流群欢迎大家加入一起学习(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]# 

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