50

I have a redis server 2.8 installed using ubuntu apt-get on ubuntu 12.04.

I have copied a dump.rdb from an other database. Now when I try to start the new server, I constantly get:

[35763] 04 Mar 01:51:47.088 * 1 changes in 900 seconds. Saving...
[35763] 04 Mar 01:51:47.088 * Background saving started by pid 43313
[43313] 04 Mar 01:51:47.088 # Failed opening .rdb for saving: Permission denied

How can I solve this?

Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
  • 1
    The answer that actually solved my problem was from serverfault.com: https://serverfault.com/a/800328/429693 The config files were ok, but for some reason redis-cli was with the variables set incorrectly (i.e. different from the config files). It could be that someone had accessed it with malicious intent. After that I noticed I had a Malware in my server and an open port for redis. – VinGarcia Jan 06 '18 at 01:02

15 Answers15

52

You should check your redis.conf file to see the permissions in dir and dbfilename. If the file named in the dbfilename which is located in the path specified in the dir path exists and the permission is also right. then the problem should be fixed.

Hope this will help someone.

P.S.

To find the redis.conf file location, you can use the #ps ax | grep redis to check. Usually it will be passed to the redis-server as input file.

For the dir permissions:it should be 755, for the dbfilename, it should be 644

Sometimes you also need to use top command to check whether the user:group of the redis-server and the owner of dir are consistent. i.e. The redis-server is running by redis:redis, but the dir is under root:root. In this case, you need to chown redis:redis -R dir.

chancyWu
  • 14,073
  • 11
  • 62
  • 81
19

Non of the above worked for me.. I've seen everyone around being so concerned on BGSAVE.. but while you're not on production, SAVE gives you a way more straight forward answer: ERR. BGSAVE does not, unless you inspect logs.

After digging dozens of posts I did not find any clue. The only thing that fixed was stopping the redis service and running it manually.

At first I thought it could be related to the user on behalf of redis was running. Not at all: the actual difference was the damn systemd subsystem which at some point in the redis config service file (/etc/systemd/system/redis.service) had the following:

ReadWriteDirectories: -/etc/redis

WoW super cool! ended up this was preventing redis from accessing anywhere in the system at all even though the permissions would perfectly allow it.

How naive of me to think that permission were just enough to ensure something had the proper rights.. (yes, I'm being ironic).

Riccardo Manfrin
  • 653
  • 1
  • 7
  • 15
  • It's old but for future ref. I just check the server port as in my case the port is open to the world. I just removed the entry of the open port in aws and all is working fine after that. as the url is localhost so not required also. – Soumitra Sarkar Sep 27 '22 at 05:58
  • Wow that was it! This is so messed up. – AlexPi Oct 02 '22 at 01:24
  • Another item in that service file that might trip people up is `ProtectHome=yes`. If you want to use a data directory like ~/SSD_Data/redis, that setting will prevent access, so you have to comment out. The redis log will say something like `Can't chdir to '~/SSD_Data/redis': Permission denied` and no amount of permissiveness on that directory will help. – James_SO Aug 11 '23 at 18:26
10

My /lib/system/systemd/redis-server.service file contained the following:

ReadOnlyDirectories=/
ReadWriteDirectories=-/var/lib/redis

My /etc/redis/redis.conf file stated that the database should be located in /data/redis

dir /data/redis

The systemd config file above effectively makes /data/redis read-only.

Once I changed the redis.conf file to read:

dir /var/lib/redis

I stopped getting the error.

Tim Stewart
  • 5,350
  • 2
  • 30
  • 45
4

My permission issue seemed to be the result of the Redis user being unable to modify the parent folder (/var/lib/redis/6379) for the purposes of creating a temporary file.

This was seen in an strace of the redis-server process:

open("temp-1833.rdb", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 EACCES (Permission denied)

The issue was resolved after running the following command:

setfacl -m d:u:redis:rwX,u:redis:rwX /var/lib/redis/6379
Paul Calabro
  • 1,748
  • 1
  • 16
  • 33
  • 1
    It wasn't exactly this issue, but SELinux was ultimately the corporate. I had to use the audit2allow tool to fix it. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Fixing_Problems-Allowing_Access_audit2allow.html – lukecampbell May 02 '16 at 17:53
4

For windows only : This means the user does not have permission for this. BY default owner of this file is NETWORK SERVICE, which has very limited access and need to changed(as per documentation)

solution :

  1. go to ur redis folder.

  2. right click --> go to properties--> security tab.

  3. click on advanced.

  4. click on Add to add ur user.

  5. click on select a principal.

  6. enter ur user (for eg GLOBAL\xxx).

  7. click on check names and click on ok

  8. give permissions to this user.

  9. finally change the owner to this user.

Sansan
  • 41
  • 1
3

Check the configuration 'dbfilename' in your redis.conf. Your redis running process have no write permission in the path.

yinqiwen
  • 604
  • 6
  • 6
3

In my case all rights were correct(I mean that the most stared answer doesn't help me). BUT! Redis used an incorrect path to file. In config it was correct, but from rails-cli it returned '/proc'. This answer helped me - https://serverfault.com/questions/800295/redis-spontaneously-failed-failed-opening-rdb-for-saving-permission-denied

Warning

For exact question it doesn't matter, but my situation looked like someone hacked server. Link to explanation. So check your setup properly.

3

supervised systemd is intended solely for Type=notify and daemonize yes corresponds to Type=forking.

sudo vim /etc/systemd/system/redis.service

When you see the service file edit the Type=forking

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Type=forking
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Open up this file

sudo vim /etc/redis/redis.conf

Add these changes to it

daemonize yes
supervised no
Rick
  • 12,606
  • 2
  • 43
  • 41
3

In my case when I typed the following command sudo tail -F /var/log/redis/redis-server.log I get this log:

987:C 08 Dec 22:28:30.344 # Can't chdir to '/var/lib/redis': Permission denied
1047:C 08 Dec 22:28:30.565 # Can't chdir to '/var/lib/redis': Permission denied
1095:C 08 Dec 22:28:30.876 # Can't chdir to '/var/lib/redis': Permission denied
1119:C 08 Dec 22:28:31.165 # Can't chdir to '/var/lib/redis': Permission denied
1151:C 08 Dec 22:28:31.413 # Can't chdir to '/var/lib/redis': Permission denied
1500:C 08 Dec 22:30:44.706 # Can't chdir to '/var/lib/redis': Permission denied
1523:C 08 Dec 22:30:45.194 # Can't chdir to '/var/lib/redis': Permission denied
1545:C 08 Dec 22:30:45.442 # Can't chdir to '/var/lib/redis': Permission denied
1568:C 08 Dec 22:30:45.696 # Can't chdir to '/var/lib/redis': Permission denied
1590:C 08 Dec 22:30:45.940 # Can't chdir to '/var/lib/redis': Permission denied

That means the user redis doesn't have permission on /var/lib/redis.

That's why I typed this command sudo ls -l /var/lib/redis to see the permissions in this directory. I get the following log:

-rw-r--r-- 1 root root 885 Dec  8 22:12 dump.rdb

That means it was associated with root instead of redis.

Then I typed the following command to change the owner of that directory: sudo chown -R redis:redis /var/lib/redis/.

Then I restarted redis-server by the following command: sudo systemctl restart redis-server.

Boom!! It worked.

Hope this will work for someone, who have similar issue as mine.

Farid Chowdhury
  • 2,766
  • 1
  • 26
  • 21
2

If anyone encounters this again and doesn't have a problem upgrading, just upgrade your Redis installation to the latest version. I encountered this problem with Redis 2.8.15, and upgraded to Redis 2.8.22 that was available at the time of this writing. A sysadmin in my company assured me that this was a bug with Redis 2.8.15, and the problem just went away after upgrading.

OmarOthman
  • 1,718
  • 2
  • 19
  • 36
2

I had the same issue with redis used by Sidekiq in Rails application, rm -rf ./tmp/ worked like charm.

medik
  • 1,174
  • 12
  • 17
2

I spent some time on this until i realised that my cmd line session was running in the wrong drive :/. Just in case this might help anyone else!

jbk
  • 1,911
  • 19
  • 36
2

The lock file in the log directory is what was causing this error for me. I was able to clear the error by deleting the lock file:

rm /var/log/redis/lock.

This happened when another system was restored to this one while redis was still running.

Kurt
  • 1,169
  • 1
  • 9
  • 16
1

No one hasn't mentioned about SELinux. On Centos most probably you will have such error when selinux mode = enforcing.

Just check getenforce and if it set to 'enforcing' , hit setenforce 0 and try one more time to run service.

E.Big
  • 723
  • 6
  • 15
1

If you are on Windows and the Redis folder was installed in C: \ Program Files \ Redis for example, you will have a problem with access permission. Modifying files within the Program Files folder, usually requires administrator permission and dump.rdb is within this context. In your redis.conf, modify the default directory to anywhere outside the Program Files folder:

from: dir ./
to: dir ../../Exceptions/Redis/

Note that I went up the directory twice to leave the Program Files folder and outside of it I created any directory (C:\Exceptions\Redis). In this directory Redis can save the .rdb file without permission problems.

Cássio Lacerda
  • 1,554
  • 1
  • 12
  • 14