安装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
终端输出
8568:M 11 Sep 21:37:46.839 * Increased maximum number of open files to 10032 (it was originally set to 256).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8568
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
8568:M 11 Sep 21:37:46.844 # Server started, Redis version 3.2.3
8568:M 11 Sep 21:37:46.845 * The server is now ready to accept connections on port 6379
可以看出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
说明确实已关闭