Redis主从服务器同步搭建(基础篇)

本例实验的环境如下:

Linux redis1 2.6.32-642.11.1.el6.x86_64

主服务器IP:192.168.0.227

从服务器IP:192.168.0.228

Redis 3.2.8下载地址: http://download.redis.io/releases/redis-3.2.8.tar.gz

yum源使用阿里云

 

声明:本实例仅仅为redis主从服务器同步的实现,由于博主也是首次接触redis,对redis.conf配置参数仍未有过多了解,参数优化会在了解redis.conf配置文件试验后补充更新

 

以下命令主、从服务器都需要执行:

先确定时间设置是否正确

[root@redis1 ~]# date

 

时间不正确的话对时间进行修改

[root@redis1 ~]# date -s “20170323 1200”

 

安装基础软件包

[root@redis1 ~]# yum install -y gcc gcc-c++ make

 

先创建redis相关目录

[root@redis1 ~]# mkdir -p /usr/local/redis/{etc,bin,var}

[root@redis1 ~]# mkdir -p /data/logs/redis/data/dbcache

 

解压及安装redis

[root@redis1 ~]# tar xzvf redis-3.2.8.tar.gz

[root@redis1 ~]# cd redis-3.2.8

[root@redis1 redis-3.2.8]# make

[root@localhost redis-3.2.8]# cd src && cp redis-server redis-cli redis-benchmark /usr/local/redis/bin

[root@localhost redis-3.2.8]# cp -a redis.conf /usr/local/redis/etc

[root@redis1 redis-3.2.8]# cd /usr/local/redis

[root@redis1 redis]# vim etc/redis.conf


主服务器配置文件内容修改如下:


daemonize no 改为 daemonize yes

pidfile /var/run/redis_6379.pid 改为 pidfile /usr/local/redis/var/redis_master.pid

# bind 127.0.0.1 ::1 改为 bind 0.0.0.0

bind 127.0.0.1 改为 #bind 127.0.0.1

# unixsocket /tmp/redis.sock 改为 unixsocket /usr/local/redis/var/redis.sock

# unixsocketperm 700 改为unixsocketperm 755

#timeout 0 改为 timeout300

loglevel notice改为 loglevel verbose

logfile "" 改为 logfile"/data/logs/redis/redis.log"

dir ./ 改为 dir  /data/logs/redis/data/dbcache

# requirepass foobared 改为 requirepass traumerei


把主服务器的redis.conf复制到从服务器的/usr/local/redis/etc目录下,并修改以下内容:

# slaveof 改为 slaveof 192.168.0.227 6379

# masterauth 改为 masterauth traumerei


测试

主从服务器都执行以下命令启动redis:

[root@localhost ~]# cd /usr/local/redis

[root@localhost redis]# bin/redis-server etc/redis.conf

 

启动之后执行以下命令查看服务器主从服务器连接状况:

[root@localhost redis]# tail -n 200 /data/logs/redis/redis.log

主服务器:

5196:M 23 Mar 15:23:50.830 * Increased maximumnumber of open files to 10032 (it was originally set to 1024).

               _._                                                 

          _.-``__ ''-._                                            

     _.-``    `.  `_. ''-._           Redis 3.2.8(00000000/0) 64 bit

  .-``.-```.  ```\/    _.,_ ''-._                                  

 (    '     ,       .-`  | `,   )     Running in standalone mode

 |`-._`-...-`__...-.``-._|'` _.-'|     Port: 6379

 |    `-._  `._    /     _.-'   |     PID: 5196

  `-._    `-._ `-./  _.-'    _.-'                                  

 |`-._`-._   `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |          http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._   `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                   

     `-._    `-.__.-'    _.-'                                      

         `-._        _.-'                                          

             `-.__.-'                                              

 

5196:M 23 Mar 15:23:50.888 # WARNING: The TCPbacklog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconnis set to the lower value of 128.

5196:M 23 Mar 15:23:50.888 # Server started, Redisversion 3.2.8

5196:M 23 Mar 15:23:50.888 # WARNINGovercommit_memory is set to 0! Background save may fail under low memorycondition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.confand then reboot or run the command 'sysctl vm.overcommit_memory=1' for this totake effect.

5196:M 23 Mar 15:23:50.889 * The server is nowready to accept connections on port 6379

5196:M 23 Mar 15:23:50.889 * The server is nowready to accept connections at /usr/local/redis/var/redis.sock

5196:M 23 Mar 15:24:06.279 - 0 clients connected (0slaves), 759720 bytes in use

5196:M 23 Mar 15:24:11.400 - 0 clients connected (0slaves), 759720 bytes in use

5196:M 23 Mar 15:24:13.596 - Accepted192.168.0.228:41013

5196:M 23 Mar 15:24:13.598 * Slave192.168.0.228:6379 asks for synchronization

5196:M 23 Mar 15:24:13.598 * Full resync requestedby slave 192.168.0.228:6379

5196:M 23 Mar 15:24:13.598 * Starting BGSAVE forSYNC with target: disk

5196:M 23 Mar 15:24:13.598 * Background savingstarted by pid 5232

5232:C 23 Mar 15:24:13.677 * DB saved on disk

5232:C 23 Mar 15:24:13.678 * RDB: 0 MB of memoryused by copy-on-write

5196:M 23 Mar 15:24:13.768 * Background savingterminated with success

5196:M 23 Mar 15:24:13.768 * Synchronization withslave 192.168.0.228:6379 succeeded

5196:M 23 Mar 15:24:16.565 - 0 clients connected (1slaves), 1829192 bytes in use

5196:M 23 Mar 15:24:21.658 - 0 clients connected (1slaves), 1829192 bytes in use

5196:M 23 Mar 15:24:26.728 - 0 clients connected (1slaves), 1829192 bytes in use

 

 

从服务器:

4915:S 23 Mar 15:23:24.354 * Increased maximumnumber of open files to 10032 (it was originally set to 1024).

               _._                                                 

          _.-``__ ''-._                                            

     _.-``    `.  `_. ''-._           Redis 3.2.8(00000000/0) 64 bit

  .-``.-```.  ```\/    _.,_ ''-._                                  

 (    '     ,       .-`  | `,   )     Running in standalone mode

 |`-._`-...-`__...-.``-._|'` _.-'|     Port: 6379

 |    `-._   `._   /     _.-'    |    PID: 4915

  `-._    `-._ `-./  _.-'    _.-'                                  

 |`-._`-._   `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |          http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._   `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                   

     `-._    `-.__.-'    _.-'                                      

         `-._        _.-'                                          

             `-.__.-'                                              

4915:S 23 Mar 15:23:40.785 * Connecting to MASTER192.168.0.227:6379

4915:S 23 Mar 15:23:40.786 * MASTER <-> SLAVEsync started

4915:S 23 Mar 15:23:40.786 * Non blocking connectfor SYNC fired the event.

4915:S 23 Mar 15:23:40.786 * Master replied to PING,replication can continue...

4915:S 23 Mar 15:23:40.787 * Partialresynchronization not possible (no cached master)

4915:S 23 Mar 15:23:40.788 * Full resync frommaster: 4e4da19727bbc66115773dea75be329d04c24f13:1

4915:S 23 Mar 15:23:40.959 * MASTER <-> SLAVEsync: receiving 76 bytes from master

4915:S 23 Mar 15:23:40.959 * MASTER <-> SLAVEsync: Flushing old data

4915:S 23 Mar 15:23:40.959 * MASTER <-> SLAVEsync: Loading DB in memory

4915:S 23 Mar 15:23:40.959 * MASTER <-> SLAVEsync: Finished with success

4915:S 23 Mar 15:23:44.880 - 1 clients connected (0slaves), 780600 bytes in use

4915:S 23 Mar 15:23:49.944 - 1 clients connected (0slaves), 780608 bytes in use

4915:S 23 Mar 15:23:55.003 - 1 clients connected (0slaves), 780608 bytes in use

 

 

OK,没问题,主服务器执行以下命令

[root@localhost redis]# bin/redis-cli -a traumerei

127.0.0.1:6379> set name test

OK

127.0.0.1:6379> set age 44

OK

127.0.0.1:6379> save

OK

 

 

从服务器执行以下命令

[root@localhost redis]# bin/redis-cli -a traumerei

127.0.0.1:6379> get name

"test"

127.0.0.1:6379> get age

"44"

 

OK,这样就表明主从服务器数据同步正常,到此redis主从服务器初步同步配置就结束了


你可能感兴趣的:(Redis主从服务器同步搭建(基础篇))