安裝redis
redis的安裝方法有2種:下載源碼編譯安裝和使用homebrew安裝察皇。本文采用后一種方法痪宰,如需下載源碼編譯安裝參考 mac下安裝配置redis又兵。通過(guò)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服務(wù)端指定配置文件跨扮,啟動(dòng)命令應(yīng)該是這樣的:
$ redis-server /usr/local/etc/redis.conf
配置文件
安裝完成后redis默認(rèn)的配置文件redis.conf位于
/usr/local/etc
同時(shí),redis-sentinel.conf也在這里携龟。
使用cat命令查看redis.conf:
$ cat /usr/local/etc/redis.conf
終端輸出文件內(nèi)容(刪掉了大部分注釋?zhuān)?/p>
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
官網(wǎng)上對(duì)于如何配置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.
根據(jù)以上內(nèi)容,如果啟動(dòng)時(shí)不指定配置文件,redis會(huì)使用程序中內(nèi)置的默認(rèn)配置.但是只有在開(kāi)發(fā)和測(cè)試階段才考慮使用內(nèi)置的默認(rèn)配置,正式環(huán)境最好還是提供配置文件勘高,并且一般命名為redis.conf
啟動(dòng)redis
可以通過(guò)以下命令啟動(dòng)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服務(wù)器啟動(dòng)成功峡蟋,并在監(jiān)聽(tīng)6379端口的網(wǎng)絡(luò)連接。
注意: 使用命令$ redis-server
也可以啟動(dòng),此時(shí)并不會(huì)加載任何配置文件,使用的是程序中內(nèi)置(built-in)的默認(rèn)配置.
檢測(cè)redis服務(wù)器是否啟動(dòng)
重新打開(kāi)一個(gè)終端窗口华望,輸入命令
$ redis-cli ping
該終端輸出
pong
說(shuō)明服務(wù)器運(yùn)作正常蕊蝗。
關(guān)閉redis
關(guān)閉redis有2種方法:
方法1
在執(zhí)行啟動(dòng)命令的終端窗口使用ctrl+c,此時(shí)第一個(gè)窗口輸出
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...
然后在另外一個(gè)終端窗口執(zhí)行$ redis-cli ping
,輸出
Could not connect to Redis at 127.0.0.1:6379: Connection refused
說(shuō)明確實(shí)已關(guān)閉
方法2
在另外一個(gè)終端窗口執(zhí)行$ redis-cli shutdown,此時(shí)第一個(gè)窗口輸出
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...
然后在另外一個(gè)終端窗口執(zhí)行$ redis-cli ping
,輸出
Could not connect to Redis at 127.0.0.1:6379: Connection refused
說(shuō)明確實(shí)已關(guān)閉