首页 > 数据库 >CentOS 7.x 安装redis-5.0.14

CentOS 7.x 安装redis-5.0.14

时间:2022-09-21 18:59:14浏览次数:66  
标签:5.0 14 redis Redis usr local

准备篇

一、防火墙配置

CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

systemctl mask firewalld

systemctl stop firewalld

yum remove firewalld

2、安装iptables防火墙

yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# sample configuration for iptables service

# you can edit this manually or use system-config-firewall

# please do not ask us to add additional ports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

/usr/libexec/iptables/iptables.init restart #重启防火墙

二、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

安装篇

1、下载安装包

下载地址:https://download.redis.io/releases/redis-5.0.14.tar.gz

上传安装包到服务器/usr/local/src目录

2、安装Redis

服务器需要有编译环境,如果没有需要安装

gcc -v

yum install gcc gcc-c++ #安装gcc

mkdir -p /usr/local/redis_db #创建数据存放目录

mkdir -p /usr/local/redis-5.0.14 #创建安装目录

mkdir -p /usr/local/redis-5.0.14/log #创建日志目录

cd /usr/local/src

tar -zxvf redis-5.0.14.tar.gz

cd redis-5.0.14

make

make install PREFIX=/usr/local/redis-5.0.14

3、配置Redis

cp /usr/local/src/redis-5.0.14/redis.conf /usr/local/redis-5.0.14/redis.conf

vi /usr/local/redis-5.0.14/redis.conf

daemonize yes #以后台daemon方式运行redis

pidfile /usr/local/redis-5.0.14/redis_6379.pid

port 6379

bind 127.0.0.1

timeout 300 #客户端超时设置,单位为秒

loglevel notice #设置日志级别,支持四个级别:debug、verbose、notice、warning

logfile "/usr/local/redis-5.0.14/log/redis.log" #日志记录方式,默认为标准输出,logs不写文件,输出到空设备/deb/null

databases 16 #开启数据库的数量

save 900 1

save 300 10

save 60 10000

rdbcompression yes #启用数据库lzf压缩

dbfilename dump.rdb

dir "/usr/local/redis_db"

requirepass 123456 #设置redis数据库连接密码

maxclients 10000 #同一时间最大客户端连接数,0为无限制

maxmemory 4096MB #设定redis最大使用内存,值要小于物理内存,必须设置

appendonly yes #开启日志记录,相当于MySQL的binlog

appendfilename "appendonly.aof" #日志文件名,注意:不是目录路径

appendfsync everysec #每秒执行同步,还有两个参数always、no一般设置为everysec,相当于MySQL事物日志的写方式

:wq! #保存退出

4、启动redis

4.1手动启动

/usr/local/redis-5.0.14/bin/redis-server  /usr/local/redis-5.0.14/redis.conf

#查看进程

ps -ef|grep redis

#端口测试

telnet 127.0.0.1 6379

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

#进入控制台

/usr/local/redis-5.0.14/bin/redis-cli -a 123456

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

127.0.0.1:6379> config get requirepass

1) "requirepass"

2) "123456"

127.0.0.1:6379>

#关闭数据库,需要进入控制台操作

/usr/local/redis-5.0.14/bin/redis-cli -a 123456

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

127.0.0.1:6379> shutdown

not connected>

4.2 配置脚本启动

vi  /usr/local/redis-5.0.14/redis.sh

#!/bin/bash

#应用名称

APP_NAME=redis-5.0.14

#Redis端口

REDISPORT=6379

#Redis安装目录

DIR=/usr/local/redis-5.0.14

#Redis进程文件

PIDFILE=/usr/local/redis-5.0.14/redis_6379.pid

#Redis配置文件

CONF="/usr/local/redis-5.0.14/redis.conf"

#Redis密码

AUTH='123456'

#使用说明,用来提示输入参数

usage() {

echo "Usage: ./redis.sh [start|stop|restart|status]"

exit 1

}

#检查程序是否在运行

is_exist() {

if [ -f $PIDFILE ]

then

pid=$(cat $PIDFILE)

else pid=

fi

#如果不存在返回1,存在返回0

if [ -z "${pid}" ]; then

return 1

else

return 0

fi

}

#启动方法

start() {

is_exist

if [ $? -eq "0" ]; then

echo "${APP_NAME} is already running. pid=${pid} ."

else

echo "Starting Redis server..."

$DIR/bin/redis-server $CONF

fi

}

#停止方法

stop() {

is_exist

if [ $? -eq "0" ]; then

$DIR/bin/redis-cli -p $REDISPORT -a $AUTH shutdown 2>/dev/null

sleep 2

while [ -x $PIDFILE ]

do

echo "Waiting for Redis to shutdown..."

sleep 1

done

echo "Redis stopped"

else

echo "${APP_NAME} is not running"

fi

}

#输出运行状态

status() {

is_exist

if [ $? -eq "0" ]; then

echo "${APP_NAME} is running. Pid is ${pid}"

else

echo "${APP_NAME} is not running."

fi

}

#重启

restart() {

stop

sleep 2

start

}

#根据输入参数,选择执行对应方法,不输入则执行使用说明

case "$1" in

"start")

start

;;

"stop")

stop

;;

"status")

status

;;

"restart")

restart

;;

*)

usage

;;

esac

:wq! #保存退出

#添加脚本执行权限

chmod +x /usr/local/redis-5.0.14/redis.sh

#执行脚本

sh /usr/local/redis-5.0.14/redis.sh start|stop|restart|status

4.3、设置开机启动Redis

cp /usr/local/src/redis-5.0.14/utils/redis_init_script /etc/init.d/redis

vi /etc/init.d/redis #添加修改

AUTH=123456

REDISPORT=6379

EXEC=/usr/local/redis-5.0.14/bin/redis-server

CLIEXEC=/usr/local/redis-5.0.14/bin/redis-cli

PIDFILE=/usr/local/redis-5.0.14/redis_${REDISPORT}.pid

CONF="/usr/local/redis-5.0.14/redis.conf"

$CLIEXEC -p $REDISPORT -a $AUTH shutdown 2>/dev/null

:wq! #保存退出

#设置开机启动

chkconfig redis on

#启动|关闭

service redis start|stop

标签:5.0,14,redis,Redis,usr,local
From: https://www.cnblogs.com/gaoyanbing/p/16716763.html

相关文章

  • Redis 安装与使用
    Redis安装与使用Redis介绍Redis是由SalvatoreSanfilippo写的key-value存储系统,是一个跨平台的非关系型数据库(NoSQL)。Redis是用C语言开发完全开源,基于内存的高......
  • Redis操作类
    importcn.hutool.core.date.DatePattern;importcn.hutool.core.date.DateUtil;importcn.hutool.extra.spring.SpringUtil;importorg.slf4j.Logger;importorg.slf......
  • Docker 运行Redis报错: WARNING overcommit_memory is set to 0!解决方案
    如果dockerrunredis时,查看日志发现警告:WARNINGovercommit_memory is set to 0! Background savemayfailunderlowmemorycondition. To fix this issue......
  • 【Redis】Redis是单线程还是多线程
     Redis6.0版本之前的单线程指的是其网络I/O和键值对读写是由一个线程完成的Redis6.0引入的多线程指的是网络请求过程采用了多线程,而键值对读写命令仍然是单线程处......
  • redis基础系列~监控模板
    {"annotations":{"list":[{"builtIn":1,"datasource":"--Grafana--","enable":true,"hide":true,......
  • HTTP 错误 403.14 - Forbidden
    1、很多人看到403.14就认为是禁止访问,因为在Web服务器上已拒绝目录列表一般情况站点都是不会允许直接读取目录内容的,如果您的站点目录没有有效的默认文档(例如index.ht......
  • LC1143 最长公共子序列
    intlongestCommonSubsequence(stringtext1,stringtext2){//dp[i][j]记录text1前i序列和text2前j序列的最长公共序列intdp[1005][1005];m......
  • Redis集群搭建
    Redis集群本章是基于CentOS7下的Redis集群教程,包括:单机安装RedisRedis主从Redis分片集群 1.单机安装RedisLinux版安装首先需要安装Redis所需要的依赖:yu......
  • [atARC148F]998244353 → 1000000007
    科技题蒙哥马利算法:求$a\cdotm^{-1}\mod\M$(其中$m^{-1}$为$m$模$M$的逆元)记$t=a\cdot\frac{m\cdotm^{-1}-1}{M}\mod\m$,则$a+tM\equiva(1+\frac{m\cdotm^{-1}-1}......
  • Problem P14. [算法课动态规划]连续数组最大和
    感觉很简单的一道题,curnum存下连续数组和大于0的数值,maxnum存下最大连续数组和,curnum从数组头开始,遍历数组,+=数组值,当curnum大于0时,那么即便紧接着的后面有一个很大的......