are you caching config ?
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
TL;DR;
The error on the title occurs randomly, without even any change of the code, and it gets fixed on reload and continues to work 99% of the time.
Long version
Hello good people. I've been recently facing something weird that I just can't figure out the cause of. In my local Laravel setup, where everything works just fine, but randomly it would show this error:
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
And after I refresh the page, the error is gone, and won't show again till another random occurrence. And I say the word random very strongly, because it is not associated with anything. Sometimes the error would show twice or once a day, and sometimes never at all. Which is very frustrating because I can't figure out how to recreate the error myself. Because 99% of the time it would just work fine.
Here's the things that I've tried:
- Clearing cache
- Re-installing everything via composer
- Making sure the config files are correct (which they are because they work just fine 99% of the time)
Here's the error from the laravel.log file:
[2021-08-26 20:20:36] local.ERROR: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: select `type`, `value` from `options` where `options`.`name` = site_name limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 3D000): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: select `type`, `value` from `options` where `options`.`name` = site_name limit 1) at H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692)
[stacktrace]
#0 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php(652): Illuminate\Database\Connection->runQueryCallback('select `type`, ...', Array, Object(Closure))
#1 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php(360): Illuminate\Database\Connection->run('select `type`, ...', Array, Object(Closure))
#2 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2350): Illuminate\Database\Connection->select('select `type`, ...', Array, true)
#3 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2338): Illuminate\Database\Query\Builder->runSelect()
#4 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2872): Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#5 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2339): Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure))
#6 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(602): Illuminate\Database\Query\Builder->get(Array)
#7 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(586): Illuminate\Database\Eloquent\Builder->getModels(Array)
#8 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Concerns\BuildsQueries.php(259): Illuminate\Database\Eloquent\Builder->get(Array)
#9 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(390): Illuminate\Database\Eloquent\Builder->first(Array)
#10 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php(23): Illuminate\Database\Eloquent\Builder->find('site_name', Array)
#11 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1993): Illuminate\Database\Eloquent\Model->forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'find', Array)
#12 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2005): Illuminate\Database\Eloquent\Model->__call('find', Array)
#13 H:\wamp64\www\laravel\app\Models\Option.php(70): Illuminate\Database\Eloquent\Model::__callStatic('find', Array)
#14 H:\wamp64\www\laravel\app\Providers\ViewServiceProvider.php(119): App\Models\Option::get('site_name')
#15 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(36): App\Providers\ViewServiceProvider->boot()
#16 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#17 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#18 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#19 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php(651): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#20 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(914): Illuminate\Container\Container->call(Array)
#21 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(895): Illuminate\Foundation\Application->bootProvider(Object(App\Providers\ViewServiceProvider))
#22 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(App\Providers\ViewServiceProvider), 25)
#23 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(896): array_walk(Array, Object(Closure))
#24 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php(17): Illuminate\Foundation\Application->boot()
#25 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(237): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#26 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(152): Illuminate\Foundation\Application->bootstrapWith(Array)
#27 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(136): Illuminate\Foundation\Http\Kernel->bootstrap()
#28 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#29 H:\wamp64\www\laravel\public\index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#30 {main}
[previous exception] [object] (PDOException(code: 3D000): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected at H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:352)
[stacktrace]
#0 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php(352): PDO->prepare('select `type`, ...')
#1 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php(685): Illuminate\Database\Connection->Illuminate\Database\{closure}('select `type`, ...', Array)
#2 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php(652): Illuminate\Database\Connection->runQueryCallback('select `type`, ...', Array, Object(Closure))
#3 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php(360): Illuminate\Database\Connection->run('select `type`, ...', Array, Object(Closure))
#4 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2350): Illuminate\Database\Connection->select('select `type`, ...', Array, true)
#5 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2338): Illuminate\Database\Query\Builder->runSelect()
#6 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2872): Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#7 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2339): Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure))
#8 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(602): Illuminate\Database\Query\Builder->get(Array)
#9 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(586): Illuminate\Database\Eloquent\Builder->getModels(Array)
#10 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Concerns\BuildsQueries.php(259): Illuminate\Database\Eloquent\Builder->get(Array)
#11 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(390): Illuminate\Database\Eloquent\Builder->first(Array)
#12 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php(23): Illuminate\Database\Eloquent\Builder->find('site_name', Array)
#13 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1993): Illuminate\Database\Eloquent\Model->forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'find', Array)
#14 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2005): Illuminate\Database\Eloquent\Model->__call('find', Array)
#15 H:\wamp64\www\laravel\app\Models\Option.php(70): Illuminate\Database\Eloquent\Model::__callStatic('find', Array)
#16 H:\wamp64\www\laravel\app\Providers\ViewServiceProvider.php(119): App\Models\Option::get('site_name')
#17 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(36): App\Providers\ViewServiceProvider->boot()
#18 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#19 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#20 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#21 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php(651): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#22 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(914): Illuminate\Container\Container->call(Array)
#23 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(895): Illuminate\Foundation\Application->bootProvider(Object(App\Providers\ViewServiceProvider))
#24 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(App\Providers\ViewServiceProvider), 25)
#25 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(896): array_walk(Array, Object(Closure))
#26 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php(17): Illuminate\Foundation\Application->boot()
#27 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(237): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#28 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(152): Illuminate\Foundation\Application->bootstrapWith(Array)
#29 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(136): Illuminate\Foundation\Http\Kernel->bootstrap()
#30 H:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#31 H:\wamp64\www\laravel\public\index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#32 {main}
"}
And this is my config/database.php file:
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => env('DB_TABLE_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'),
]) : [],
],
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => env('DB_TABLE_PREFIX', ''),
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => env('DB_TABLE_PREFIX', ''),
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => env('DB_TABLE_PREFIX', ''),
'prefix_indexes' => true,
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer body of commands than a typical key-value system
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
];
Here's my .env file:
APP_NAME="Laravel"
APP_ENV="local"
APP_VERSION=0.1
APP_KEY=base64:mAjzmSO/23GKI2cDEpu5VBGOpN7IbZVgvFMqNDIsnd4=
APP_DEBUG=true
APP_URL=
FILESYSTEM_DRIVER="public"
DB_CONNECTION="mysql"
DB_HOST="127.0.0.1"
DB_PORT="3306"
DB_DATABASE="laravel"
DB_USERNAME="root"
DB_PASSWORD=
DB_TABLE_PREFIX="red_"
SESSION_DRIVER="file"
SESSION_EXPIRE_ON_CLOSE=true
LOG_CHANNEL=stack
LOG_LEVEL=debug
BROADCAST_DRIVER=log
QUEUE_CONNECTION=sync
CACHE_DRIVER="file"
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=ap-south-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Environment
- PHP 7.4.2
- MySQL 5.7.14
- "laravel/framework": "^8.12"
- WAMP server on Windows 10
Please any context on the issue would be very much appreciated. I don't want anyone facing this randomly on production. Thank you
After a year, I've figured out the cause of the issue. I'm aware this is an old thread but I'm simply providing the solution in case someone in the future stumbles upon the issue again.
The issue is Laravel by default stores the loaded environment values using putenv() / apache_setenv function which is not thread-safe.
When there's multiple requests are sent to the server, what happens is the following:
- 1st request: variables are not there -> load
- 2nd request: variables are there -> do not load
- 1st request: ends, cleans up variables
- 2nd request: use variable -> does not exist anymore, cleaned up by 1st request.
To avoid this while development all you need to do is add the following in Laravel's
bootstrap/app.php
Illuminate\Support\Env::disablePutenv();
(before initializing the app)
And for production no such modification is needed, as generally configs are cached in production.
Please or to participate in this conversation.