Mac安装Redis

chenvle 2021-08-31 PM 886℃ 0条

macbook笔记本推荐安装homeBrew与iterm2,以方便大多数软件的安装homeBrew与iterm2,安装方法百度即可,关于homeBrew的操作命令如下:

brew search **  //查找某个软件包
brew list  //列出已经安装的软件的包
brew install ** //安装某个软件包,默认安装的是稳定版本
brew uninstall **//卸载某个软件的包
brew upgrade ** //更新某个软件包
brew info ** //查看指定软件包的说明
brew cache clean //清理缓存

下面我们一起看下在mac上如何利用homeBrew来安装redis:

  • 首先我们查看下已有的redis各个版本:
brew search redis
  • 输出如下,我们以安装redis@3.2版本为例进行说明:
==> Formulae
hiredis         redis           redis-leveldb   redis@3.2 ✔     redis@4.0
 
==> Casks
homebrew/cask/redis
  • 安装命令
brew install redis@3.2
  • 正常情况下会出现:
.......
redis@3.2 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
 
If you need to have redis@3.2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/redis@3.2/bin:$PATH"' >> ~/.bash_profile
 
 
To have launchd start redis@3.2 now and restart at login:
  brew services start redis@3.2
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/redis@3.2/bin/redis-server /usr/local/etc/redis.conf
==> Summary
?  /usr/local/Cellar/redis@3.2/3.2.13: 13 files, 1.7MB
==> `brew cleanup` has not been run in 30 days, running now...
.......

根据上述提示信息设置环境变量和启动Redis:(注意对应你输出的提示信息操作,不要盲目复制我的)

  • 环境变量设置:
echo 'export PATH="/usr/local/opt/redis@3.2/bin:$PATH"' >> ~/.bash_profile
  • 两种方式,启动命令:
brew services start redis@3.2        #(后台运行)
/usr/local/opt/redis@3.2/bin/redis-server /usr/local/etc/redis.conf  #(非后台运行)

若在启动的过程中报错“Can't handle RDB format version 9,Fatal error loading the DB: Invalid argument. Exiting.”,如下:

B000000098663i:lib rongsong$ /usr/local/opt/redis@3.2/bin/redis-server /usr/local/etc/redis.conf
99730:M 23 Jun 12:28:53.633 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.13 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 99730
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
 
99730:M 23 Jun 12:28:53.638 # Server started, Redis version 3.2.13
99730:M 23 Jun 12:28:53.639 # Can't handle RDB format version 9
99730:M 23 Jun 12:28:53.639 # Fatal error loading the DB: Invalid argument. Exiting.

原因是:
当前redis版本无法处理version:9的rdb文件,原因是此rdb文件为其他版本redis生成,当前版本redis无法兼容。也就说你的计算机以前装过其它版本的Redis。

解决方法:删除所有的dump.rdb文件,重新启动redis-server。

那么问题来了,这个文件在哪 ?按网上的路径,我找了半天没找到,最后在根目录下使用find 命令找到;然后删除重启

Redis即可。

 find / -name dump.rdb
B000000098663i:~ rongsong$ find / -name dump.rdb
find: /usr/sbin/authserver: Permission denied
/usr/local/var/db/redis/dump.rdb
find: /.Spotlight-V100: Permission denied
find: /.PKInstallSandboxManager-SystemSoftware: Permission denied
find: /Library/Application Support/Apple/ParentalControls/Users: Permission denied
find: /Library/Application Support/Apple/AssetCache/Data: Permission denied
find: /Library/Application Support/ApplePushService: Permission denied

---------------------------------------dump.rdb文件---------------------------------------------

简而言之,dump.rdb是由Redis服务器自动生成的
默认情况下 每隔一段时间redis服务器程序会自动对数据库做一次遍历,把内存快照写在一个叫做“dump.rdb”的文件里,这个持久化机制叫做SNAPSHOT。有了SNAPSHOT后,如果服务器宕机,重新启动redis服务器程序时redis会自动加载dump.rdb,将数据库状态恢复到上一次做SNAPSHOT时的状态。

至于多久做一次SNAPSHOT,SNAPSHOT文件的路径和文件名,你可以在redis的conf文件里指定。

除了SNAPSHOT,redis还支持AOF持久化。


最后可以采用如下命令检查Redis是否启动成功:

查看端口是否在:

lsof -i:6379

查看进程是否存在:

 ps -ef | grep redis

如果上述操作都没问题,就可以愉快的使用常见的Redis命令进行数据的增删改查啦。

标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~