linux centos redis-6.2.6一键安装及配置密码

1 篇文章 0 订阅

linux centos redis-6.2.6一键安装及配置密码

redis基本原理

redis作为非关系型nosql数据库,一般公司会作为缓存层,存储唯一会话id,以及请求削峰作用
一、数据结构

Redis支持多种数据结构,包括字符串(String)、列表(List)、集合(Set)、有序集合(Sorted Set)和哈希(Hash)。这些数据结构为Redis提供了灵活的数据存储和操作能力,使得它能够适应不同的应用场景。

二、持久化机制

Redis提供了两种持久化方式:RDB(Redis DataBase)和AOF(Append Only File)。

RDB:通过将某个时刻的内存数据以二进制方式写入磁盘来创建快照。RDB方式可以通过手动触发(save或bgsave命令)或根据配置自动触发。其中,bgsave命令会fork一个子进程来执行持久化,从而避免阻塞主线程。
AOF:通过记录所有的操作命令,并以文本形式追加到文件中。AOF方式提供了更好的数据持久性保证,但相对于RDB方式,其性能可能稍逊一筹。
此外,Redis还支持混合持久化方式,即结合RDB和AOF的优点。在写入时,先将当前数据以RDB形式写入文件开头,再将后续操作命令以AOF格式存入文件。

三、主从复制

Redis的主从复制是其提供高可用性和分布式读取能力的重要手段。主从复制的核心原理包括全量复制与部分复制。当从节点连接到主节点时,会发送PSYNC命令进行同步。如果是从节点第一次连接主节点,会触发全量复制;如果从节点之前已经与主节点同步过数据,则只需进行部分复制。主节点会将写命令传播给所有从节点,以保持数据一致性。

四、事务处理

Redis事务允许以原子性方式执行多个命令,确保这些命令要么全部执行,要么全部不执行。事务通过MULTI、EXEC和DISCARD等命令进行控制。在事务执行期间,其他客户端的命令不会被阻塞,这使得Redis事务非常适合用于高并发场景。然而,Redis事务只能处理简单的Redis命令,对于更复杂的操作,可能需要借助Lua脚本或Redis Streams等高级功能。

综上所述,Redis的原理主要涉及其灵活的数据结构、持久化机制、主从复制以及事务处理等方面。这些特性使得Redis能够适应不同的应用场景,并提供高性能、高可用性的数据存储和操作服务。

一、操作阶段,开始安装

官方下载地址https://download.redis.io/releases/选择6.2.6下载
在这里插入图片描述
配置文件、安装文件、启动文件位置
在这里插入图片描述
安装包位置
在这里插入图片描述

1、将安装包上传到/opt/install/package/redis如没有此目录则创建

mkdir -p /opt/install/package/redis

2、创建配置文件

mkdir -p /opt/install/redis && /opt/install/redis
vim 6379.conf
#protected-mode yes
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
#daemonize no
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis_6379.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /var/lib/redis/6379
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
oom-score-adj no
oom-score-adj-values 0 200 800
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble 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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
appendonly yes
notify-keyspace-events "Egx"
#cluster-enabled yes
#cluster-config-file nodes.conf
#cluster-node-timeout 5000
requirepass aaaaaa

2、创建安装文件

vim install-redis.sh
#!/bin/bash
#install redis
package_name="redis-6.2.6.tar.gz"
script_dir=$(cd $(dirname $0);pwd)

source ./$scipt_dir/../Loginfo.sh

if [ -d /usr/local/redis ];then
    log_info "redis dir already exists!"
    exit 1
else
    mkdir -p /usr/local/redis
    log_info "tar redis package .."
    tar -zxvf $script_dir/../package/redis/$package_name -C /usr/local &>/dev/null
    log_error "redis package tar failed."
if ! grep "transparent_hugepage"  /etc/rc.local &>/dev/null; then
cat >>  /etc/rc.local << EOF
echo never > /sys/kernel/mm/transparent_hugepage/enabled
EOF
fi
log_error "add transparent_hugepag argse to /etc/rc.local failed"
if ! grep "vm.overcommit_memory= 1"  /etc/sysctl.conf &>/dev/null; then
cat >>  /etc/sysctl.conf << EOF
vm.overcommit_memory=1
EOF
sysctl -p &>/dev/null
fi

    log_info "cp redis config to /etc/redis/"
    mkdir -p /var/lib/redis/6379 /etc/redis
    chmod 755 /usr/local/redis/ -R
    cp -v 6379.conf /etc/redis/6379.conf
    cp -v redis_6379.sh /etc/init.d
    chmod +x /etc/init.d/redis_6379.sh
    ln -s /usr/local/redis/bin/redis-* /usr/bin/
    sh /etc/init.d/redis_6379.sh start
fi

log_info "log_path: /var/log/redis_6379.log"
log_info "install redis is ok"

3、创建启动文件

cat redis_6379.sh
#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"
PASSWORD=$(cat $CONF|grep '^\s*requirepass'|awk '{print $2}'|sed 's/"//g')
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO


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
            if [ -z $PASSWORD ]
            then
                $CLIEXEC -p $REDISPORT shutdown
            else
                $CLIEXEC -a $PASSWORD -p $REDISPORT shutdown
            fi
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
        fi
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

4、执行安装命令一键安装

sh install-redis.sh

在这里插入图片描述
在这里插入图片描述
5、如果需要加入到开机自启动

 cd /etc/init.d/
 mv redis_6379.sh redis_6379
 chkconfig --add redis_6379
 chkconfig --level 35 redis_6379 on
 chkconfig --list

在这里插入图片描述

  • 33
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是分别针对CentOS系统下的源码一键部署安装配置Redis持久化存储、MongoDB和Nginx的脚本,以及CentOS系统下源码一键部署安装配置Redis数据库的脚本。请注意,这些脚本仅供参考,使用前请仔细检查并根据自己的需要进行相应的修改。 1. CentOS系统下源码一键部署安装配置Redis持久化存储脚本 ``` #!/bin/bash # 安装Redis相关依赖 yum install -y gcc make wget # 下载Redis源码 wget http://download.redis.io/releases/redis-6.2.5.tar.gz # 解压Redis源码 tar -zxvf redis-6.2.5.tar.gz # 进入Redis源码目录 cd redis-6.2.5 # 编译Redis源码 make # 安装Redis make install # 创建Redis配置文件 cp redis.conf /etc/redis.conf # 修改Redis配置文件 sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf sed -i 's/# requirepass foobared/requirepass yourpassword/g' /etc/redis.conf sed -i 's/# appendonly no/appendonly yes/g' /etc/redis.conf # 启动Redis服务 redis-server /etc/redis.conf ``` 2. CentOS系统下源码一键部署安装配置MongoDB脚本 ``` #!/bin/bash # 安装MongoDB相关依赖 yum install -y gcc make wget # 下载MongoDB源码 wget https://fastdl.mongodb.org/src/mongodb-src-r4.4.6.tar.gz # 解压MongoDB源码 tar -zxvf mongodb-src-r4.4.6.tar.gz # 进入MongoDB源码目录 cd mongodb-src-r4.4.6 # 编译MongoDB源码 scons # 安装MongoDB scons --prefix=/usr/local/mongodb install # 创建MongoDB配置文件 mkdir /usr/local/mongodb/conf cp ./mongod.conf /usr/local/mongodb/conf/mongod.conf # 修改MongoDB配置文件 sed -i 's/#auth = true/auth = true/g' /usr/local/mongodb/conf/mongod.conf # 启动MongoDB服务 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongod.conf ``` 3. CentOS系统下源码一键部署安装配置Nginx脚本 ``` #!/bin/bash # 安装Nginx相关依赖 yum install -y gcc make wget zlib-devel openssl-devel pcre-devel # 下载Nginx源码 wget http://nginx.org/download/nginx-1.21.0.tar.gz # 解压Nginx源码 tar -zxvf nginx-1.21.0.tar.gz # 进入Nginx源码目录 cd nginx-1.21.0 # 编译Nginx源码 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre make && make install # 创建Nginx配置文件 cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak cat > /usr/local/nginx/conf/nginx.conf <<EOF user www www; worker_processes auto; error_log /usr/local/nginx/logs/error.log; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' '\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" "\$http_x_forwarded_for"'; access_log /usr/local/nginx/logs/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 60s; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } server { listen 443 ssl; server_name localhost; ssl_certificate /usr/local/nginx/conf/server.crt; ssl_certificate_key /usr/local/nginx/conf/server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } } } EOF # 启动Nginx服务 /usr/local/nginx/sbin/nginx ``` 4. CentOS系统下源码一键部署安装配置Redis数据库一键自己部署安装脚本 ``` #!/bin/bash # 安装Redis相关依赖 yum install -y gcc make wget # 下载Redis源码 wget http://download.redis.io/releases/redis-6.2.5.tar.gz # 解压Redis源码 tar -zxvf redis-6.2.5.tar.gz # 进入Redis源码目录 cd redis-6.2.5 # 编译Redis源码 make # 安装Redis make install # 创建Redis目录 mkdir /usr/local/redis # 将Redis配置文件复制到Redis目录下 cp redis.conf /usr/local/redis/redis.conf # 设置Redis为后台运行模式 sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/redis.conf # 设置Redis密码 sed -i 's/# requirepass foobared/requirepass yourpassword/g' /usr/local/redis/redis.conf # 设置Redis持久化 sed -i 's/# appendonly no/appendonly yes/g' /usr/local/redis/redis.conf # 启动Redis服务 redis-server /usr/local/redis/redis.conf ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值