Use digiKam with a NAS and MariaDB


Got a NAS? Still store your photo library and digiKam databases on a local machine? It's time to take your digiKam setup to the next level by moving your photo library to the NAS and switching to the MariaDB (or MySQL) database back end. This allows you to access your photo library from any machine on the same network as well as keep your photo library safe thanks to the fact that storage on most NAS appliances is usually configured as RAID.

Switching to the NAS-based digiKam setup is a three-step process:

  1. Switch to the MariaDB database back end
  2. Enable and configure Samba
  3. Move the photo library to the NAS

Before you proceed, though, you need to do some preparatory work. First of all, you need a fast network link between the NAS and any machine that you want to use for working with your photo library. Even the fastest WiFi connection is simply not fast enough, so a wired network connection is the only viable option here. However, laying Ethernet cables around your apartment might not be particularly practical. In that case, a couple of Gigabit powerline network adapters provide a less cumbersome solution. Keep in mind, though, that if any of your neighbors get the same idea of using powerline adapters on their local networks, this will affect the overall speed of your network too.

While many NAS models ship with the MySQL/MariaDB database server, you often need to activate it manually. To do this on a QNAP NAS, launch the Control Panel, click SQL Server in the Applications section, and activate the Enable SQL server and Enable TCP/IP networking options. Press Apply to start the MariaDB server. To manage the server and databases, install the phpMyAdmin application from the App Center.

Next step is to create a new database user as well as databases for use with digiKam. To do this, launch phpMyAdmin, switch to the Databases section, and create three databases: digikam, digikam-thumbnails, and digikam-recognition. Switch then to the User Accounts section and create a new user by clicking on Add user account. Specify the desired user name and password. Choose Any host from the Host name drop-down list. Enable the Global privileges → Check all option, and press Go.

Now it's time to switch digiKam to the MariaDB database engine. To be on the safe side, back up the digiKam databases first. Launch then digiKam, choose Settings → Database Migration and use the migration tool to move the existing digiKam databases to MariaDB.

Next step is to mount a remote NAS directory on the local machine storing your photo library. Practically all NAS appliances on the market support the SMB protocol, so you can use it to mount a remote directory on your machine. Assuming that the SMB server is up and running on the NAS, you can use the following command to mount the desired remote directory into the local folder:

sudo mount -t cifs //ipaddess/path/to/dir /path/to/local/dir -o user="user",password="password",uid=$(id -u),gid=$(id -g)

Replace the example values as follows:

  • ipaddess - the actual IP address of the NAS (e.g.,
  • /path/to/dir - path to the directory on the NAS. This is going to be the new location for your photo library.
  • /path/to/local/dir path to the directory on the local machine where you want to mount the remote folder
  • user SMB user name (usually an existing NAS user name)
  • password password of the specified user

In digiKam, choose Settings → Configure digiKam and switch to the Collections section. Use the Add Collection button next to Collection on Network Shares to add the mounted folder as a new collection, then click OK. Move the existing albums into the new collection. Once you've done that, go back to the Settings → Configure digiKam → Collections section and remove the emptied local collections. Congratulations, you are done!

Automate Mount and Backup Actions

Since your photo library sits now on the NAS, you need to mount it on your local machine before you can launch digiKam. Doing this manually can quickly become a nuisance, but you can easily automate this task by writing a simple Bash shell script:

#!/usr/bin/env bash
if [ ! "$(ls -A /path/to/local/dir)" ]; then
    sudo mount -t cifs //ipaddess/path/to/dir /path/to/local/dir -o user="user",password="password",uid=$(id -u),gid=$(id -g)

Save the script under the name and make it executable using the chmod +x command. Since the mount command in the script requires root privileges, the script prompts you to enter the root password every time you run it. To prevent this from happening, add mount to the sudoers list. To do this, use the which mount command to find out the exact path to the mount tool (e.g., /usr/bin/mount). Run the sudo visudo command, and add the following line at the end of the sudoers file (replace user with your user name on the local machine):

user ALL = NOPASSWD: /usr/bin/mount

Save the changes, and you are done. You can then assign a keyboard shortcut to the script, so you can launch digiKam using a key combination.

Now that digiKam stores all its data in MariaDB databases, it makes sense to back them up on a regular basis. And a quick-and-dirty shell script can help you with that.

NOW=`date +%Y%m%d`
DB="digikam digikam-thumbnails digikam-recognition"
mkdir -p $DIR
mysqldump -u$USER -h$HOST -p$PASSWD \
--databases ${DB} > $DIR/$NOW-digikam-db.sql
cd $DIR
tar -zcvf $NOW-digikam-db.tar.gz $NOW-digikam-db.sql
rm $NOW-digikam-db.sql

Replace the example values with the actual MariaDB user name, password, and host. Specify also the path to the directory for storing backups. Note that on some NAS appliances like QNAP, you need to specify the absolute path to the mysqldump tool. To find out the absolute path, run the find / -name mysqldump command. Save the script on your NAS and make the script executable. When you run the script, it should create a .tar.gz backup file in the specified directory. To run the script on a regular basis, add a cron job.