Redis实战-Redis安装(二)
Redis安装
1.Windows下安装Redis
下载地址:https://github.com/MSOpenTech/redis/releases
Redis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择,这里我下载 Redis-x64-3.0.501.zip压缩包到 G 盘,解压后,将文件夹重新命名为 redis。
image打开文件夹,内容如下:
image打开一个 cmd 窗口 使用 cd 命令切换目录到 G:\developTool\Redis 运行:
redis-server.exe redis.windows.conf
image如上图,我们现在已经打开了Redis的服务端了,接下来我们再打开一个cmd窗口,第一个cmd窗口不要关闭,不然就无法访问服务端了。
切换到redis目录下运行:
redis-cli.exe
image这个时候我们已经打开了redis的客户端,注意这个打开客户端的命令,全部命令应该是:
redis-cli.exe -h 127.0.0.1 -p 6379
我这里对主机和端口省略了,这里的-h表示主机,-p表示端口,这两个都是默认值,127.0.0.1 是本机 IP ,6379 是 redis 服务端口。其实我们可使用上述的命令连接远程的redis库。
现在我们输入 PING 命令:
image以上说明我们成功安装了redis。
为了方便起见,我们通常会将Redis做成一个服务,设置成开机自起,这样就不用每次启动项目还需要手动的开启redis服务端。
从CMD命令窗口中进入Redis的安装目录,如:G:\developTool\Redis,然后执行下面的命令:
redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis_new
image此服务加载的配置文件是redis.windows.conf,不带service的,下一节中会详细介绍Redis的配置文件。
这个时候我们将Redis_new的服务设置成自动即可。
image补充:
启动redis服务:
redis-server --service-start
删除redis服务:
redis-server --service-uninstall
2.Linux(CentOs 6.7)下安装Redis
下载地址:http://redis.io/download,下载最新稳定版本。
本教程使用的最新文档版本为 2.8.17,下载并安装:
#使用wget工具直接下载redis压缩包
[hand@localhost opt]$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
#解压
[hand@localhost opt]$ tar xzf redis-2.8.17.tar.gz
#进入解压后的文件夹
[hand@localhost opt]$ cd redis-2.8.17
#编译
[hand@localhost opt]$ make
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
下面启动redis服务.
[hand@localhost redis-2.8.17]$ cd src
# 启动redis服务
[hand@localhost src]$ ./redis-server
注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。
#该方法指的是启动redis服务时候指定配置文件是redis.config,这个配置文件是可以手动去配置的
[hand@localhost src]$ ./redis-server ../redis.conf
[8246] 22 Apr 21:26:22.341 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
[8246] 22 Apr 21:26:22.341 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
[8246] 22 Apr 21:26:22.341 # Current maximum open files is 1024. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.17 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8246
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[8246] 22 Apr 21:26:22.343 # Server started, Redis version 2.8.17
[8246] 22 Apr 21:26:22.345 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[8246] 22 Apr 21:26:22.345 * DB loaded from disk: 0.000 seconds
[8246] 22 Apr 21:26:22.345 * The server is now ready to accept connections on port 6379
redis.conf 是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。 比如:
[hand@localhost src]$ ./redis-cli
127.0.0.1:6379> set key ping
OK
127.0.0.1:6379> get key
"ping"
127.0.0.1:6379>
注意上述打开客户端的时候需要重开一个窗口,否则直接退出的话Redis服务器会断开。
为了防止Redis服务器会话断开,而导致Redis服务端关闭,则需要指定Redis为守护进程,打开redis.conf,把daemonize设置为yes
[hand@localhost src]$ vi redis.config
daemonize yes
然后再次重启redis,同时指定配置文件为redis.config即可。
[hand@localhost src]$ ./redis-cli redis.config