# Redis mac 安装及配置信息

# 安装命令

1
brew install redis

# 开启服务

1
brew services start redis

# 关闭服务

1
brew services stop redis

如果出现提示类似于

image-20240601155541001

执行

1
brew update

然后,继续执行上述命令即可

# 指定端口

1
2
3
4
5
redis-cli -h localhost -p 6379

-h ip地址
-p 端口号
-a 密码(如果需要)

# 配置文件开启密码

1
2
cd /opt/homebrew/etc
open redis.conf

# 如何查找配置文件位置

1
brew list redis

image-20240601160205142

找到 plist 文件,cat 命令查看,并找出 conf 配置文件位置即可

1
cat /opt/homebrew/Cellar/redis/7.2.5/homebrew.mxcl.redis.plist

image-20240601160648419

# 记得备份配置文件

1
cp /opt/homebrew/etc/redis.conf /opt/homebrew/etc/redis.conf.backup

# 修改配置文件

1
2
cd /opt/homebrew/etc/
sudo vim redis.conf

设置开启密码

image-20240601161404303

1
2
3
4
5
6
# 允许访问的地址,默认是127.0.0.1,会导致只能在本地访问。修改为0.0.0.0则可以在任意IP访问,生产环境不要设置为0.0.0.0
bind 0.0.0.0
# 守护进程,修改为yes后即可后台运行
daemonize yes
# 密码,设置后访问Redis必须输入密码
requirepass 123321

Redis 的其它常见配置:

1
2
3
4
5
6
7
8
9
10
# 监听的端口
port 6379
# 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录
dir .
# 数据库数量,设置为1,代表只使用1个库,默认有16个库,编号0~15
databases 1
# 设置redis能够使用的最大内存
maxmemory 512mb
# 日志文件,默认为空,不记录日志,可以指定日志文件名
logfile "redis.log"

# Redis 命令行客户端

Redis 安装完成后就自带了命令行客户端:redis-cli,使用方式如下:

1
redis-cli [options] [commonds]

其中常见的 options 有:

  • -h 127.0.0.1 :指定要连接的 redis 节点的 IP 地址,默认是 127.0.0.1
  • -p 6379 :指定要连接的 redis 节点的端口,默认是 6379
  • -a 123321 :指定 redis 的访问密码

其中的 commonds 就是 Redis 的操作命令,例如:

  • ping :与 redis 服务端做心跳测试,服务端正常会返回 pong