mac上安装使用redis

安装redis

redis的安装方法有2种:下载源码编译安装和使用homebrew安装。本文采用后一种方法,如需下载源码编译安装参考 mac下安装配置redis。通过homebrew安装redis:
$ brew install redis
终端输出

==> Downloading http://download.redis.io/releases/redis-3.2.3.tar.gz
######################################################################## 100.0%
==> make install PREFIX=/usr/local/Cellar/redis/3.2.3 CC=clang
==> Caveats
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf
==> Summary
  /usr/local/Cellar/redis/3.2.3: 10 files, 1.7M, built in 21 seconds

从以上日志输出可以看出,如果需要给redis服务端指定配置文件,启动命令应该是这样的:

$ redis-server /usr/local/etc/redis.conf

配置文件

安装完成后redis默认的配置文件redis.conf位于

/usr/local/etc

同时,redis-sentinel.conf也在这里。

使用cat命令查看redis.conf:

$ cat /usr/local/etc/redis.conf

终端输出文件内容(删掉了大部分注释):

bind 127.0.0.1 ::1
bind 127.0.0.1
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized.
daemonize no


supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""


# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

################################ SNAPSHOTTING  ################################

save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb

# The working directory.
dir /usr/local/var/db/redis/

################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

################################## SECURITY ###################################

################################### LIMITS ####################################

############################## APPEND ONLY MODE ###############################
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

################################ LUA SCRIPTING  ###############################
lua-time-limit 5000

################################ REDIS CLUSTER  ###############################


################################## SLOW LOG ##################################

slowlog-max-len 128

################################ LATENCY MONITOR ##############################

latency-monitor-threshold 0

############################# EVENT NOTIFICATION ##############################

notify-keyspace-events ""

############################### ADVANCED CONFIG ###############################
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

官网上对于如何配置redis的描述:

Redis is able to start without a configuration file using a built-in default configuration, however this setup is only recommended for testing and development purposes.
The proper way to configure Redis is by providing a Redis configuration file, usually called redis.conf.

根据以上内容,如果启动时不指定配置文件,redis会使用程序中内置的默认配置.但是只有在开发和测试阶段才考虑使用内置的默认配置,正式环境最好还是提供配置文件,并且一般命名为redis.conf

启动redis

可以通过以下命令启动redis:

$ redis-server /usr/local/etc/redis.conf &

终端输出

[1] 94381
94381:C 28 Jan 2019 13:36:47.371 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
94381:C 28 Jan 2019 13:36:47.371 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=94381, just started
94381:C 28 Jan 2019 13:36:47.371 # Configuration loaded
94381:M 28 Jan 2019 13:36:47.372 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 94381
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

94381:M 28 Jan 2019 13:36:47.377 # Server initialized
94381:M 28 Jan 2019 13:36:47.377 * DB loaded from disk: 0.000 seconds
94381:M 28 Jan 2019 13:36:47.377 * Ready to accept connections

可以看出redis服务器启动成功,并在监听6379端口的网络连接。
注意: 使用命令$ redis-server也可以启动,此时并不会加载任何配置文件,使用的是程序中内置(built-in)的默认配置.

检测redis服务器是否启动

重新打开一个终端窗口,输入命令
$ redis-cli ping
该终端输出
pong
说明服务器运作正常。

关闭redis

关闭redis有2种方法:

方法1

在执行启动命令的终端窗口使用ctrl+c,此时第一个窗口输出

8773:M 11 Sep 21:46:26.581 # User requested shutdown...
8773:M 11 Sep 21:46:26.581 * Saving the final RDB snapshot before exiting.
8773:M 11 Sep 21:46:26.583 * DB saved on disk
8773:M 11 Sep 21:46:26.583 * Removing the pid file.
8773:M 11 Sep 21:46:26.583 # Redis is now ready to exit, bye bye...

然后在另外一个终端窗口执行$ redis-cli ping,输出
Could not connect to Redis at 127.0.0.1:6379: Connection refused
说明确实已关闭

方法2

在另外一个终端窗口执行$ redis-cli shutdown,此时第一个窗口输出

8773:M 11 Sep 21:46:26.581 # User requested shutdown...
8773:M 11 Sep 21:46:26.581 * Saving the final RDB snapshot before exiting.
8773:M 11 Sep 21:46:26.583 * DB saved on disk
8773:M 11 Sep 21:46:26.583 * Removing the pid file.
8773:M 11 Sep 21:46:26.583 # Redis is now ready to exit, bye bye...

然后在另外一个终端窗口执行$ redis-cli ping,输出
Could not connect to Redis at 127.0.0.1:6379: Connection refused
说明确实已关闭

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容