2

I'm in the process of configuring my laravel application to run within a docker container; I've successfully configured the laravel side on top of the php7.4-apache docker image, and I've separately configured the Mariadb10.3 image for the db. I've troubleshot a number of connection issues, and have finally gotten it down to the following error:

SQLSTATE[HY000] [2002] Invalid argument (SQL: select * from information_schema.tables where table_schema = laravel_db and table_name = migrations and table_type = 'BASE TABLE')

The particular error instance above appears when running php artisan migrate, but I also see the same error in two other places: when using php artisan tinker and attempting a basic query on tables that I've verified are present, and when running a query through my laravel app (triggered by visiting the site on the test server). The errors are reproduced below:

\DB::table('users')->get();
Illuminate/Database/QueryException with message 'SQLSTATE[HY000] [2002] Invalid argument (SQL: select * from `users`)'

and from the docker logs:

PDOException(code: 2002): SQLSTATE[HY000] [2002] Invalid argument at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

The only information I've found on this particular error via SO comes from these two posts: SO#1 SO#2

Both state that you need to "specify TCP/IP (1), or tell MySQL where your Unix socket is (2)", however, I'm using docker and am referencing my db via the docker container name, shown in my .env file:

DB_CONNECTION=mysql
DB_HOST=db
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=<MY_PASSWORD>
DB_HOST=3333

So my question is this: how do I bypass this error while using docker? I need to reference the docker container in the .env file in order for the docker side of things to work, so then I'm unable to implement the suggestions from SO. I've also searched extensively outside SO (looking at Mariadb docs for error documentation and googling widely) to no avail.

My docker-compose file is here, in case that's needed:

version: "2.2"
services:
 app:
  image: php:7.4-apache
  ports:
   - "8060:80"
  tty: true
  restart: unless-stopped
  working_dir: /var/www/html
  container_name: app
  build:
   context: .
   dockerfile: app_dockerfile
  networks:
   - app-network

 db:
  image: mariadb:10.3
  container_name: db
  restart: unless-stopped
  tty: true
  environment:
   MYSQL_ROOT_PASSWORD: <MY_PASSWORD>
   MYSQL_DATABASE: laravel_db
   SERVICE_TAGS: dev
   SERVICE_NAME: mysql
  networks:
   - app-network
  volumes:
   - dbdata:/var/lib/mysql/docker_db/
  build:
   context: .
   dockerfile: db_dockerfile
  ports:
   - "3333:3306"
networks:
 app-network:
  driver: bridge

volumes:
 dbdata:
  driver: local

Thanks everyone, I really appreciate it.

  • Okay, it looks like my .env file above was incorrectly configured--I had DB_HOST in there twice. After changing it to DB_PORT properly, I now get: ```SQLSTATE[HY000] [2002] Connection refused``` – Kirk Lundblade Sep 23 '20 at 15:01

1 Answers1

0

Could it be that the host in your .env file is wrong? Have you tried to connect to the database via a client like table plus or similar?

Jernej Beg
  • 39
  • 5
  • I've connected to the db via a second mariadb container (via the instructions here: https://hub.docker.com/_/mariadb/ ) and I've successfully pinged the db container from the host container. I've also verified that both containers are on the same docker network. – Kirk Lundblade Sep 23 '20 at 15:04