redis-01-安装配置

redis

说明

本篇文章将介绍在三大主流操作系统(Linux,mac,windows)上安装redis。

Linux上的安装介绍自不必说,很少有人用除了Linux之外的其他操作系统在生成环境下部署redis吧……

但是在开发的时候,很有可能不是在Linux环境下开发的,有必要在自己的操作系统上装一个来开发测试用。所以此处也介绍在mac和windows下的安装。

1 linux[CentOS-6.8-x64]安装

1.1 下载解压

wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxvf redis-3.2.8.tar.gz

1.2 编译安装

cd redis-3.2.8
make PREFIX=/usr/local/bin install # 目录可以自己指定
  • 至此,安装完成,可以看看生成了哪些东西
# 在/usr/local/bin下,已经在环境变量里了
[root@h4 redis-3.2.8]$ ls -l /usr/local/bin/
total 26348
-rwxr-xr-x. 1 root root 5580319 Apr  4 15:27 redis-benchmark
-rwxr-xr-x. 1 root root   22185 Apr  4 15:27 redis-check-aof
-rwxr-xr-x. 1 root root 7829986 Apr  4 15:27 redis-check-rdb
-rwxr-xr-x. 1 root root 5709187 Apr  4 15:27 redis-cli
lrwxrwxrwx. 1 root root      12 Apr  4 15:27 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 7829986 Apr  4 15:27 redis-server

1.3 配置

  • 复制redis.conf
# 从redis解压目录将redis.conf配置文件复制到/data/redis
# 此处的/data/redis目录是本人的习惯,按你自己的需要来就行
cp redis.conf /data/redis/
  • 修改配置文件
# 编辑配置文件/data/redis/redis.conf
# 修改以下几项
vim /data/redis/redis.conf

# 后台模式运行
daemonize yes
# 修改日志文件路径
logfile "/data/redis/log.log"
# 关闭保护模式,虽然这样不安全,但是从其他主机连接redis时是要关闭保护模式的
protected-mode no

1.4 将redis做成系统服务[可选]

[root@h4 redis-3.2.8]$ utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /data/redis/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /data/redis/log.log
Please select the data directory for this instance [/var/lib/redis/6379] /data/redis
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /data/redis/redis.conf
Log file       : /data/redis/log.log
Data dir       : /data/redis
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!

1.5 启停控制

  • 如果你执行了步骤1.4,即你把redis做成了系统服务,可以像下面这样操作:
# 启动Redis
[root@h4 redis-3.2.8]$ service redis_6379 start
Starting Redis server...
# 查看redis进程
[root@h4 redis-3.2.8]$ ps -ef | grep redis
root      17160      1  0 15:43 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
root      17164   3968  0 15:43 pts/1    00:00:00 grep --color redis
# 查看redis使用的端口
[root@h4 redis-3.2.8]$ netstat -tnpl | grep 6379
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      17160/redis-server 
# 连接客户端测试
[root@h4 redis-3.2.8]$ redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
# 停止Redis
[root@h4 redis-3.2.8]$ service redis_6379 stop
Stopping ...
Redis stopped
  • 如果没有做成系统服务,可以像下面这样操作:
# 启动Redis
[root@h4 redis-3.2.8]# redis-server /data/redis/redis.conf
# 查看redis进程
[root@h4 redis-3.2.8]# ps -ef | grep redis
root      17238      1  0 15:48 ?        00:00:00 redis-server 127.0.0.1:6379
root      17243   3968  0 15:48 pts/1    00:00:00 grep --color redis
# 查看redis使用的端口
[root@h4 redis-3.2.8]# netstat -tlnp | grep 6379
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      17238/redis-server
# 连接客户端测试
[root@h4 redis-3.2.8]# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
# 停止Redis
[root@h4 redis-3.2.8]# redis-cli shutdown

2 mac安装

其实在mac下安装和在Linux(CentOS-6.8-x64)下是非常类似的。

在mac下安装redis最简单的方式就是使用brew install redis来一键搞定了。

如果你不喜欢瞎折腾,就是要最快的傻瓜式安装,那你就用上面的brew install redis吧。

否则,可以参考我们这里介绍的手动编译安装的方式:

2.1 下载解压

$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
$ tar -zxvf redis-3.2.8.tar.gz 

2.2 编译安装

$ cd redis-3.2.8/
$ make PREFIX=/usr/local/bin/redis install
  • 由于此处我将redis安装到了/usr/local/bin/redis,不是默认的/usr/local/bin,所以得改下环境变量
$ vim ~/.bash_profile
# 加入如下配置
export redis_home=/usr/local/bin/redis
export PATH=$PATH:$redis_home/bin

# 刷新环境变量
$ source ~/.bash_profile
# 看看配好没?
$ which redis-server
/usr/local/bin/redis/bin/redis-server

2.3 配置

  • 复制redis.conf
$ mkdir /usr/local/etc/redis
$ cp redis.conf /usr/local/etc/redis
  • 编辑配置文件
$ cd /usr/local/etc/redis/
$ vim redis.conf

# 按需修改如下几项
# 绑定所有网络地址
bind 0.0.0.0
# 数据目录(需事先手动建立目录)
dir /Users/hylexus/data/redis/6379/
# 日志文件(需事先手动建立目录)
logfile "/Users/hylexus/data/redis/log.log"
# 后台运行
daemonize yes
# 关闭保护模式
protected-mode no

2.4 启停控制

# 启动Redis
$ redis-server /usr/local/etc/redis/redis.conf 
# 查看redis进程
$ ps -ef | grep redis
  501  5456     1   0  4:47下午 ??         0:00.01 redis-server 0.0.0.0:6379 
# 连接客户端
$ redis-cli -p 6379
# 测试
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> ping redis
"redis"
127.0.0.1:6379> exit
# 停止Redis
$ redis-cli shutdown

3 windows安装

3.1 下载msi安装包安装

以下是redis官网下载页面的部分截图:

redis-for-windows

也就是说redis是不支持windows的。

但是Microsoft Open Tech group维护了一个windows版本的redis。

地址在这里:https://github.com/MSOpenTech/redis

去这里下载个msi安装包安装就行了:https://github.com/MSOpenTech/redis/releases

一路狂点鼠标[Next],安装完成即可。

  • 有点需要注意的 最好在安装的时候勾选将redis可执行文件加入环境变量

3.2 将redis做成windows服务

具体的步骤请看这里:https://raw.githubusercontent.com/MSOpenTech/redis/3.0/Windows%20Service%20Documentation.md

C:\Program Files\Redis>redis-server --service-install redis.windows-service.conf --loglevel verbose

可以去服务窗口看看:

redis-windows-service

3.3 配置

配置文件都在安装目录下:C:/Program Files/Redis

按需修改即可.

3.4 启停控制

  • 直接去windows提供的服务管理控制台(3.2中的截图)最方便
  • 命令方式如下:
# 启动Redis
# 注意:如果已经启动了,会提示错误。但是错误并不会告诉你已经启动了………………
C:\Users\hylexus> redis-server --service-start
[13032] 04 Apr 17:58:42.168 # HandleServiceCommands: system error caught. error code=1056, message = StartService failed: unknown error

# 先停止,再试试启动会不会正常
C:\Users\hylexus> redis-server --service-stop
[5308] 04 Apr 17:58:47.284 # Redis service successfully stopped.
# 再次尝试启动[成功]
C:\Users\hylexus> redis-server --service-start
[15200] 04 Apr 17:58:52.940 # Redis service successfully started.

# 连接客户端测试
C:\Program Files\Redis>redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> ping haha
"haha"
127.0.0.1:6379> exit

# 停止服务
C:\Program Files\Redis>redis-server --service-stop
[18564] 04 Apr 17:59:22.700 # Redis service successfully stopped.

C:\Program Files\Redis>

参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值