MAC下使用redis

REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。
Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

Redis有三个主要特点,使它优越于其它键值数据存储系统

  • Redis将其数据库完全保存在内存中,仅使用磁盘进行持久化。
  • 与其它键值数据存储相比,Redis有一组相对丰富的数据类型。
  • Redis可以将数据复制到任意数量的从机中。

下载

官网下载redis,并解压 redis-3.2.9.tar.gz/usr/local
如果官网无法访问 可以从 这里 下载

测试

  1. 解压 tar zxvf redis-3.2.9.tar.gz
  2. 移动到 mv redis-3.2.9 /usr/local/
  3. 编译测试 cd /usr/local/redis-3.2.9
  4. 执行 sudo make test
    提示 All tests passed without errors!

安装

编译安装 sudo make install
提示

1
2
3
4
5
6
7
8
9
10
11
firstdeMac-mini:redis-3.2.9 syc$ sudo make install
cd src && /Applications/Xcode.app/Contents/Developer/usr/bin/make install

Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL
firstdeMac-mini:redis-3.2.9 syc$

启动、关闭

启动Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
firstdeMac-mini:~ syc$ pwd
/Users/firstmac
firstdeMac-mini:~ syc$ redis-server
11404:C 13 Jul 17:00:47.784 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
11404:M 13 Jul 17:00:47.786 * Increased maximum number of open files to 10032 (it was originally set to 7168).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 11404
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

11404:M 13 Jul 17:00:47.787 # Server started, Redis version 3.2.9
11404:M 13 Jul 17:00:47.787 * DB loaded from disk: 0.000 seconds
11404:M 13 Jul 17:00:47.787 * The server is now ready to accept connections on port 6379

检查启动

new Window 输入

1
2
3
4
5
6
7
8
Last login: Thu Jul 13 16:59:09 on ttys007
firstdeMac-mini:~ syc$ pwd
/Users/firstmac
firstdeMac-mini:~ syc$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
firstdeMac-mini:~ syc$

关闭

1
2
3
4
5
6
firstdeMac-mini:~ syc$ redis-cli
127.0.0.1:6379> shutdown
not connected> ping
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
firstdeMac-mini:~ syc$

强行关闭

强行终止redis进程可能会导致数据丢失,因为redis可能正在将内存数据同步到硬盘中。

1
2
ps axu|grep redis  ## 查找redis-server的PID
kill -9 PID

配置

  • /usr/local/redis-3.2.9 目录下建立 bin etc db 三个目录
  • /usr/local/redis-3.2.9/src 目录下的 mkreleasehdr.sh redis-benchmark redis-check-dump redis-cli redis-server 拷贝到 bin 目录
  • etc 下,参考原 /usr/local/redis-3.2.9目录下的redis.conf ,新建一个redis.conf
    我设置了密码 requirepass 123456

启动

进入 /usr/local/redis-3.2.9 目录,执行 redis-server etc/redis.conf 指定配置文件

1
2
3
4
5
firstdeMac-mini:~ syc$ cd /usr/local/redis-3.2.9
firstdeMac-mini:redis-3.2.9 syc$ pwd
/usr/local/redis-3.2.9
firstdeMac-mini:redis-3.2.9 syc$ redis-server etc/redis.conf
firstdeMac-mini:redis-3.2.9 syc$

检查是否启动

别忘记,我配置过密码的,所以出现认证问题

1
2
3
4
5
6
firstdeMac-mini:~ syc$ pwd
/Users/firstmac
firstdeMac-mini:~ syc$ redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379>

使用 auto 输入密码

1
2
3
4
5
6
7
8
9
firstdeMac-mini:~ syc$ pwd
/Users/firstmac
firstdeMac-mini:~ syc$ redis-cli
127.0.0.1:6379> auth "123456"
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
firstdeMac-mini:~ syc$

本文标题:MAC下使用redis

文章作者:史彦超

发布时间:2017年07月12日 - 15:07

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2017/07/12/2017-07-13-MAC%E4%B8%8B%E4%BD%BF%E7%94%A8redis/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here