"database is locked" - unable to use grafana anymore :(

Hi there,

I had a power outage and my docker containers got killed. After restarting them, Grafana is very
slow and I get a lot of these messages in the logs:

t=2019-04-21T16:28:24+0000 lvl=eror msg="Failed to update last_seen_at" logger=context userId=1 orgId=1 uname=admin error="database is locked"

I also cannot save the dashboard. I suspect there are some stale locks in place?

Can anyone tell me how I can fix this?
Thank you very much!

4 Likes

The “database is locked” error indicates a problem with your sqlite database. This can happen if the database is left in an inconsistent state after a crash, or if there are problems with the disk.

One thing you can try is to dump the data from your existing db file into a new one, then swap it out. From inside your Grafana data directory (after shutting down Grafana):

sqlite3 grafana.db '.clone grafana-new.db'
mv grafana.db grafana-old.db
mv grafana-new.db grafana.db
4 Likes

Thanks a lot! This worked!

1 Like

I have this exact issue, but I am running grafana from the container. How can I apply this solution?

2 Likes

Hi @iraklic ,

if you are using the grafana/grafana docker image then the sqlite is located at /var/lib/grafana/grafana.db
Again, if you are using grafana/grafana image inside kubernetes then the sqlite is stored on a persistent-volume. Inside the container, there is no sqlite3 available.

Therefore you must copy the database out of the container or PVC on your machine, clone it there and copy it back.

kubectl cp <grafana_pod_name>:/var/lib/grafana/grafana.db -c grafana ./grafana.db

then do the cloning as described above and copy it back.

Hey, I got the same issue “database is locked” 2 weeks back, and used the steps for cloning that u mentioned and it solved the error, but now it again came and I had to again implement the cloning to make it right. There was no Grafana crash or anything issue recent, so how to solve this permanently?