1.打开终端
2 使用 Homebrew 包管理器安装 Redis。在终端中输入以下命令:
brew install redis
如果您尚未安装 Homebrew,则需要使用以下命令进行安装:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
该命令将下载并安装Homebrew,如果您已经安装了Homebrew,则可以跳过此步骤。
如果在执行brew install redis
命令时卡住不动,可能是由于网络连接问题或Homebrew安装程序被墙所致。您可以尝试以下步骤解决这个问题:
2.1 尝试使用Homebrew的替代安装命令:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
该命令将下载并安装Homebrew,如果您已经安装了Homebrew,则可以跳过此步骤。
2.2运行以下命令以更新Homebrew:
brew update
2.3运行以下命令以清理Homebrew缓存:
brew update
2.4运行以下命令以清理Homebrew缓存:
brew cleanup
2.5 再次运行brew install redis
命令以安装Redis。
3.等待安装完成后,您可以使用以下命令来启动 Redis 服务:
redis-server
4.如果您想在 Redis 安装目录下运行 Redis 服务,请使用以下命令:
redis-server /usr/local/etc/redis.conf
5.如果您想在后台运行 Redis 服务,请使用以下命令:
redis-server --daemonize yes
如何检验成功安装了redis
打开终端
运行一下命令
redis-cli ping
如果 Redis 已经成功安装并正在运行,您将看到以下输出:
pong
如果 Redis 未运行或未安装,您将看到以下输出:
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
#或者是
-bash: redis-cli: command not found
如果您看到 Could not connect to Redis
的错误信息,请尝试启动 Redis 服务,使用以下命令:
redis-server
如果安装了 Redis,但是无法成功连接,请确保 Redis 正确运行,并检查防火墙设置以确保允许Redis端口(默认为 6379)的通信。
查看防火墙状态
#查看防火墙状态
systemctl status firewalld
#暂时关闭防火墙
systemctl stop firewalld
#开放指定端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent #6379指定端口号