本文共 5059 字,大约阅读时间需要 16 分钟。
安装前的准备
yum install \vim \wget \make \gcc \gcc-c++ \automake \autoconf \-y \
下载解压并安装
cd /rootwget http://download.redis.io/releases/redis-4.0.8.tar.gztar -zxzf redis-4.0.8.tar.gzcd redis-4.0.8make PREFIX=/usr/local/redis/ install
创建配置中要求的数据文件存储位置
mkdir /datamkdir /data/redismkdir /usr/local/redis/log
添加修改配置文件
cp ./redis.conf /usr/local/redisvim /usr/local/redis/redis.conf# IP绑定bind 127.0.0.1 192.168.0.111# 保护模式(开启条件为各redis之间可以互相通信,做集群不可开启)protected-mode yes# 访问端口port 6379# 连接超时,单位S,0为不启用超时timeout 0# 以守护进程运行daemonize yes# 数据文件路径dir /data/redis# 进程ID文件的路径pidfile /usr/local/redis/log/redis.pid# 日志文件路径logfile /usr/local/redis/log/redis.log# 开启键过期删除通知notify-keyspace-events ExESC:wq# 以下安全配置选项仅作参考 # 禁用部分危险命令 rename-command FLUSHALL "" rename-command CONFIG "" rename-command EVAL "" # 添加访问密码 requirepass ******** # IP绑定本机 bind 127.0.0.1
性能优化
# 编辑/etc/rc.localvim /etc/rc.localecho never > /sys/kernel/mm/transparent_hugepage/enabledESC:wq# 添加/etc/rc.local执行权限chmod +x /etc/rc.d/rc.local# 编辑/etc/sysctl.confvim /etc/sysctl.confvm.overcommit_memory = 1net.core.somaxconn = 1024ESC:wq# 立即解决echo never > /sys/kernel/mm/transparent_hugepage/enabledecho 1024 > /proc/sys/net/core/somaxconnsysctl vm.overcommit_memory=1sysctl -p
修改目录归属
useradd -s /sbin/nologin -M redischown -R redis:redis /data/redischown -R redis:redis /usr/local/redis
启动redis并设置开机启动
# 进入单元文件目录cd /etc/systemd/system# 创建redis单元文件,格式为: [单元文件名].[单元文件类型]vim redis.service[Unit]Description=Start redis on boot.After=default.target network.target[Service]User=redisGroup=redisType=forkingPIDFile=/usr/local/redis/log/redis.pidExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=falseRestart=always[Install]WantedBy=multi-user.targetESC:wq# 修改文件权限为只有root用户可以编辑该文件chown -R root:root /etc/systemd/system/redis.servicechmod -R 644 /etc/systemd/system/redis.service# 更新systemdsystemctl daemon-reloadsystemctl enable redissystemctl start redis
存入数据
/usr/local/redis/bin/redis-cli -h 127.0.0.1set num 123saveget numquit
批量删除匹配规则数据
redis-cli keys [匹配规则]|xargs redis-cli del
修复坏损的aof文件
redis-check-aof --fix [aof文件名]
Redis该漏洞用到的原理如下
件是一个二进制文件,但其内容中包含保存的数据的值,如果/www/目录为web
目录,则/www/test.php则会被人利用,成为入侵你服务器的入口;漏洞举例(这是一个负面教材)
web服务器为nginx,并做如下配置:
server { listen x; server_name xxx.xxx.com; root /www; location / { index index.php index.html index.htm; } location ~ \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}
通过redis-cli执行了以下命令
config set dir /www/config set dbfilename test.phpset test " "save
这时候可怕的事情出现了
集群前的配置(单机两服务测试6379,6380)
# 内网IP:192.168.0.100
修改redis.conf
vim /usr/local/redis/redis.conf# 启用clustercluster-enabled yes# 调用cluster配置文件cluster-config-file nodes-6379.conf# 集群超时cluster-node-timeout 15000# 关闭宕机全停cluster-require-full-coverage no# 集群从属元素个数cluster-slave-validity-factor 10# 从机数最低迁移值(master至少有两台slave,才能做slave迁移)cluster-migration-barrier 1ESC:wq
配置文件添加
yum install rubygem install rediscp /usr/local/redis/redis.conf /usr/local/redis/redis.conf.backupcp /usr/local/redis/redis.conf /usr/local/redis/redis-6379.confcp /usr/local/redis/redis.conf /usr/local/redis/redis-6380.confmkdir /usr/local/redis/data/6379mkdir /usr/local/redis/data/6380
修改redis-6379.conf
vim /usr/local/redis/redis-6379.confunixsocket /usr/local/redis/data/6379/redis.sockpidfile /usr/local/redis/log/redis_6379.pidlogfile /usr/local/redis/log/redis_6379.logdir /usr/local/redis/data/6379/bind 192.168.0.100port 6379ESC:wq
修改redis-6380.conf
vim /usr/local/redis/redis-6380.confunixsocket /usr/local/redis/data/6380/redis.sockpidfile /usr/local/redis/log/redis_6380.pidlogfile "/usr/local/redis/log/redis_6380.log"dir /usr/local/redis/data/6380/bind 192.168.0.100port 6380ESC:wq
修改开机启动
vim /etc/rc.local/usr/local/redis/bin/redis-server /usr/local/redis/redis-6379.conf/usr/local/redis/bin/redis-server /usr/local/redis/redis-6380.confESC:wq
创建cluster
cd /root/redis/src/# 查看帮助./redis-trib.rb help# 添加绑定./redis-trib.rb create --replicas 1 192.168.0.100:6379 192.168.0.100:6380
集群测试
/usr/local/redis/bin/redis-cli -h 192.168.0.100 -p 6379 -c
正常关闭(不要强制关闭)
/usr/local/redis/bin/redis-cli -h 192.168.0.100 -p 6379 shutdown
转载地址:http://kqazx.baihongyu.com/