安裝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
說明確實已關閉
安裝phpredis擴展
brew install php70-redis #這里根據你的PHP版本決定,我的是php70,所以安裝php70的擴展
編輯你的php.ini,再最后添加上下面代碼:
extension=redis.so
檢查是否有redis擴展
php -m | grep redis
最后PHP測試代碼:
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->set('test',"11111111111"); $result = $redis->get('test'); var_dump($result); ?>
瀏覽器顯示出下圖表示安裝成功: