1、 利用 HomeBrew 安装 Redis
$ brew install redis
HomeBrew 安装的软件会默认在 /usr/local/Cellar
路径下 Redis 的配置文件 redis.conf
存放在 /usr/local/etc
路径下。
2、 Redis 命令
redis-server
:启动 redis 服务器
启动 redis 服务器可以选择带配置文件启动和不带配置文件启动。
不带配置文件
启动命令:
redis-server
输出信息:
➜ ~ redis-server
46905:C 16 Jul 11:43:13.067 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
46905:C 16 Jul 11:43:13.068 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=46905, just started
46905:C 16 Jul 11:43:13.068 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
46905:M 16 Jul 11:43:13.069 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 46905
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
46905:M 16 Jul 11:43:13.071 # Server initialized
46905:M 16 Jul 11:43:13.071 * Ready to accept connections
Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
通过这句提示我们可以知道如果启动服务器时没有输入配置文件路径的话,选择的是默认的配置文件路径。
带配置文件
启动命令:
redis-server /usr/local/etc/redis.conf
输出信息:
➜ ~ redis-server /usr/local/etc/redis.conf
46487:C 16 Jul 11:12:41.858 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
46487:C 16 Jul 11:12:41.859 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=46487, just started
46487:C 16 Jul 11:12:41.859 # Configuration loaded
46487:M 16 Jul 11:12:41.860 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 46487
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
46487:M 16 Jul 11:12:41.949 # Server initialized
46487:M 16 Jul 11:12:41.950 * DB loaded from disk: 0.001 seconds
46487:M 16 Jul 11:12:41.950 * Ready to accept connections
带配置文件启动 且指定某几个配置 配置名称前加 —
启动命令:
redis-server /usr/local/etc/redis.conf --port 6480
输出信息如下,可以看到启动端口就是我们上述设置的端口:
48859:C 16 Jul 14:00:13.142 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
48859:C 16 Jul 14:00:13.143 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=48859, just started
48859:C 16 Jul 14:00:13.143 # Configuration loaded
48859:M 16 Jul 14:00:13.144 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6480
| `-._ `._ / _.-' | PID: 48859
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
48859:M 16 Jul 14:00:13.146 # Server initialized
48859:M 16 Jul 14:00:13.147 * DB loaded from disk: 0.001 seconds
48859:M 16 Jul 14:00:13.147 * Ready to accept connections
redis-cli
:启动客户端
客户端默认连接的 IP 和 端口号是 127.0.0.1
和 6379
不带 IP 和 PORT
启动命令:
redis-cli
执行结果如下:
➜ ~ redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
原因是我们刚才启动的是 6480
端口,所以无法连接。
注:如果我们没有启动 redis 服务端,然后连接 redis 客户端的话,错误和上述信息一致。
带 IP 和 PORT
启动命令:
redis-cli -h 127.0.0.1 -p 6480
执行成功结果:
➜ ~ redis-cli -h 127.0.0.1 -p 6480
127.0.0.1:6480>
3、 主从环境搭建
1、 复制配置文件
cp /usr/local/etc/redis.conf /usr/local/etc/redis_slave.conf
1、 修改 redis_slave.conf
配置文件,需要修改的位置如下:
port 6380
slaveof 127.0.0.1 6379
注意:slaveof 原始配置文件格式如下:slaveof <masterip> <masterport>
,尖括号也需要去掉。
启动主从服务器
启动主服务器:
redis-server /usr/local/etc/redis.conf
输出信息如下:
49160:C 16 Jul 14:13:27.822 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
49160:C 16 Jul 14:13:27.822 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=49160, just started
49160:C 16 Jul 14:13:27.822 # Configuration loaded
49160:M 16 Jul 14:13:27.824 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 49160
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
49160:M 16 Jul 14:13:27.828 # Server initialized
49160:M 16 Jul 14:13:27.828 * DB loaded from disk: 0.000 seconds
49160:M 16 Jul 14:13:27.828 * Ready to accept connections
启动从服务器
启动命令:
redis-server /usr/local/etc/redis_slave.conf
输出信息如下:
49193:C 16 Jul 14:14:30.648 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
49193:C 16 Jul 14:14:30.649 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=49193, just started
49193:C 16 Jul 14:14:30.649 # Configuration loaded
49193:S 16 Jul 14:14:30.651 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6380
| `-._ `._ / _.-' | PID: 49193
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
49193:S 16 Jul 14:14:30.653 # Server initialized
49193:S 16 Jul 14:14:30.653 * DB loaded from disk: 0.000 seconds
49193:S 16 Jul 14:14:30.653 * Ready to accept connections
49193:S 16 Jul 14:14:30.653 * Connecting to MASTER 127.0.0.1:6379
49193:S 16 Jul 14:14:30.653 * MASTER <-> SLAVE sync started
49193:S 16 Jul 14:14:30.654 * Non blocking connect for SYNC fired the event.
49193:S 16 Jul 14:14:30.654 * Master replied to PING, replication can continue...
49193:S 16 Jul 14:14:30.654 * Partial resynchronization not possible (no cached master)
49193:S 16 Jul 14:14:30.655 * Full resync from master: 99edd0e3826785242217fb243618d96ace866a42:0
49193:S 16 Jul 14:14:30.708 * MASTER <-> SLAVE sync: receiving 176 bytes from master
49193:S 16 Jul 14:14:30.708 * MASTER <-> SLAVE sync: Flushing old data
49193:S 16 Jul 14:14:30.709 * MASTER <-> SLAVE sync: Loading DB in memory
49193:S 16 Jul 14:14:30.709 * MASTER <-> SLAVE sync: Finished with success
可以看到其中有一条日志,Connecting to MASTER 127.0.0.1:6379
,说明从服务器连接上主服务器了。
我们再看一下主服务器的日志:
49160:M 16 Jul 14:13:27.828 # Server initialized
49160:M 16 Jul 14:13:27.828 * DB loaded from disk: 0.000 seconds
49160:M 16 Jul 14:13:27.828 * Ready to accept connections
49160:M 16 Jul 14:14:30.654 * Slave 127.0.0.1:6380 asks for synchronization
49160:M 16 Jul 14:14:30.654 * Full resync requested by slave 127.0.0.1:6380
49160:M 16 Jul 14:14:30.654 * Starting BGSAVE for SYNC with target: disk
49160:M 16 Jul 14:14:30.655 * Background saving started by pid 49194
49194:C 16 Jul 14:14:30.657 * DB saved on disk
49160:M 16 Jul 14:14:30.708 * Background saving terminated with success
49160:M 16 Jul 14:14:30.708 * Synchronization with slave 127.0.0.1:6380 succeeded
我们看到 Synchronization with slave 127.0.0.1:6380 succeeded
这条日志,主从服务器建立成功了。
启动客户端
启动客户端的时候,我们需要指定端口
启动命令如下:
redis-cli -p 6379
127.0.0.1:6379>
redis-cli -p 6380
127.0.0.1:6380>
主从测试
我们在主服务器上插入一条测试数据
127.0.0.1:6379> set test1 test1
OK
127.0.0.1:6379>
我们再从从服务器上尝试读取测试数据
127.0.0.1:6380> get test1
"test1"
127.0.0.1:6380>
发现能够正常读取得到,说明主从服务器搭建是成功的。
反过来,我们再测试一下。
在从服务器上插入一条测试数据:
127.0.0.1:6380> set test2 test2
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380>
说明从服务器上是只读的,不能写入数据。
当然我们也可以设置从服务器可以写数据,但是在从服务器上写的这部分数据是不能同步给主服务器的,所以不建议开启。