Failed to connect database using docker in laravel - php

i have a problem when connect database using laravel and docker ,i got error like this :
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = sisuket and table_name = migrations and table_type = 'BASE TABLE')
and this is my docker-compose in section mariadb
mariadb:
image: docker.io/bitnami/mariadb:10.7
ports:
- 3306:3306
environment:
- ALLOW_EMPTY_PASSWORD=yes
- MARIADB_DATABASE=sisuket
- MARIADB_ROOT_USER=sisuket
- MARIADB_ROOT_PASSWORD=sisuket
restart: always
networks:
- app-tier
and this is my .env and database.php , i have try to change DB_HOST to be localhost and 127.0.0.1 . but,that's useless
APP_NAME=app
APP_ENV=local
APP_KEY=base64:aQ+ckCjANh0NPWNp1zOWJYHS6k3eiaoBmMVdhX93oqQ=
APP_DEBUG=true
APP_URL=http://localhost:80
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST= mariadb
DB_PORT=3306
DB_DATABASE=sisuket
DB_USERNAME=sisuket
DB_PASSWORD=sisuket
[//database.php
'mysql' => \[
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'mariadb'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'sisuket'),
'username' => env('DB_USERNAME', 'sisuket'),
'password' => env('DB_PASSWORD', 'sisuket'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter(\[
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
\]) : \[\],
\],][1]

Thre thing what you can try:
php artisan config:cache. If that not work =>
Remove the whitespace in the line DB_HOST= mariadb => DB_HOST=mariadb and php artisan config:cache. If that not work =>
If that not work change DB_HOST to DB Host DB_HOST=127.0.0.1 and php artisan config:cache.

Related

MYSQL error: SQLSTATE[HY000] [2002] No such file or directory (FEDORA LARAVEL)

I have an aplication that works with angularjs and its backend is on Laravel. I'm working on Fedora 37.
The back is in a docker container, and I have a local database with mysql.
When I try to query anithing to the database, it brings the next error:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from
usuarios where email = admin#test and usuarios.deleted_at is
null limit 1)
The .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wymaq
DB_USERNAME=root
DB_PASSWORD=
Frontend is located in localhost:9001, and its linked in config.app.js to back (localhost:8080) like this:
.constant('API_CONFIG', {
'url': 'http://127.0.0.1:8080/', etc...
My database.php file is configured like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'strict' => true,
'engine' => null,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],
In windows, there is no problem. I have the database in another container and it works. I tried it here in fedora but its the same error.
I get my mysql socket and its: '/var/lib/mysql/mysql.sock'
When i type php -i | grep pdo in the back container i get this:
pdo_mysql.default_socket => no value => no value
Anytime I do local dev w/ docker, and a SQL database (running on the host), the host name for the database that works for me is: host.docker.internal

Artisan migrate error: Illuminate\Database\QueryException

I am trying to migrate and already installed Xampp and also already uncomment the ";extension=php_pdo_mysql.dll" and ";extension=pdo_mysql" in the php.ini file, and still getting that error:
Illuminate\Database\QueryException
could not find driver (SQL: select * from information_schema.tables where table_schema = rav2 and table_name = migrations and table_type = 'BASE TABLE')
PDOException::("could not find driver")
Any ideas?
Thank you!
Maybe, you don't set correct db states at .env.
Check your .env params that you want to connect your database.
for example, table_type = 'BASE TABLE' seems to be wrong?
first, check your php.ini is correct like
# Windows
extension=php_pdo_mysql.dll
# Linux
extension=php_pdo_mysql.so
Is that uncommented ?
if you have ;,remove that.
and you need to restart your xampp after fixing that php.ini.
do /etc/init.d/httpd restart
also your laravel env may be wrong ... so fix .env like:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= your database name
DB_USERNAME= your database user name
DB_PASSWORD= your database password
and you should check your database config like:
/config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your database name'),
'username' => env('DB_USERNAME', 'your database user name'),
'password' => env('DB_PASSWORD', 'your database password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Laravel is not connecting to the php myadmin database

I am trying to connect laravel application to php myadmin.
When DB_HOST=127.0.0.1 in my .env file
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `sessions` where `id` = EO901DpTx4qUrgxMniyykEu84HrJGQESns0ySzlx limit 1)
and when DB_HOST=localhost in my .env file
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `sessions` where `id` = WYha9n6qWtwN0YcAACdydQSgyHeMc1PcID2Mb6Ns limit 1)
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 cachefor php artisan config:clear
my .env file is as follows
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=haydar
DB_USERNAME=root
DB_PASSWORD=
my data.php file is as follows
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Access denied for user 'user'#'localhost' Laravel 5.8

I have this project running on Laravel 5.8 and Database on Mysql using XAMPP server.
This is my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
This is my config/database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
and also created a Database in Xampp named homestead.
this error is showing: SQLSTATE[HY000] [1045] Access denied for user 'user'#'localhost' (using password: YES) (SQL: select * from userswhereemail = test#gmail.com
you need to provide db_name, mysql user_name and password in your .env file.
for xampp default username is 'root' and no password.
so update your .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=matkagu_satta
DB_PASSWORD=

Cannot run Laravel migration on Homestead

I have been trying to run migration since yesterday but database connection is preventing it
Note: I can access the database very well from other projects. This only happens with laravel on homestead. Here is the output of the php artisan migrate command:
SQLSTATE[HY000] [1045] Access denied for user
'abdellah'#'192.168.10.10' (using password: YES) (SQL: select * from
information_schema.tables where table_schema = furbook and table_name
= migrations and table_type = 'BASE TABLE')
This is my .env database config
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=furbook
DB_USERNAME=abdellah
DB_PASSWORD=bonjour1
And this is the database.php file
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'furbook'),
'username' => env('DB_USERNAME', 'abdellah'),
'password' => env('DB_PASSWORD', 'bonjour1'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
I am running on Homestead environment.
Thanks.
Have you made sure everything is spelled correctly?
The only other thing I can think of is adding an extra 0 at the end of the port (DB_PORT=33060).
It's weird but I had a fresh Laravel project using homestead and the only way I could access the DB artisan commands in the terminal was to add a 0 on the port but then I had to take it off again once accessing it through HTTP.
I was able to run the migration successfully by setting the DB username to homestead and DB password to secret.
DB_USERNAME=homestead
DB_PASSWORD=secret

Categories