Laravel 5.6 cannot connect to database after clearing cache - php

i cannot connect to database after clearing even php artisan is not working.
here is how i clear config caches:
php artisan cache:clear
then it's cleared successfully but when i run:
php artisan config:cache
i got this error:
<pre><strong>ERROR:</strong> Can't connect to the database server. SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)</pre>%
i also tried: composer dump-autoload
but still no luck. Once in the past i got same error and i was able to solve it but no idea how. If someone knows solution then kindly explain it as well.
My php version: PHP 7.2.5-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: May 5 2018 05:00:15) ( NTS )
Thank you!

User you are using to connect the database in not having privilege to connect, please provide that access to your db user

I don't know if i'm allowed to answer my own question or not. Let me know if it's not allowed to answer to your question.
In bootstrap directory which is located in root of your app. There i saw cache directory and then i just delete it and run:
composer dump-autoload
then it's works. But even if i try to run:
php artisan config:cache
it gonna be successful. But if after this you may get same error again:
php artisan serve
ERROR: Can't connect to the database server. SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)
After this i try to delete config.php inside bootstrap/cache/.
it gonna work even after deleting that file.

Related

SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES) (SQL: select * from `cats`)

I am building a basic CRUD app using Laravel 7 Homestead in a Vagrant VM. The root address http://crud-app.local.test works, but when I add a defined route (http://crud-app.local.test/cats) I get this error page:
Error message in browser when navigating to '/cats' route.
So far I have:
Verified my MySQL password matches in the .env file and in practice
Changed the MYSQL password so that it's no longer empty (and made sure it matches what's in the .env file)
Ran php artisan config:cache
Ran php artisan config:clear followed by php artisan cache:clear
Changed 127.0.0.1 to localhost
Made sure the database defined in the .env file exists (crud)
Ran php artisan migrate (no errors, and the cats table exists in the crud database)
Restarted and re-provisioned the machine (vagrant reload --provision)
I'm at a loss now.
Make sure that you give "root" permissions to read from the database that you've created. As it's root#localhost rather than root#127.0.0.1 sometimes mysql differentiates. Just check the permissions that root#localhost has for the database and if you update them, flush the privileges.

Laravel: Access denied for user using No password

I uploaded my laravel project to my shared server and when I try to connect to it through Postman and register a new user but it gives me the following error:
Illuminate\Database\QueryException: SQLSTATE[28000] [1045] Access denied for user 'stylmyvz_haraj'#'localhost' (using password: NO) (SQL: select * from `users` where `email` = bella#alexa.com limit 1) in file /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
I am wondering why it gives me that Access denied (using password: NO) although I set the password in .env file
I run 'php artisan serve' on the server but I receive this error:
ErrorException
chdir(): No such file or directory (errno 2)
at vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:43
39| * #throws \Exception
40| */
41| public function handle()
42| {
> 43| chdir(public_path());
44|
45| $this->line("Laravel development server started: http://{$this->host()}:{$this->port()}");
46|
47| passthru($this->serverCommand(), $status);
+15 vendor frames
16 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
Any solution for it?
If you've updated your password in the .env file and haven't restarted the server it might not be seeing the changes in the .env file. If you're using php artisan serve just kill that command and run it again. If you're using a different web server, do whatever you need to do to restart the web server and see if that helps.
Edit:
Learning that you're on a shared host, try running php artisan config:cache and see if that helps. Side note: You cannot run php artisan serve because you're already running a server on the shared host.
Another Possibility:
It's possible that the user 'stylmyvz_haraj'#'localhost' is not actually on the database listed in the .env file. You might need to check your shared host management area to ensure the user and password are set properly on the appropriate database.
I found the answer in this post here:
Solution
the problem was that password was starting with # which it was considered as a comment so I put the password like this: '#fhh344+9f' and it finally worked

PHP artisan returns access denied for database connection in Laravel

I transferred a Laravel project based on moving Laravel project between computers
Everything was fine at the first look. I could install the composer without any problems, then I set my environmental variables in my .env such as database name, database user and so on.
When I started using the following command,
php artisan cache: clear
I got these two errors,
In Connection.php line 664:
SQLSTATE[HY000] [1045] Acess denied for user 'root'#'localhost' (using password: Yes) (SQL: select * from information_schema.tables where tabale_schema = *** and table_name = ****)
In Connector.php line 67:
SQLSTATE [HY000] [1045] Access denied for user 'root'#'localhost' (using password: Yes)
Further Information:
It seems .env has not been read by the application because when I browse the homepage of the app, I got Whoops error which shows environmental variable is empty.
I tested my database connection such as its username, password, and other parameters, I know they are working properly.
In the end, I attached a photo in order to elaborate on the issue.
Interestingly, in my .env file, DB_DATABASE value is "nlp" and DB_USERNAME equals "Javad" but as you can see in the errors, they are not working, and the Artisan assumes the root as the user!
After you move a Laravel project to another location, The first thing you have to do is regenerate APP_KEY in the .env file by running the command from your application root directory:
php artisan key:generate
After that, you reconfigure your cache:
php artisan config:cache
Additionally, make sure all your database parameters in your .env file and config/database.php are correct and the same as your previous Laravel setup - You can change them manually and then reconfigure your cache as shown above. If not (for instance, the database name is changed), you may have to rerun your database migrations and seeders again:
php artisan migrate
and
php artisan db:seed
I think the configuration files are not writable by the apache user, considering you are using apache. Make sure you have given write permission to bootstrap/cache for the user. This directory contains the cached configuration files.If that is the case the congratulations will be loaded from here and your previously used configuration will be loaded which may be causing the issue.
Also you need to give write permission to the storage directory too. As there is the log file and apache user needs to write on that too.
Step 1: Go to the project path project_name\bootstrap\cache folder and remove the marked file config.php from the mentioned folder.
Step 2: Run the following command
composer dump-autoload
php artisan clear-compiled
php artisan config:clear
php artisan cache:clear
php artisan config:cache
I hope that it will work now!
Open the .env file and edit it. Just set up correct DB credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= // Your Database Name
DB_USERNAME= // Your Database Username, If no username has been set then by default there is a root as a username
DB_PASSWORD= // Your Database Password, If no password is set on the database, clear it
After completion of .env edit please enter this command in your terminal for clear cache:
php artisan config:cache
If there is still the same error then try another possible solution.
I had also face the same problem, I tried all the possible answers in StackOverflow but nothing could help me, this solution worked on the first try:
If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server after changing your .env file values.
I found this solution from here laravel.io (Read the solution of Byjml)

Laravel artisan route list database connection error

I'm using Laravel 5 and when I am running php artisan route:list, it always gives me an error of SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES). This shouldn't be because my application will not be connecting to any database since it will be a static website.
I tried removing the variables on the .env file:
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Just still gives me errors. It seems like it is still connecting to the database wherein it shouldn't make any database connection.
I was experiencing the same issues when I upgraded my Laravel installer from v1.1 to v1.2.
It appears that the routes.php file by default is adding the Authentication route:
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
This causes to display an error message to what you were describing:
$ php artisan route:list
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'forge'#'localhost' (using password: NO)
I resolved this by removing the scaffolding included within Laravel by executing the following command:
$ php artisan fresh
Or by simply deleting the Auth route within the file itself.
You should now be able to run: php artisan route:list
Hope this helps!
This might be the problem of localhost. Do check your WAMP, MAMP, LAMP and XAMP MySql server if you are using any of them.
This have solve my problem:
It looks like artisan might be working in the wrong environment. Run "php artisan tinker", then "App::environment();" to see which environment Artisan is running in. If it is something other than production, you need to create a folder with that environments name within your app/config folder, and put a copy of the "database.php" file in it.
After That if you are not doing any DB related task it will not access DB.
I found answer here
Alternatively You can run following command to resolve this issue:
php artisan migrate:install --env="local"
After this run you command:
php artisan route:list
Enjoy.

Laravel 4 with vagrant database migration "using password NO" reset

I am currently getting an error when I do my database migration with artisan with the command
sudo php artisan migration
I then get the following error:
SQLSTATE[28000][1045] Access denied for user 'root'#'localhost' (using password: NO).
I don't know where I would be able to reset the password or how and I have been looking for quite some time now. Any advice would be great. I am running this in a vagrant box. Thank you.
Edit your app/config/database.php file

Categories