SQLite unable to open database file: Laravel + Windows - php

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

Related

Database (database/db_intranet.sqlite) does not exist

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')),

I'm using Laravel 5 and I'm not being able to migrate my database using php artisan migrate

I'm using Laravel 5 but I'm not being able to migrate my database table. I have a macbook pro and I'm using Terminal. I'm using php artisan command:
php artisan migrate.
When I execute this command, I get the following error message: [PDOException]
SQLSTATE[HY000] [2002] Connection refused.
I have configured my database.php following the official tutorial videos on laracasts.com. My database.php file looks like the following:
...
'fetch' => PDO::FETCH_CLASS,
...
'default' => env('DB_CONNECTION', 'sqlite'),
...
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
....
I have read many comments on stackoverflow.com about this issue. Most of them are talking about modifying the ".env" file. The thing is I can't find this file! Which makes me wonder if my installation of Laravel is complete or not! I read that my ".env" file might be overriding my "database.php" file but I can't file the ".env" file!
The env file is most likely hidden.
Run defaults write com.apple.finder AppleShowAllFiles YES and Killall Finder in Terminal.
Open Finder and you should be able to see the .env file.
Edit your .env file.

Laravel Access denied for user 'root'#'localhost' (using password: YES) in laravel 4.2

I have old project which built using Laravel 4.2.I am getting following error
PDOException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
I have googled and tried every think but i couldn't able to fix it
.env file
APP_KEY=az9tq5VHQCV9g5m2CsgY89jtijrCMgEA
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'billing',
'username' => 'root',
'password' => '1234',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Can any one guide me where m doing wrong ?
Note: Before asking question i tried by updating composer update as well as most of the stackoverflow answers.
Updated
I have tested this connection by creating php file
<?php
$servername = "localhost";
$username = "root";
$password = "1234";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
i will get Connected successfully message
My error was Laravel Access denied for user 'root'#'localhost' (using password: NO)
and I only got this error at my host site.
In .env I changed:
DB_USERNAME=user
DB_PASSWORD=password
to:
DB_USERNAME='user'
DB_PASSWORD='password'
Worked for me!
DB_HOST="localhost" worked for me.
And to be safe, wrap .env variables in double quotes to avoid ENV errors:
DB_USERNAME="root"
DB_PASSWORD="#Password"
These will give errors: DB_PASSWORD=#Password, DB_PASSWORD=I have spaces
# is a start of a comment and is exactly the same as //
Run php artisan serve after configure .env, not before.
Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.
I just ran into the same problem on a new Laravel install.
The problem was that I was connecting to my ubuntu localhost mysql server instead of to the vagrant box's mysql server that was on it's own ip address '192.168.10.10'.
I changed over to that and all worked a charm :)
I'm test in Laravel 5.6.33 and i see this error: "Access denied for user 'root'#'localhost'", but the username and password is correct.
In this version i think of this is a BUG, i change DB_HOST, from 127.0.0.1 to localhost and works!
DB_HOST=127.0.0.1 (before)
DB_HOST=localhost (after)
Try In .ENV
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
And Delete Cache Files From Root/boostrap/chache/files
and Run The App
The error says it all, Laravel can't connect to a DB. Check priveleges and make sure the DB exists. Also, try to connect to a DB with a client, like MySQL Workbench using same login and password. This will give a hint about what you can do to fix this. If you can't do this, it's not a Laravel issue.
Try This
.env file
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
config/database.php
default' => env('DB_CONNECTION', 'mysql'),
'mysql' => array(
'driver' => 'mysql',
'host' => env('DB_HOST','localhost'),
'database' => env('DB_DATABASE','billing'),
'username' => env('DB_USERNAME','root'),
'password' => env('DB_PASSWORD', '1234'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
Else checkout the .env file and check the connection
I got the same problem. You should add "" to the DB_USERNAME and DB_PASSWORD
So,
Step 1: DB_USERNAME="root" DB_PASSWORD="1234"
Step 2: Run "php artisan config:clear"
Step 3: Run "php artisan cache:clear"
please delete config file located at bootstrap/cache/config.php
it will be autogenerated when you run php artisan config:cache command
it will work
I faced the same problem when learning laravel using homestead, the problem happened when I try to open a page. It tried to connect to database but got rejected, although I can connect to the database using mysql command line.
The problem was I had wrong assumption that since homestead forward web server request from guest to host, the same thing is also same with myqsl, but apparently it is not.
Homestead has it's own mysql service, so my problem was fixed by:
- homestead ssh into the guest machine
- mysql -u root -p to connect to mysql command line, default password is "secret"
- create the database, but you need to change your password first, just change into the same password as in mysql root's password in your host machine for convenience
- exit the mysql cli and
- php artisan migrate to create the tables
- refresh your browser, now it should be able to connect to your db
Access denied for user 'root'#'localhost' (using password: YES)
The error was coming? The possible reason is your database
connection.If the database is not connect then throw the error.
So Check the database releted all things and If all things is correct.
If not solved then change
DB_HOST=127.0.0.1 TO DB_HOST=localhost
Make sure your .env is using the correct port number. If you're using phpMyAdmin this is how is setup:
MariaDB defaults to 3306
and
MySQL defaults to 3308
So change your .env to use port 3308 for MySQL. I hope this helps!
I changed this line DB_HOST=127.0.0.1 in .env to DB_HOST=localhost and it worked for me.
I had a similar problem, the password is the major issue in such communication, see my picture below, i solved it after changing password, php artisan serve and then reloading my view. It worked.
please login to mysql or phpmyadmin and change your password.
enter image description here
It sounds kind of obvious, but check the credentials of the database.
For me something was wrong in the env file. So I created a fresh installation, copied the env file from there and updated the credentials and it worked.
If you're using docker and still having issues with user access denied issues, you may want to try starting over fresh with the DB, which is was what it took for me.
First, I figured out where my DB data volumes were being stored in my docker-compose.yml file, which ended up being here: [app root dir]/docker/db/data/
db:
container_name: ${APP_NAME}_db
image: mysql:8.0
ports:
- 33060:3306
volumes:
- ./docker/db/data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_DATABASE}
Then I ran this in the app root directory to remove all the docker containers.
docker-compose rm -v
Next I navigated to the DB data files and removed all of them:
cd ~/[app root dir]/API/docker/db/data
sudo rm -rf ./*
Finally, I started up the containers and was then able to connect with my database user credentials.
docker-compose up --build --force-recreate --no-deps
REFERENCE: https://github.com/docker-library/mysql/issues/51
If the username and password are correct and the problem still persists, try clearing the cache using the following method
Route::get('/clear-cache', function () {
Artisan::call('cache:clear');
Artisan::call('route:clear');`
});
Laravel 6 just set secret instead of empty space.
DB_PASSWORD=secret

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).

"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