Laravel with Docker issue connecting MySQL
I think you have a couple of issues, the first is that laravel .env configuration should point to the MySQL container, not localhost
DB_HOST=127.0.0.1
should be
DB_HOST=db
And the other error you talked about, is not related to Docker
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)
Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)
it's probably related to a missing dependency, you should run
composer require doctrine/dbal
Also, you said:
now I have access to run any command in running web_laravel container so I've installed composer and php extensions like mbstrings, pdo, pdo_mysql etc.
You should build your image on top of PHP 7/Apache image, and add those to the build dockerfile, because your changes (php extensions, configurations...etc) are not persistent. I would suggest you use Laradock or any other existing Laravel/docker environment.
Dhaval
When I started doing code I was not enthusiasts as much as I am today. it's because of stack overflow who guide me through my journey from nothing to something that I have today about programming. I've experience to work with PHP , MySQL , java script library and frameworks. In the day time I apply my knowledge to solve real life problems and at the night I start learning new things and technology. I'm learning Graph Database (Neo4j) , AngularJs , VueJs , ReactJs . I'm AWS certified. Now I'm enjoying coding and more then coding I love errors. because without errors I can't learn base concept of programming.
Updated on June 17, 2022Comments
-
Dhaval almost 2 years
Steps I follow to setup Laravel using Docker: in my local system I don't have installed PHP, Composer, Apache, MySQL, phpMyAdmin, etc. I only have Git and Docker install in my system.
git clone https://github.com/laravel/laravel.git
-
create docker-composer.yml file on project root.
version: "3" services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: pass MYSQL_DATABASE: db MYSQL_USER: root MYSQL_PASSWORD: pass ports: - "3306:3306" web: image: php:7.2.2-apache container_name: web_laravel depends_on: - db volumes: - ./:/var/www/html/ ports: - "4000:80" stdin_open: true tty: true phpmyadmin: image: phpmyadmin/phpmyadmin depends_on: - db external_links: - db:mysql ports: - "9191:80" environment: MYSQL_USER: root MYSQL_PASSWORD: pass MYSQL_ROOT_PASSWORD: pass PMA_HOST: db
-
run command from project root.
docker-compose up
This command will fetch all the images (php:7.2.2-apache, phpmyadmin/phpmyadmin, mysql:5.7) from local cache or Docker Hub and start three containers for these images.
Now I need to interact with php:7.2.2-apache image's container called web_laravel (see in yml file) so I can add PHP extensions and Composer to run Laravel project.
-
run this command.
docker exec -it web_laravel /bin/bash
Now I have access to run any command in running web_laravel container so I've installed Composer and PHP extensions like mbstrings, pdo, pdo_mysql etc.
Then install Laravel dependency using composer install, set permission for storage and bootstrap/cache folders and run php artisan key:generate.
open localhost:4000 and I'm able to see Laravel home page:
At this point all is good. The problem starts now when I'm connecting to my DB.
Next command to run (I'm still within container):
php artisan migrate
and the errors are:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations) Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)
I'm able to open phpMyAdmin (http://localhost:9191) and can create DB, table and operations related DB. I've DB called blog.
MySQL env variables:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD=pass
-
Dhaval over 5 yearsstill not working.I don't want to use Laradock. I want to create my own dev environment. Your suggestion is good feedback to build your image on top of PHP 7/Apache.
-
Dhaval over 5 yearsI'm not able to connect with mysql and same error show.
-
Amin over 5 yearsI would debug it further, isolate the 2 issues, you can install mysql-cli on PHP container, and try connecting from there, using mysql -h db -u root -p if it works, then connecting the two containers is good to go, but the laravel dependency issue is left. or you can grab a zipped version of phpmyadmin into PHP container, and see if it connects to MySQL container. I think you issue is the laravel dependency, you can try creating a table manually from PHPMyAdmin,like users table,and running any UserModel::all() on it, to see if it pulls data, then the issue is when running migrations only
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
Thalles Portilho almost 2 yearsChanging DB_HOST=127.0.0.1 to DB_HOST=db worked to me. Thanks!
-
Ivan Ovcharenko almost 2 yearsthis does not work in Linux