I am getting the following error
[2022-04-07 12:11:23] local.ERROR: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = swimrace and table_name = migrations and table_type = 'BASE TABLE') {"exception":"[object] (Illuminate\\Database\\QueryException(code: 2002): SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = swimrace and table_name = migrations and table_type = 'BASE TABLE') at /var/www/swimrace/vendor/laravel/framework/src/Illuminate/Database/Connection.php:712)
When I run php artisan migrate
I'm running it inside the container using this command docker-compose exec workspace bash
Here my laradock .env configuration:
### MARIADB ###############################################
MARIADB_VERSION=latest
MARIADB_DATABASE=default
MARIADB_USER=root
MARIADB_PASSWORD=root
MARIADB_PORT=3306
MARIADB_ROOT_PASSWORD=root
MARIADB_ENTRYPOINT_INITDB=./mariadb/docker-entrypoint-initdb.d
Here my laravel project .env configuration:
DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=swimrace
DB_USERNAME=swimrace_user
DB_PASSWORD=raceswim
User and password in this last configuration was created via console into mariadb container and assigned to this account all privileges.
If I try to read database with any database ui tools (PMA, DataGrip, etc) I can see and write inside the database, but not with artisan commands.
Any suggestions?
Related
I got a laravel app (9.19.0) and php (8.1.1) linked to a mariadb database.
Each part of the app is in a specific docker container.
When I try to do php artisan migrate to build my tables, I got the error
SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not respond properly after a certain amount of time or an established connection failed because the connecting host did not respond (SQL: select *
from information_schema.tables where table_schema = mydatabasename and table_name = migrations and table_type = 'BASE TABLE')
\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct("mysql:host=172.19.0.3;port=3306;dbname=mydatabasename", "username", "password", [])
The point is when I run an API request on the same application, I can access my database with the exact same .env file.
I tried to :
php artisan cache:clear : Clear cache but no changes
Reinstall completely my docker containers (volumes & containers) : No changes
Change the DB_HOST parameter from IP to container name : No changes
Create manually the table migrations : No changes
For me, the probleme come from the request returned in the error message, because when I type the exact same query select * from information_schema.tables where table_schema = mydatabasename and table_name = migrations and table_type = 'BASE TABLE' I got an error saying that mysql cannot found the field mydatabasename but when I put the parameters between quotes no more errors select * from information_schema.tables where table_schema = 'mydatabasename' and table_name = 'migrations' and table_type = 'BASE TABLE'
For me, this is pure magic, why can't artisan connect to my database but my app can since they are using the same .env file and (I guess) the same underlaying method to connect to the database ?
Note : With the exact same setup, things seems to work previously
SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
found this somewhere and it was helpful
The DB_USERNAME should be set to root if you do not have a default username
If no password is set on the database, clear it DB_PASSWORD
After completion of .env edit must be clear cache: php artisan config:cache
After Run the migrate Artisan command: php artisan migrate
I'm trying to make a project using Laravel and Sail but I'm getting errors when I'm trying to migrate or do anything related to the project's database (for example, seeding).
More specifically, when using artisan sail migrate or anything related, I'm getting the following errors:
When DB_HOST=localhost in my .env file:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = noway and table_name = migrations and table_type = 'BASE TABLE')
When DB_HOST=127.0.0.1 in my .env file:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = noway and table_name = migrations and table_type = 'BASE TABLE')
What I've tried:
changing the DB_HOST to localhost and 127.0.0.1 (most common answer online, no succes)
making sure SQL is running
clearing cache for sail artisan migrate
./vendor/bin/sail down --rmi all -v to remove all images and volumes
changing username and password and putting them between '' or ""
None of the above have been of succes.
did you run ./vendor/bin/sail up after creating and setting .env ? if not please execute ./vendor/bin/sail down --rmi all -v to remove all images and volumes and then just ran ./vendor/bin/sail up to recreate the images and volumes.
Also as per laravel docs https://laravel.com/docs/8.x/sail you can connect to db by setting env DB_HOST to mysql
T had the same problem with pretty much the same conditions:
-I started with a laravel project then moved it to sail
the way I solved this was by adding an entry DB_PORT=3306 in the .env file and changed DB_HOST=localhost to DB_HOST=mysql as described in laravel docs : https://laravel.com/docs/8.x/sail#mysql
don't forget to check the new credentials that sail uses by default
When i run the command php artisan migrate it gives me this error:
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432? (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
In as much as i am using a database.sqlite file for my database
My .env file:
DB_CONNECTION=sqlite
The problem was fixed by doing the following:
I added the path for my database file in my .env, then it looked like this below
DB_CONNECTION=sqlite
DB_DATABASE='database/database.sqlite'
Then I cleared my cache using php artisan config:cache
Then I ran php artisan migrate and it worked!
Originally had a project that worked with MySQL(5.7.25x64 if it matters).
Now I need to deploy this project on another server, with MariaDB (10.4.11-MariaDB)
After composer install trying php artisan migrate but catch exception
Illuminate\Database\QueryException : SQLSTATE[HY000] [2006] MySQL server has gone away (SQL: select * from information_schema.tables where table_schema = admin_testtable and table_name = migrations and table_type = 'BASE TABLE')
laravel 6.10.1
MariaDB 10.4.11
PHP 7.3.13
Centos8
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=admin_testtable //(i can't remove 'admin_' in name. the DB is created and exists)
DB_USERNAME=username
DB_PASSWORD=password
php artisan optimize:clear, php artisan config:cache, composer dump-autoload, restart server - doesn't help (just in case)
Edit your my.cnf file
max_allowed_packet=64M // set 128M if required
On Windows this file is located at: "C:\ProgramData\MySQL\MySQL Server 5.6"
On Linux (Ubuntu): /etc/mysql
Reload your mysql service after editing.