Redis 主从服务器搭建

1. 利用 HomeBrew 安装 Redis

$ brew install redis
复制代码

HomeBrew 安装的软件会默认在 /usr/local/Cellar 路径下 Redis 的配置文件 redis.conf 存放在 /usr/local/etc 路径下。redis

2. Redis 命令

redis-server:启动 redis 服务器

启动 redis 服务器能够选择带配置文件启动和不带配置文件启动。bash

不带配置文件

启动命令:服务器

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 经过这句提示咱们能够知道若是启动服务器时没有输入配置文件路径的话,选择的是默认的配置文件路径。ui

带配置文件

启动命令:spa

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
复制代码

带配置文件启动 且指定某几个配置 配置名称前加 --

启动命令:code

redis-server /usr/local/etc/redis.conf --port 6480
复制代码

输出信息以下,能够看到启动端口就是咱们上述设置的端口:server

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.16379ip

不带 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> 复制代码

说明从服务器上是只读的,不能写入数据。

固然咱们也能够设置从服务器能够写数据,可是在从服务器上写的这部分数据是不能同步给主服务器的,因此不建议开启。

相关文章
相关标签/搜索