Database (database/db_intranet.sqlite) does not exist - php

I managed to migrate and seed a sqlite database (located in database dir) by replacing the database block in .env with the following
DB_CONNECTION=sqlite
DB_DATABASE=database/db_intranet.sqlite
However when I want to make any operations to the database from the models I'm given this error
Illuminate\Database\QueryException
Database (database/db_intranet.sqlite) does not exist.
Which is solved by changing the block to this
DB_CONNECTION=sqlite
DB_DATABASE=../database/db_intranet.sqlite
My question is, why? And what can I do so I don't have to constantly alter the .env file to work with the database?

In database.php change :
'database' => env('DB_DATABASE', database_path('db_intranet.sqlite')),
to:
'database' => database_path('db_intranet.sqlite'),
Instead of a relative path you need to use an absolute path in your .env file, as :
DB_DATABASE=/var/www/project/database/db_intranet.sqlite
On windows, like :
DB_DATABASE=C:\www\project\database\db_intranet.sqlite
After change, run php artisan config:clear and php artisan cache:clear then serve it.

you have to use relative path in the .env file as
DB_DATABASE=/var/www/project/database/db_intranet.sqlite if you are using Ubuntu operating system.
if you are using windows system then you can use
DB_DATABASE=C:\www\project\database\db_intranet.sqlite
and in config/database.php you can use
'database' => env('DB_DATABASE', database_path('db_intranet.sqlite')),

Related

Laravel database still use old name after .env changed

Edit:
php artisan config:cache work nice, but now I got other problem.
the URL giving me Error 500
I upload a project to a new subdomain area after I changed .env file
when I open URL I still got an error with the old database and user
I tried to check online with the .env file but - I don't know where he stored this database, I tried to see where is this name with ctrl+f but - nothing found
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=faceterc_hot
DB_USERNAME=faceterc_hot
DB_PASSWORD=testpro
I expect to get the same error maybe but not with the old database name.
and this gives me a indicate that maybe the file or something not changed or he using the other details from I don't know where
Good, practice for, If you change in a .env file first restart the server using below command.
php artisan serve
then after run below more command for clear old cache and config.
composer dump-autoload
php artisan config:cache
php artisan config:clear
You can simply do a
php artisan config:cache
In case you are on a shared hosting, you can change the values in config/database.php to only use the set values in your .env. Setting it like this will make sure, it only use the .env values.
mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
php artisan config:clear Solve the problem with DATABASE error
just ran :
php artisan config:clear
with ssh at the main folder.
Just execute
php artisan config:cache
as the laravel cache's the environment files rather than accessing the data from it on every request.
It's always a good practice to run these couple of commands when you change any environment related variable:
php artisan config:clear
php artisan cache:clear
Execute these commands:
php artisan config:cache
php artisan route:cache
php artisan optimize
That will clear your env cache and it should do.

Laravel 5 + PostgreSQL: "Database [postgres] not configured." Error

I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql

How to add sqlite to lumen?

I would like to add sqlite db to my lumen app but I have some troubles.
I create migration:
php artisan make:migration create_users_table --create=users
then I changed my .env file, so it looks like:
DB_CONNECTION=sqlite
DB_HOST=localhost
DB_DATABASE=database.sqlite
then I was created database.sqlite and put it in storage folder and when I 'm trying to do:
php artisan migrate
I have
[InvalidArgumentException]
Database (database.sqlite) does not exist.
I uncommented this lines in bootstrap/app.php:
Dotenv::load(__DIR__.'/../');
$app->withFacades();
I can't find what is wrong.
I work on ubuntu 14.04
In my .env file I changed to:
DB_CONNECTION=sqlite
# DB_HOST=localhost
# DB_DATABASE=database.sqlite
I left only
DB_CONNECTION=sqlite
So Lumen use default config from /vendor/laravel/lumen-framework/config/database.php. It works.
According to lumen-framwork/config/database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', storage_path('database.sqlite')),
'prefix' => env('DB_PREFIX', ''),
],
sqlite is the default setting for lumen.
And you can set DB_CONNECTION in .env as follows:
DB_CONNECTION=sqlite
Then lumen will use storage_path('database.sqlite') as storage/database.sqlite
Otherwise, if you would like to assign DB_DATABASE directly, you should give the full path:
DB_DATABASE=/Users/../../storage/database.sqlite
Step 1 : Open .env file & replace respective database configuration with following piece of code.
DB_CONNECTION = sqlite
#DB_HOST = 127.0.0.1
#DB_PORT = 3306
#DB_DATABASE = homestead
#DB_USERNAME = homestead
#DB_PASSWORD = secret
Note : lines those are started with # are basically commented code.
Step 2 : Create a new file, name it database.sqlite in database folder. It will store out database structure.
Step 3 : There is need to include this file as ignored in our versioning system as there will be so many changes are going to be made with database i.e. insert, delete, update etc. To ignore database.sqlite, Open .gitignore file, and add this line at the end database/database.sqlite. (i.e. path of database.sqlite file).

SQLite unable to open database file: Laravel + Windows

I'm try use a sqlite database in my laravel project, in local environment for dev (Windows 8.1 with AMMPS), but when I try run a migrate:instal command, this error apeear:
[PDOException]
SQLSTATE[HY000] [14] unable to open database file
My database config file (app/config/local/database.php):
<?php
return array(
'default' => 'sqlite',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__ . '\..\..\database\production.sqlite',
'prefix' => '',
),
),
);
try to remove the DB_DATABASE="yourdatabase" variable in your .env file
If the permissions allow to write on the folder just create the storage folders.
e-g, this how I fix the problem.
in config/database.php :
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database/databaseName.sqlite'),
'prefix' => '',
],
Then if you run on your terminal php artisan migrate, it returns you the [PDOException] SQLSTATE[HY000] [14] unable to open database file
Create the path folder by your own, e-g in your terminal mkdir storage/database/databaseName.sqlite
Make sure the permissions allow you to write, then re-run the command php artisan migrate, it returns you the success message : Migration table created successfully.
On laravel 5.3.x
This was the only thing that worked for me:
Change DB_CONNECTION from "mysql" to "sqlite". Because this is an
sqlite database, you can delete all the other DB_* items in that .env
file.
APP_ENV=local APP_KEY=base64: APP_DEBUG=true APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=sqlite
You just need to make sqlite file available at the defined path. In larvel 5.7, I just used following command and run migration.
touch /absolute/path/of/db.sqlite
That will create an empty file at defined location and you are good to go.
Observation: In Larvel 5.7, .sqlite file should be already available at defined location.
For windows, right click and create new file with the defined name at defined location.
try change the file databaseName.sqlite permission database to writeable and readable. Maybe change permissions folder database also.
In the production server change this file database to private directory for security.
Well it may be late but I have also faced the problem in ubuntu environment, here exactly how I overcome this issue.
You have to pass your fully qualified sqlite file's path to your .env file, like following:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="/var/www/html/my-awesome-project/database/database.sqlite"
DB_USERNAME=homestead
DB_PASSWORD=secret
Here change the value of DB_DATABASE key to the exact location of your sqlite file in your computer's filesystem.
Tips:
You can find your database's path for sqlite file by adding
dd(database_path('database.sqlite'));
this line of code in the top of config/database.php file and run php artisan migrate to terminal. You can find the exact location from there, copy it and put it to .envs DB_DATABASE key.
Don't forget to restart laravel server and remove dd(database_path('database.sqlite')); form database.php before testing.
In my case, changing url: '%env(DATABASE_AUTH_URL)%' to url: '%env(resolve:AUTH_DATABASE_URL)%' fixed the issue
Please check the permissions of the directory that database file is stored in. chmod 777 YOU_APP/database
Change DB_CONNECTION from "mysql" to "sqlite".
Because this is an sqlite database, you can delete all the other DB_* items in that .env file.
APP_ENV=local
APP_KEY=base64:
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=sqlite

"SQLSTATE[HY000] [2002] No such file or directory" error when migrating table in Laravel

I am getting the following error when I try to migrate a table in Laravel 5 using the "php artisan migrate" command:
'SQLSTATE[HY000] [2002] No such file or directory' in . . .
/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47
My .env file includes the default settings as follows:
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
My database.php file lists mysql as the default database connection, homestead is listed as the database in my homestead.yaml file, and homestead is one of the tables listed when I access mysql and use the show databases; command.
Any thoughts about what I might be doing wrong?
try 127.0.0.1 instead of localhost in your .env file. It works for me :)
Add mysql.sock path in database.php file like below example
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
Eample
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '8889'),
PDOException SQLSTATE[HY000] [2002] No such file or directory
Change localhost as your DATABASE_HOST in the .env file, change it to 127.0.0.1, and after that run php artisan config:clear and then run php artisan migrate:install again.
Or if your problem is not solved, use these changes in your .env files:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
Then in Database.php add folder location:
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
Finally, try:
php artisan config:clear
php artisan migrate:install
This seems to be an issue with the socket file. Please let me know if this helps Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"
Okay, this is might be a special case to me however I had this issue.
I moved my files from a Laragon based server to a mamp based server on a different machine. I had this error for days and I only received this error when running php artisan migrate and not when loading the page. When loading the page i received table not found which is understandable as the table isn't there.
After quite some time I just though of running composer update and then it fixed the error and worked for me. Not sure if this helps anyone but I came back to this post in case it somehow does.

Categories