搭建redis主从复制环境,非root用户启动redis,结果总是同步失败,日志报错如下:
42580:M 25 Apr 2021 20:50:16.183 # Background saving error
42580:M 25 Apr 2021 20:50:16.183 # Connection with replica 127.0.0.1:6380 lost.
42580:M 25 Apr 2021 20:50:16.183 # SYNC failed. BGSAVE child returned an error
42580:M 25 Apr 2021 20:50:16.183 # Connection with replica 127.0.0.1:6381 lost.
42580:M 25 Apr 2021 20:50:16.183 # SYNC failed. BGSAVE child returned an error
42580:M 25 Apr 2021 20:50:16.186 * Replica 127.0.0.1:6380 asks for synchronization
42580:M 25 Apr 2021 20:50:16.186 * Full resync requested by replica 127.0.0.1:6380
42580:M 25 Apr 2021 20:50:16.186 * Starting BGSAVE for SYNC with target: disk
42580:M 25 Apr 2021 20:50:16.187 * Background saving started by pid 77759
42580:M 25 Apr 2021 20:50:16.187 * Replica 127.0.0.1:6381 asks for synchronization
42580:M 25 Apr 2021 20:50:16.188 * Full resync requested by replica 127.0.0.1:6381
42580:M 25 Apr 2021 20:50:16.188 * Waiting for end of BGSAVE for SYNC
77759:C 25 Apr 2021 20:50:16.188 # Failed opening the RDB file dump-6379.rdb (in server root dir /) for saving: Permission denied
其实日志说的很明白了,就是权限的问题,普通用户不能在root目录下创建文件。需要修改如下几个地方。
1、dir "/" 修改为 dir "/home/hl/redis",改成自己实际的redis安装目录。
2、pidfile "/var/run/redis.pid" 修改为 pidfile "/home/hl/redis/run/redis.pid",手动创建run目录。
3、logfile "" 修改为 logfile "/home/hl/redis/logs/redis.log"。需要手动创建logs目录。
重启redis就可以了。
77907:M 25 Apr 2021 21:01:31.490 * Replica 127.0.0.1:6380 asks for synchronization
77907:M 25 Apr 2021 21:01:31.491 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '82b6f43c59d5e853c5752c2e12065718260e917d', my replication IDs are 'f27fc99c8768000e3e76e44f28e835e318760ec9' and '3c7011ce4e39066acc8aed2e8d9a66377596b8e1')
77907:M 25 Apr 2021 21:01:31.491 * Replication backlog created, my new replication IDs are '246c830e07b9a1e175c3934fdcb1572ef690b400' and '0000000000000000000000000000000000000000'
77907:M 25 Apr 2021 21:01:31.491 * Starting BGSAVE for SYNC with target: disk
78497:C 25 Apr 2021 21:01:31.541 * DB saved on disk
78497:C 25 Apr 2021 21:01:31.541 * RDB: 2 MB of memory used by copy-on-write
77907:M 25 Apr 2021 21:01:31.549 * Background saving started by pid 78497
77907:M 25 Apr 2021 21:01:31.549 * Background saving terminated with success
77907:M 25 Apr 2021 21:01:31.549 * Synchronization with replica 127.0.0.1:6380 succeeded
77907:M 25 Apr 2021 21:02:07.781 * Replica 127.0.0.1:6381 asks for synchronization
77907:M 25 Apr 2021 21:02:07.781 * Full resync requested by replica 127.0.0.1:6381
77907:M 25 Apr 2021 21:02:07.781 * Starting BGSAVE for SYNC with target: disk
77907:M 25 Apr 2021 21:02:07.804 * Background saving started by pid 78529
78529:C 25 Apr 2021 21:02:07.806 * DB saved on disk
78529:C 25 Apr 2021 21:02:07.806 * RDB: 2 MB of memory used by copy-on-write
77907:M 25 Apr 2021 21:02:07.877 * Background saving terminated with success
77907:M 25 Apr 2021 21:02:07.878 * Synchronization with replica 127.0.0.1:6381 succeeded