mac与windows上部署使用Redis
windows下Redis安装
在Redis的官网下载页上有各种各样的版本,由于redis官网不支持windows,但是我们伟大的windows家族还是召唤了一群小伙伴开发了win版的redis。要在windows上部署的,要去GitHub上下载。
下载完成你会看到一个Redis-x64.rar,文件解压文件出现如图的结构:
启动Redis
下面介绍两种启动方式:
第一种、直接运行redis-server.exe文件,你会看到一个弹出命令框:
这样redis就启动了,
第二种,在解压的redis目录下运行
redis-server redis.windows.conf
结果就悲剧了,提示:
QForkMasterInit: system error caught. error code=0x000005af, message=VirtualAllocEx failed.: unknown error
原因是内存分配的问题。解决方法有两个,第一:启动的时候使用--maxmemory
命令限制Redis的内存:
redis-server redis.windows.conf --maxmemory 200m
第二种方法就是修改配置文件redis.windows.conf
:
maxmemory 209715200
注意单位是字节,改完后如下:
之后再运行redis-server redis.windows.conf
就可以启动了:
但是这种方式关闭cmd窗口就会关闭Redis,难道服务器上要一直开着吗?这显然是不科学的,下面看怎么在服务器上部署。
windows下部署redis
下面我们来看怎样把redis部署到windows上的,这样实现了我们想要的开机自启动。
redis-server --service-install redis.windows.conf
安装完之后,就可看到Redis已经作为windows服务了:
但是安装好之后,Redis并没有启动,启动命令如下:
redis-server --service-start
这样就在windows下部署完成,并启动了。
停止命令:
redis-server --service-stop
还可以安装多个实例并命名这些实例
redis-server --service-install –service-name redisService1 –port 10001 redis-server --service-start –service-name redisService1 redis-server --service-install –service-name redisService2 –port 10002 redis-server --service-start –service-name redisService2 redis-server --service-install –service-name redisService3 –port 10003 redis-server --service-start –service-name redisService3
卸载命令:
redis-server --service-uninstall
最后提示一下:2.8版本的不支持32位系统,32位系统要去下载2.6版本的。2.6版本的无法像上面一样方便的部署,它提供一个叫RedisWatcher的程序来运行redis server,Redis停止后会自动重启。
mac下redis安装
官网
文档 https://redis.io/documentation
使用homebrew安装
redis的安装方法有2种:下载源码编译安装和使用homebrew安装。本文采用后一种方法,如需下载源码编译安装参考 mac下安装配置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-server
终端打印日志如下:
34743:C 27 Mar 20:11:55.715 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 34743:M 27 Mar 20:11:55.716 * 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: 34743 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 34743:M 27 Mar 20:11:55.717 # Server started, Redis version 3.2.3 34743:M 27 Mar 20:11:55.717 * The server is now ready to accept connections on port 6379
可以看到启动时不指定配置文件会使用默认配置文件。
关于默认配置文件
默认的配置文件就是使用homebrew安装时提示的/usr/local/etc/redis.conf,使用cat命令查看:
$ 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 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.conf