Laravel Database Connection Error Illuminate\Database\QueryException - php

I have the problem with database connection after i ran a migration which is Illuminate\Database\QueryException. I've tried to ran the "php artisan migrate:fresh" command, but it didn't work. What should I do? Thanks in advance.
Here is the image

Check your php path in environment variables, php path must be of XAMP i.e "C:\xampp\php". Set this and then try to migrate again

Related

Laravel: Database connection error when using "config:cache", but working fine without caching

Now this is a really weird one.
My .env file is fine. When I run
php artisan config:clear
everything works. But when I run
php artisan config:cache
my database connection stops working:
SQLSTATE[HY000] [1045] Access denied for user 'foobar'#'xxx.xxx.xxx.xxx'
So again: My settings are correct. But when they are cached, they stop working. How could this possibly be?! Laravel seems to use some kind of old settings when using the cache, but I have no idea where these are coming from?
What I already tried:
php artisan optimize:clear
rm -rf bootstrap/cache/*.php
I ran into this issue while trying to setup a Laravel forge deployment script for zero downtime.
I hard coded the path to the ssl files for my database connection in /config/database.php. Changed that to use the values of .env, now it's working.

Laravel 6 config()->get('database.connections.mysql') not matching DB:connection()

Prerequisites
In my local environment I am working with multiple tenants and Redis (Auth required).
To serve the project I am using Valet.
For this case I am addressing these two connections:
- basic_foo (is defined in my .env)
- tenant_foo (is the one to change to during a request)
Until now I successfully changed the connections like so:
config()->set('database.connections.mysql',
array_merge(
config()->get('database.connections.mysql') ,
['database' => 'tenant_foo']
);
Problem
However, now I am seeing an issue with the query builder, keeping or falling back to the basic connection.
I get the expected connection results of tenant_foo (same for Redis) when I run
dd(config()->get('database.connections.mysql'));
I get the wrong but apparently active results of basic_foo when I run
dd(\DB::connection()); // returns Illuminate\Database\MySqlConnection
So all in all the app will return this Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'basic_foo.table_bar' doesn't exist...
where it should search for
'tenant_foo.table_bar'
Things that did not solve the problem yet
restarting Redis
reinstalling Redis
php artisan config:cache
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan optimize
composer dump-autoload
Simply changing the database name to tenant_foo like below is not enough, as the config array remains the same of basic_foo.
\DB::connection()->setDatabaseName('tenant_foo');
Thoughts
I want to change the config-array the of \DB::connection(), but I don't know another way than the config->set().
I installed Telescope could this affect the db connection?
Any other ideas?
To dynamically change database name you should use:
DB::disconnect();
Config::set('database.mysql.database', 'tenant_foo');
DB::reconnect();
This worked for me:
\DB::disconnect('mysql');
Config::set('database.connections.mysql.database', 'tenant_foo');
\DB::reconnect('mysql');

Artisan migrate command no output

I'm trying to setup already made project but I'm stuck at the migrating the DB. I'm using Laravel (5.6.34) on PHP 7.2.9, and MySQL (8.0.12). If I try to run php artisan migrate command I'm getting no output, command stays "active" but nothing happens, no errors.
I tried making fresh project, adding migration and running the same command, same thing. I noticed that all other (or at least bunch of them I tried) artisan commands are working but only migrate one doesn't.
.env info is correct and mysql is up and running.
Any help is appreciated.
Thank you!
Try the following;
1. Check your migration files for content.
2. Flush config with php artisan config:clear
3. Check if database is connected using tinker. Run php artisan tinker then paste in this code; DB::connection()->getPdo();
4. If PDO object is returned, your app is connected to the database. If not and PDOException with message 'SQLSTATE[HY000] [2002] Connection refused' is returned then mysql is not running. If database does not exist, you get the error PDOException with message 'SQLSTATE[HY000] [1049] Unknown database [your_db].
Assigning the password to DB User solved this issue for me (be sure populate DB_PASSWORD inside .env)
mysqladmin -u YOURDBUSER password 'newpassword'

Laravel 5.5 can't connect to database

I have a larvael and I want it to make it in production. I have a server hosting and database on infomaniak.com.
I have upload all my file and I get a SQL timeout error :
SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from
characters where checked = 1 order by RAND() limit 1)
I have an other Laravel website on infomaniak and don't have this problem. I have check my .env file, my /config/database file and try php artisan config:clear or php artisan cache:clear. I also try to dump DB:connection() and the connection information are right.
I can connect to my homestead database in local and work fine. But when I try to connect to the SAME database than my other website with the same .env file and still have the timeout error.
How can I fix this ? Are there a way to clear another cache ? Completly lost :/
EDIT :
If i try :
if(DB::connection()->getDatabaseName()) {
echo "Yes! successfully connected to the DB: " . DB::connection()->getDatabaseName();
}
The page return Yes! successfully connected to the DB: db_name
EDIT 2 :
I just see my hosting server is in PHP 5.6 but it say that's my website is in PHP 7. Really confusing but maybe this informations is important (have another laravel on this server and works like that)
Your problem is the php version, if you're using laravel 5.5 then your host must have php 7 installed.
See the oficial docs here

Laravel 4.1 to Heroku: SQLSTATE[HY000] [2002] No such file or directory

I deployed my Laravel 4.1 app in Heroku by following this article http://phpartisan.tumblr.com/post/71580870739/running-laravel-4-on-heroku
The static HTML homepage loaded okay but when I am trying to log-in, I am getting the error
SQLSTATE[HY000] [2002] No such file or directory
My local setup is Laravel 4.1 & MySQL.
I don't think there was a database created when I deployed.
Do I need to do it manually? How and what will be my settings then?
thanks!
The problem could be your environment might still point to local. in cli, type heroku run php artisan env to find out.
else, you might wanto run php artisan migrate --env=production to set it to the correct production environment.
you might then encounter another issue like this:
PHP Fatal error: Call to undefined function Symfony\Component\Console\mb_convert_variables() in /var/www/mysite/vendor/symfony/console/Symfony/Component/Console/Application.php on line 1154
Trust me, tt has nothing to do with mb_string module like what this post stated.
Laravel: Call to undefined function Symfony\Component\Console\mb_convert_variables()?
The problem is at environment setting.
First, make sure you have .env.php file at same dir level with composer.json and it is exists in your heroku instance. Since you cant FTP inside, you need to push to repo. But laravel tell you this is git ignored, so you will need to temporary allow it to push to your heroku repo.
Next, you will need to edit the bootstrap\start.php
replace the $env = $app->detectEnvironment(function(){}) to:
$env = $app->detectEnvironment(function() {
return file_exists(dirname(__FILE__) . '/../.env.php')
?
'production'
:
'local';
});
Once you done all these, rerun the migrate command and it will works.
You may follow this link for reference.
Laravel environment config not loading, migration fails - SQLSTATE[HY000] [2002] No such file or directory

Categories