Linux 平台安装 Redis 6

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

bind 127.0.0.1 -::1

94行 保护模式关闭

protected-mode no

98行 Redis端口号,可以修改

port 6379

257行 守护进程方式启动

daemonize yes

901行 复制另起一行,设置密码

requirepass foobared

requirepass 123456

最后使用配置文件,以后台方式启动 Redis 服务。

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

验证



查看 Redis 服务进程状态。

[root@chenpi bin]# ps -ef | grep redis

root 5981 1 0 19:02 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379

root 5987 1342 0 19:02 pts/0 00:00:00 grep --color=auto redis

[root@chenpi bin]#

使用客户端工具连接,进行验证。

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379

使用客户端成功连接后,如果没输入密码进行验证,执行 Redis 命令会报错。认证后,可以使用 PING 命令进行测试,正常返回 PONG

127.0.0.1:6379> PING

(error) NOAUTH Authentication required.

127.0.0.1:6379> AUTH 123456

OK

127.0.0.1:6379> PING

PONG

127.0.0.1:6379>

当然,也可以在使用客户端连接时指明密码,不过不推荐,毕竟是明文显示。

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456

退出客户端连接,可以使用快捷键 Ctrl+C 或者使用 exit 命令。

127.0.0.1:6379> exit

[root@chenpi bin]#

关闭 Redis 服务,可以使用 kill Redis服务进程号的方式;或者使用客户端工具关闭,如下:

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown

当然,也可以使用客户端工具连接进入终端后,再进行关闭。

127.0.0.1:6379> SHUTDOWN

(error) NOAUTH Authentication required.

127.0.0.1:6379> AUTH 123456

OK

127.0.0.1:6379> SHUTDOWN

not connected>

开机自启动(可选)



/etc/init.d 目录下新建一个脚本文件 redis ,文件名可随意,一般是 redis_端口 的形式。输入以下内容:

#!/bin/sh

Redis服务端启动文件

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

Redis客户端连接工具,主要用于关闭Redis服务

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

pid保存所在文件位置

PIDFILE=/var/run/redis_6379.pid

配置文件

CONF=“/usr/local/redis/bin/redis.conf”

端口号

REDISPORT=“6379”

密码,如果有

REDISPASSWORD=“123456”

###############

SysV Init Information

chkconfig: - 58 74

description: redis_6380 is the redis daemon.

BEGIN INIT INFO

Provides: redis_6380

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_6380

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 -a $REDISPASSWORD shutdown

如果不需要密码,使用 $CLIEXEC -p $REDISPORT shutdown

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

保存脚本文件后,为文件添加可执行权限。

[root@chenpihost init.d]# chmod 755 /etc/init.d/redis

使用此脚本文件启动 Redis 服务,并增加 Redis 服务,让 chkconfig 指令得以管理它。如果要删除可以使用命令 chkconfig –del name

启动Redis服务

[root@chenpihost init.d]# service redis start

Starting Redis server…

查看是否启动成功

[root@chenpihost init.d]# ps -ef | grep redis

root 1456 1 0 10:43 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379

root 1476 1359 0 10:44 pts/0 00:00:00 grep --color=auto redis

增加redis服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。

[root@chenpihost init.d]# chkconfig --add redis

显示所有运行级系统服务的运行状态信息(on或off)

[root@chenpihost 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 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@chenpihost init.d]#

注意,如果你的 Redis 配置文件设置了密码,但是脚本文件没有写上密码,会导致关闭服务失败,如下:

[root@chenpihost init.d]# service redis stop

Stopping …

(error) NOAUTH Authentication required.

Waiting for Redis to shutdown …

Waiting for Redis to shutdown …

Waiting for Redis to shutdown …

Waiting for Redis to shutdown …

只有添加了密码之后,才能关闭成功,如下:

重启 Linux 服务器,验证 Redis 服务是否自启动成功,验证如下则成功。

[root@chenpihost ~]# ps -ef | grep redis

root 1028 1 0 11:03 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379

root 1344 1326 0 11:03 pts/0 00:00:00 grep --color=auto redis

[root@chenpihost ~]#

防火墙设置



将 Redis 服务端口加到防火墙上,不然外部无法访问 Redis 服务。即添加端口,重启防火墙,查看防火墙开放的端口。

[root@chenpi rc.d]# firewall-cmd --zone=public --add-port=6379/tcp --permanent

success

[root@chenpi rc.d]# systemctl restart firewalld

[root@chenpi rc.d]# firewall-cmd --zone=public --list-ports

6379/tcp

[root@chenpi rc.d]#

脚本安装


在解压后的 utils 目录下有一些脚本工具,其中的 install_server.sh 脚本可以用来安装 Redis,执行脚本后,按照提示输入即可,最终会自动启动 Redis 服务。而且可以使用这个脚本启动多个 Redis 服务。

在这里插入图片描述

[root@chenpihost utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 6380

Please select the redis config file name [/etc/redis/6380.conf]

Selected default - /etc/redis/6380.conf

Please select the redis log file name [/var/log/redis_6380.log]

Selected default - /var/log/redis_6380.log

Please select the data directory for this instance [/var/lib/redis/6380]

Selected default - /var/lib/redis/6380

Please select the redis executable path [] /usr/local/redis/bin/redis-server

Selected config:

Port : 6380

Config file : /etc/redis/6380.conf

Log file : /var/log/redis_6380.log

Data dir : /var/lib/redis/6380

Executable : /usr/local/redis/bin/redis-server

Cli Executable : /usr/local/redis/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6380.conf => /etc/init.d/redis_6380

Installing service…

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server…

Installation successful!

[root@chenpihost utils]#

注意,如果执行脚本时报以下错误。

[root@chenpihost utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

/redis_6380

Installing service…

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server…

Installation successful!

[root@chenpihost utils]#

注意,如果执行脚本时报以下错误。

[root@chenpihost utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值