php artisan migrate error : (trying to connect via (null)) #4284

Closed
opened 2026-02-05 08:25:37 +03:00 by OVERLORD · 6 comments
Owner

Originally created by @askme-gpt on GitHub (Oct 31, 2023).

Attempted Debugging

  • I have read the debugging page

Searched GitHub Issues

  • I have searched GitHub for the issue.

Describe the Scenario

I use docker to run this application.
PHP_VERSION=8.2.10
MySQL Server 8.0.26-1debian10

when I run php artisan migrate it come out below error :

/www/BookStack # php artisan migrate

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002]  (trying to connect via (null)) (SQL: select * from information_schema.tables where table_schema = book_stack and table_name = migrations and table_type = 'BASE TABLE')

  at 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▕     }

      +36 vendor frames 
  37  artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

my DB config as follow : and it's right.

DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=book_stack
DB_USERNAME=asp
DB_PASSWORD='1f#sf3456$%'

Exact BookStack Version

v23.10

Log Content

No response

Hosting Environment

I use docker to run this application.
PHP_VERSION=8.2.10
MySQL Server 8.0.26-1debian10

Originally created by @askme-gpt on GitHub (Oct 31, 2023). ### Attempted Debugging - [X] I have read the debugging page ### Searched GitHub Issues - [X] I have searched GitHub for the issue. ### Describe the Scenario I use docker to run this application. PHP_VERSION=8.2.10 MySQL Server 8.0.26-1debian10 when I run `php artisan migrate` it come out below error : ```linux /www/BookStack # php artisan migrate Illuminate\Database\QueryException SQLSTATE[HY000] [2002] (trying to connect via (null)) (SQL: select * from information_schema.tables where table_schema = book_stack and table_name = migrations and table_type = 'BASE TABLE') at 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▕ } +36 vendor frames 37 artisan:35 Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) ``` my DB config as follow : and it's right. ``` DB_HOST=mysql DB_PORT=3306 DB_DATABASE=book_stack DB_USERNAME=asp DB_PASSWORD='1f#sf3456$%' ``` ### Exact BookStack Version v23.10 ### Log Content _No response_ ### Hosting Environment I use docker to run this application. PHP_VERSION=8.2.10 MySQL Server 8.0.26-1debian10
OVERLORD added the 🐕 Support label 2026-02-05 08:25:37 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Oct 31, 2023):

Hi @askme-gpt,
What docker container are you using for BookStack? And where are those options being set?

@ssddanbrown commented on GitHub (Oct 31, 2023): Hi @askme-gpt, What docker container are you using for BookStack? And where are those options being set?
Author
Owner

@askme-gpt commented on GitHub (Nov 1, 2023):

I use windows container.and my compose-yml as follow,I have 2 php container ,one is php7.4,and php82 is php8.2,and I use php8.2 as the php running bookstack. when I use php -a interactive terminal ,php can connet mysql right, as the follow picture says.

version: "3"
services:
  nginx:
    build:
      context: ./services/nginx
      args:
        NGINX_VERSION: nginx:${NGINX_VERSION}
        CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL}
        NGINX_INSTALL_APPS: ${NGINX_INSTALL_APPS}
    container_name: nginx
    ports:
      - "${NGINX_HTTP_HOST_PORT}:80"
      - "${NGINX_HTTPS_HOST_PORT}:443"
      - "9080:9080"
      - "9081:9081"
      - "8443:8443"
      - "4443:4443"
    volumes:
      - ${SOURCE_DIR}:/www/:rw
      - ${NGINX_SSL_CERTIFICATE_DIR}:/ssl:rw
      - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw
      - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro
      - ${NGINX_FASTCGI_PHP_CONF}:/etc/nginx/fastcgi-php.conf:ro
      - ${NGINX_FASTCGI_PARAMS}:/etc/nginx/fastcgi_params:ro
      - ${NGINX_LOG_DIR}:/var/log/nginx/:rw
    environment:
      TZ: "$TZ"
    restart: always
    extra_hosts:
      - "openapi:127.0.0.1"
      - "lvs:127.0.0.1"
      - "admin:127.0.0.1"
      - "portal:127.0.0.1"
    networks:
      - default

  php:
    build:
      context: ./services/php
      args:
        PHP_VERSION: php:${PHP_VERSION}-fpm-alpine
        CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL}
        PHP_EXTENSIONS: ${PHP_EXTENSIONS}
        TZ: "$TZ"
    container_name: php
    ports:
      - "9506:9506"
      - "9507:9507"
      - "9508:9508"
      - "9509:9509"
    expose:
      - 9501
    extra_hosts:
      - "www.site1.com:172.17.0.1"
    volumes:
      - ${SOURCE_DIR}:/www/:rw
      - ${PHP_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro
      - ${PHP_FPM_CONF_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw
      - ${PHP_LOG_DIR}:/var/log/php
      - ${PHP_TMP_DIR}:/tmp
      - ${DATA_DIR}/composer:/tmp/composer
    restart: always
    cap_add:
      - SYS_PTRACE
    networks:
      - default
  php82:
    build:
      context: ./services/php8
      args:
        PHP_VERSION: php:${PHP82_VERSION}-fpm-alpine
        CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL}
        PHP_EXTENSIONS: ${PHP82_EXTENSIONS}
        TZ: "$TZ"
    ports:
      - "9100:9100"
      - "8117:8187"
      - "8119:8189"
      - "8118:8788"
    volumes:
      - ${SOURCE_DIR}:/www/:rw
      - ${PHP82_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro
      - ${PHP82_FPM_CONF_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw
      - ${PHP_LOG_DIR}:/var/log/php
      - ${PHP_TMP_DIR}:/tmp
      - ${DATA_DIR}/composer:/tmp/composer
    networks:
      - default
    container_name: php82
    
#  mysql:
#    image: mysql:${MYSQL_VERSION}
#    container_name: mysql
#    ports:
#      - "${MYSQL_HOST_PORT}:3306"
#    volumes:
#      - ${MYSQL_CONF_FILE}:/etc/mysql/conf.d/mysql.cnf:ro
#      - ${DATA_DIR}/mysql:/var/lib/mysql/:rw
#    restart: always
#    networks:
#      - default
#    environment:
#      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
#      TZ: "$TZ"
#
#  mongodb:
#    image: mongo:${MONGODB_VERSION}
#    container_name: mongodb
#    environment:
#        MONGO_INITDB_ROOT_USERNAME: "${MONGODB_INITDB_ROOT_USERNAME}"
#        MONGO_INITDB_ROOT_PASSWORD: "${MONGODB_INITDB_ROOT_PASSWORD}"
#        TZ: "$TZ"
#    volumes:
#      - ${DATA_DIR}/mongo:/data/db:rw
#      - ${DATA_DIR}/mongo_key:/mongo:rw
#    ports:
#       - "${MONGODB_HOST_PORT}:27017"
#    networks:
#       - default
#    command:
#       --auth
#
#  redis:
#    image: redis:${REDIS_VERSION}
#    container_name: redis
#    ports:
#      - "${REDIS_HOST_PORT}:6379"
#    volumes:
#      - ${REDIS_CONF_FILE}:/etc/redis.conf:ro
#      - ${DATA_DIR}/redis:/data/:rw
#    restart: always
#    entrypoint: ["redis-server", "/etc/redis.conf"]
#    environment:
#      TZ: "$TZ"
#    networks:
#      - default
      
networks:
  default:

image
image

@askme-gpt commented on GitHub (Nov 1, 2023): I use windows container.and my compose-yml as follow,I have 2 php container ,one is php7.4,and php82 is php8.2,and I use php8.2 as the php running bookstack. when I use `php -a` interactive terminal ,php can connet mysql right, as the follow picture says. ```linux version: "3" services: nginx: build: context: ./services/nginx args: NGINX_VERSION: nginx:${NGINX_VERSION} CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL} NGINX_INSTALL_APPS: ${NGINX_INSTALL_APPS} container_name: nginx ports: - "${NGINX_HTTP_HOST_PORT}:80" - "${NGINX_HTTPS_HOST_PORT}:443" - "9080:9080" - "9081:9081" - "8443:8443" - "4443:4443" volumes: - ${SOURCE_DIR}:/www/:rw - ${NGINX_SSL_CERTIFICATE_DIR}:/ssl:rw - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro - ${NGINX_FASTCGI_PHP_CONF}:/etc/nginx/fastcgi-php.conf:ro - ${NGINX_FASTCGI_PARAMS}:/etc/nginx/fastcgi_params:ro - ${NGINX_LOG_DIR}:/var/log/nginx/:rw environment: TZ: "$TZ" restart: always extra_hosts: - "openapi:127.0.0.1" - "lvs:127.0.0.1" - "admin:127.0.0.1" - "portal:127.0.0.1" networks: - default php: build: context: ./services/php args: PHP_VERSION: php:${PHP_VERSION}-fpm-alpine CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL} PHP_EXTENSIONS: ${PHP_EXTENSIONS} TZ: "$TZ" container_name: php ports: - "9506:9506" - "9507:9507" - "9508:9508" - "9509:9509" expose: - 9501 extra_hosts: - "www.site1.com:172.17.0.1" volumes: - ${SOURCE_DIR}:/www/:rw - ${PHP_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro - ${PHP_FPM_CONF_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw - ${PHP_LOG_DIR}:/var/log/php - ${PHP_TMP_DIR}:/tmp - ${DATA_DIR}/composer:/tmp/composer restart: always cap_add: - SYS_PTRACE networks: - default php82: build: context: ./services/php8 args: PHP_VERSION: php:${PHP82_VERSION}-fpm-alpine CONTAINER_PACKAGE_URL: ${CONTAINER_PACKAGE_URL} PHP_EXTENSIONS: ${PHP82_EXTENSIONS} TZ: "$TZ" ports: - "9100:9100" - "8117:8187" - "8119:8189" - "8118:8788" volumes: - ${SOURCE_DIR}:/www/:rw - ${PHP82_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro - ${PHP82_FPM_CONF_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw - ${PHP_LOG_DIR}:/var/log/php - ${PHP_TMP_DIR}:/tmp - ${DATA_DIR}/composer:/tmp/composer networks: - default container_name: php82 # mysql: # image: mysql:${MYSQL_VERSION} # container_name: mysql # ports: # - "${MYSQL_HOST_PORT}:3306" # volumes: # - ${MYSQL_CONF_FILE}:/etc/mysql/conf.d/mysql.cnf:ro # - ${DATA_DIR}/mysql:/var/lib/mysql/:rw # restart: always # networks: # - default # environment: # MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}" # TZ: "$TZ" # # mongodb: # image: mongo:${MONGODB_VERSION} # container_name: mongodb # environment: # MONGO_INITDB_ROOT_USERNAME: "${MONGODB_INITDB_ROOT_USERNAME}" # MONGO_INITDB_ROOT_PASSWORD: "${MONGODB_INITDB_ROOT_PASSWORD}" # TZ: "$TZ" # volumes: # - ${DATA_DIR}/mongo:/data/db:rw # - ${DATA_DIR}/mongo_key:/mongo:rw # ports: # - "${MONGODB_HOST_PORT}:27017" # networks: # - default # command: # --auth # # redis: # image: redis:${REDIS_VERSION} # container_name: redis # ports: # - "${REDIS_HOST_PORT}:6379" # volumes: # - ${REDIS_CONF_FILE}:/etc/redis.conf:ro # - ${DATA_DIR}/redis:/data/:rw # restart: always # entrypoint: ["redis-server", "/etc/redis.conf"] # environment: # TZ: "$TZ" # networks: # - default networks: default: ``` ![image](https://github.com/BookStackApp/BookStack/assets/22534808/c1aded2b-1c23-4fd3-999c-4fde9d32919b) ![image](https://github.com/BookStackApp/BookStack/assets/22534808/90b6cf1a-86e7-4400-9a3a-43b6e92cdeb2)
Author
Owner

@askme-gpt commented on GitHub (Nov 1, 2023):

@ssddanbrown thank you for your help . my php8.2's dockerfile as follow,it runs right.

ARG PHP_VERSION
FROM ${PHP_VERSION}

ARG TZ
ARG PHP_EXTENSIONS
ARG CONTAINER_PACKAGE_URL


RUN if [ $CONTAINER_PACKAGE_URL ] ; then sed -i "s/dl-cdn.alpinelinux.org/${CONTAINER_PACKAGE_URL}/g" /etc/apk/repositories ; fi

COPY ./extensions /tmp/extensions
WORKDIR /tmp/extensions
RUN chmod +x install.sh \
    && sh install.sh \
    && rm -rf /tmp/extensions

ADD ./extensions/install-php-extensions  /usr/local/bin/

RUN chmod uga+x /usr/local/bin/install-php-extensions

RUN apk --no-cache add tzdata \
    && cp "/usr/share/zoneinfo/$TZ" /etc/localtime \
    && echo "$TZ" > /etc/timezone


# Fix: https://github.com/docker-library/php/issues/240
RUN apk add gnu-libiconv libstdc++ --no-cache --repository http://${CONTAINER_PACKAGE_URL}/alpine/edge/community/ --allow-untrusted
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php


# Install composer and change it's cache home
RUN curl -o /usr/bin/composer https://mirrors.aliyun.com/composer/composer.phar \
    && chmod +x /usr/bin/composer
ENV COMPOSER_HOME=/tmp/composer

# php image's www-data user uid & gid are 82, change them to 1000 (primary user)
RUN apk --no-cache add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data


WORKDIR /www

@askme-gpt commented on GitHub (Nov 1, 2023): @ssddanbrown thank you for your help . my php8.2's dockerfile as follow,it runs right. ```php ARG PHP_VERSION FROM ${PHP_VERSION} ARG TZ ARG PHP_EXTENSIONS ARG CONTAINER_PACKAGE_URL RUN if [ $CONTAINER_PACKAGE_URL ] ; then sed -i "s/dl-cdn.alpinelinux.org/${CONTAINER_PACKAGE_URL}/g" /etc/apk/repositories ; fi COPY ./extensions /tmp/extensions WORKDIR /tmp/extensions RUN chmod +x install.sh \ && sh install.sh \ && rm -rf /tmp/extensions ADD ./extensions/install-php-extensions /usr/local/bin/ RUN chmod uga+x /usr/local/bin/install-php-extensions RUN apk --no-cache add tzdata \ && cp "/usr/share/zoneinfo/$TZ" /etc/localtime \ && echo "$TZ" > /etc/timezone # Fix: https://github.com/docker-library/php/issues/240 RUN apk add gnu-libiconv libstdc++ --no-cache --repository http://${CONTAINER_PACKAGE_URL}/alpine/edge/community/ --allow-untrusted ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php # Install composer and change it's cache home RUN curl -o /usr/bin/composer https://mirrors.aliyun.com/composer/composer.phar \ && chmod +x /usr/bin/composer ENV COMPOSER_HOME=/tmp/composer # php image's www-data user uid & gid are 82, change them to 1000 (primary user) RUN apk --no-cache add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data WORKDIR /www ```
Author
Owner

@askme-gpt commented on GitHub (Nov 1, 2023):

after I commented code in database.php like follow ,it works right. the error message sound strange, it confused me.
image

@askme-gpt commented on GitHub (Nov 1, 2023): after I commented code in database.php like follow ,it works right. the error message sound strange, it confused me. ![image](https://github.com/BookStackApp/BookStack/assets/22534808/9dfb0bec-4663-43eb-b1d7-8409486b219c)
Author
Owner

@askme-gpt commented on GitHub (Nov 1, 2023):

I solve this ,Thank you.

@askme-gpt commented on GitHub (Nov 1, 2023): I solve this ,Thank you.
Author
Owner

@ssddanbrown commented on GitHub (Nov 2, 2023):

Good to hear, I'll therefore close this off.

Just a note, from your screenshots it looks like you've copied the entire .env.example.complete as a basis for your .env file. This is not advised/recommended and as likely led to this (you shoudn't need to comment out code).
You should instead use .env.example as a basis then add/tweak it as required.

@ssddanbrown commented on GitHub (Nov 2, 2023): Good to hear, I'll therefore close this off. Just a note, from your screenshots it looks like you've copied the entire `.env.example.complete` as a basis for your `.env` file. This is not advised/recommended and as likely led to this (you shoudn't need to comment out code). You should instead use `.env.example` as a basis then add/tweak it as required.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#4284