Bookstack is unable to connect to MariaDB

I’ve been attempting to install Bookstack with the following docker compose configuration, somewhat based off of this example. Note that I have a couple of other services running on the same computer at the same time, so the database port has been changed to 3136 instead of the default 3036:

---
version: "2"

volumes:
  app:
  db:

services:
  app:
    image: lscr.io/linuxserver/bookstack
    environment:
      - PUID=1000
      - PGID=1000
      - APP_URL=https://wiki.[hostname].local
      - DB_HOST=db
      - DB_PORT=3136
      - DB_USER=bookstack
      - DB_PASS=[password]
      - DB_DATABASE=bookstackapp
    volumes:
      - app:/config
    ports:
      - 6875:80
    restart: unless-stopped
    depends_on:
      - db

  db:
    image: lscr.io/linuxserver/mariadb
    environment:
      - PUID=1000
      - PGID=1000
      - MYSQL_ROOT_PASSWORD=[root password]
      - TZ=America/Chicago
      - MYSQL_DATABASE=bookstackapp
      - MYSQL_USER=bookstack
      - MYSQL_PASSWORD=[password]
    ports:
      - 3136:3306
    volumes:
      - db:/config
    restart: unless-stopped

However, in doing so, the app container itself seems unable to connect to the database, at least according to these logs:

[migrations] started
[migrations] 01-nginx-site-confs-default: skipped
[migrations] 02-default-location: skipped
[migrations] done
───────────────────────────────────────
      β–ˆβ–ˆβ•—     β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— 
      β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•—
      β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘
      β–ˆβ–ˆβ•‘     β•šβ•β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘
      β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•
      β•šβ•β•β•β•β•β•β•β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β•β•β•β•β• 
   Brought to you by linuxserver.io
───────────────────────────────────────
To support LSIO projects visit:
https://www.linuxserver.io/donate/
───────────────────────────────────────
GID/UID
───────────────────────────────────────
User UID:    1000
User GID:    1000
───────────────────────────────────────
using keys found in /config/keys
App Key found - setting variable for seds
Running config - DB_HOST set
**** APP_URL in /config/www/.env is being updated from https://wiki.[hostname].com to https://wiki.[hostname].local ****
**** If this is an existing install, you should run the following line from your host terminal to update the database URL entries: ****
************************************************************************
docker exec -it bookstack php /app/www/artisan bookstack:update-url https://wiki.[hostname].com https://wiki.[hostname].local
************************************************************************
Waiting for DB to be available
   Illuminate\Database\QueryException 
  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = bookstackapp and table_name = migrations and table_type = 'BASE TABLE')
  at /app/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756β–•         // If an exception occurs when attempting to run a query, we'll format the error
    757β–•         // message to include the bindings with SQL, which will make this exception a
    758β–•         // lot more helpful to the developer instead of just the database's errors.
    759β–•         catch (Exception $e) {
  ➜ 760β–•             throw new QueryException(
    761β–•                 $query, $this->prepareBindings($bindings), $e
    762β–•             );
    763β–•         }
    764β–•     }
      +39 vendor frames 
  40  /app/www/artisan:35
      Illuminate\Foundation\Console\Kernel::handle()
[custom-init] No custom files found, skipping...
[ls.io-init] done.

Just in case, I’ve added port 3136 into the UFW firewall, but still, no luck:

$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
// Snip
[ 2] 3306                       ALLOW IN    Anywhere
[ 3] 80,443/tcp                 ALLOW IN    Anywhere
// Snip
[15] 3306/tcp                   ALLOW IN    Anywhere
// Snip
[17] 6465                       ALLOW IN    Anywhere
// Snip
[21] 3136/tcp                   ALLOW IN    192.168.0.0/24
[22] 3136/tcp                   ALLOW IN    10.10.10.0/24
[23] 3136/tcp                   ALLOW IN    Anywhere
// Snip
[25] 80,443/tcp (v6)            ALLOW IN    Anywhere (v6)
// Snip
[33] 3306 (v6)                  ALLOW IN    Anywhere (v6)
[34] 3306/tcp (v6)              ALLOW IN    Anywhere (v6)
// Snip
[36] 6465 (v6)                  ALLOW IN    Anywhere (v6)
// Snip
[39] 3136/tcp                   ALLOW IN    fd08:4711::1/64
[40] 3136/tcp (v6)              ALLOW IN    Anywhere (v6)

Obviously, the line, SQLSTATE[HY000] [2002] Connection refused is suspicious to me, and I’m thinking the reason is due to something in the app configuration not being able to connect to the db, but I can’t figure out why. Any ideas?

1 Like

TL;DR: Communication between containers within docker-compose can only access the container’s internal ports. Set DB_PORT in the BookStack service to 3306 while keeping the external port mapping for MariaDB as 3136:3306.


Hi japtar10101,

I encountered the exact same issue with BookStack being unable to connect to MariaDB, and after extensive troubleshooting and research, I came across your forum post. In fact, your post was the only one I could find targeting this exact error, and so I thought we both faced similar circumstances, particularly by changing the database port.

Like you, I had another database service using the default port 3306, so I needed to change the port for MariaDB, in my case to 3307. Initially, I thought everything was working fine since I could connect to the database from my PC using MySQL Workbench on the new port 3307. However, BookStack was still unable to connect to the database, showing the same β€œconnection refused” error in the logs.

After further investigation, I discovered that while external connections can use the manually specified port (3307), internal container-to-container communication within Docker (docker-compose) can only access the container’s internal port (3306).

To solve the problem, set the DB_PORT in the BookStack service to 3306. At the MariaDB service, you can keep the external port mapping as 3136:3306 in your case. This way, you can still access the database externally using port 3136.

Here’s an example of how your docker-compose configuration should look:

services:
  bookstack:
    ...
    environment:
      ...
      - DB_PORT=3306  # Internal port of the bookstack_db container

  bookstack_db:
    ...
    ports:
      - 3136:3306  # Externally map 3136 to internal 3306

I hope this helps! If you have any further questions or run into more issues, feel free to ask. I’ll try my best to answer them :sweat_smile:

Best of luck!

1 Like