"Access denied for user ''#'localhost' to database 'forge" appears randomly - php

I have a search function for my database but sometimes I get this message:
[2016-02-04 07:03:18] local.ERROR: PDOException: SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge' in C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:55
Stack trace:
#0 C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(55): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
...
In one of ten calls I get this 500 error message, but I don't know why. The other calls give the right result.
.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=bJM6O0MnrIPaTNwKKOqNJkGinRDv1fnc
DB_HOST=localhost
DB_DATABASE=reko
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
Search function:
public function search(Modul $modul, Request $request)
{
$question = Question::whereModulId($modul->id)
->where('value', 'LIKE', '%' . $request->get('keywords') . '%')
->with('tags')
->whereHas('exams', function ($query) use ($request) {
$query->where('date', '>=', $request->get('year').'-01-01');
});
if (!$request->get('parent'))
$question->where('type', '<>', 'parent');
if (!$request->get('own'))
$question->where('type', '<>', 'own');
if (!$request->get('normal'))
$question->where('type', '<>', 'normal');
if ($request->get('answered'))
$question->has('answers');
return $question->paginate(10);
}
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I haven't modified the database.php file and all other calls work great.

PLEASE RUN THIS COMMAND
php artisan cache:clear
THEN
php artisan config:cache

This seems to hold most of the answers : Laravel 5.2 not reading env file
Remember to not to edit your core files as one of the users said.
Just do the safe checks, clear cache, should work.
Hope it helps.
As a workaround you can add to your DB forge account with all privileges.

Try php artisan clear:cache to clear your cache and re-configure your cache php artisan config:cache it might works for you .

I had the same issue once. what I discovered was that my default connection was set to PostgreSQL instead of MySQL so I changed my default connection in database.php
'default' => env('DB_CONNECTION', 'mysql'),
other wise do add following to your .env
DB_CONNECTION=mysql

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=
try adding remaining methods in your .env file

Too often we have to face this error when hosting.
You should see that you have given Privileged Users permission to the database in the hosting.
First
First I need to see if the database name is correct.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zhara_demo
DB_USERNAME=zhara_admin
DB_PASSWORD=zhara#2021
or
Try php artisan clear:cache to clear your cache and re-configure
your cache php artisan config:cache it might works for you .

Related

Laravel SQLSTATE[HY000] [1049] Unknown database - nothing seems to work

I got Laravel SQLSTATE[HY000] [1049] Unknown database Error, when I try to migrate using php artisan migrate:install/ migrate. I have properly named database on my localhost SQL server. I'm using XAMPP to run it.
here's my .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=todo
DB_USERNAME=root
DB_PASSWORD=root
here's database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'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'),
]) : [],
],
also here's create_todo_table.php file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTodoTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('todo', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('yettodo');
$table->string('done');
$table->string('user');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('todo');
}
}
but constantly I keep getting this:
C:\Users\gracj\todo\todo\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("SQLSTATE[HY000] [1049] Unknown database 'todo'")
I am fully aware that there are plenty of threads like this on the Internet. The case is I feel like I've tried everything I could find (including installing different MySQL version - now I'm on 5.7, earlier I was on 8.0). I feel hopeless.
I had to change DB_PORT to 3307 since I had "3306 is occupied" error. I'm on Laravel 8 and PHP 8.0.2.
Please help!
Ok, fixed - very counterintuitive. Here's what I did.
I deleted and created todo database on my server couple of times, every time "php artisan config:clear" afterwards. I did try it earlier, BUT, I changed the DB_PORT in my .env file back to 3306 EVEN TOUGH XAMPP says it's busy, so in XAMPP I had to change it to 3307 (image below). As you can see, these aren't the same! Could you explain that? I'm worried since it might work just once and after few lines of code or migrations, it is possible for it to come back...
Faced a similar issue. However, mine was solved after creating a table on myphpAdmin.
Try:
open XAMP->click on admin in MySQL row and create database as stated in your DB_DATABASE=
try running php artisan migrate again.
First set your Laravel .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=todo
DB_USERNAME=forge
DB_PASSWORD=forge
if so then run:
php artisan config:clear

SQLSTATE[HY000] [1049] Unknown database 'laravel'

I am getting this error while trying to save an object into DB.
SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: insert into cards (card_price, active, updated_at, created_at) values (0, 1, 2019-10-10 15:14:43, 2019-10-10 15:14:43))
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cardgame
DB_USERNAME=root
DB_PASSWORD=P#assword1!
Database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_T_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'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'),
]) : [],
],
CardController.php
public function generateCards()
{
$card = new Card();
$card->card_price = 0;
$card->active = 1;
$card->save();
}
Web.php
Route::get('/generate-cards', 'CardController#generateCards');
Card.php
class Card extends Model
{
protected $guarded =[];
}
Migration file
public function up()
{
Schema::create('cards', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('card_price');
$table->integer('active');
$table->timestamps();
});
}
I've tried clearing cache and also have edited DB_PASSWORD To DB_T_PASSWORD as this corrected a similar issue earlier. Double checked the DB name, passwords etc & also other projects are already running also. I'm not able to figure it out.
Go to your localhost server and drop the database and recreate a new one named cardgame
Go to your laravel project in console and run php artisan config:cache command. That way all your env variables will be used.
Run php artisan:migrate to run your migrations for your database and create your tables.
If you do the above 3 steps in that order you should be fine.
You must clear the cache because your old configuration is in the cache file, just run this command in your terminal for clear cache:
php artisan config:cache
You can solve this problem by 2 simple step.
Step:1 php artisan config:cache
step:2 php artisan migrate
I had the same problem and I solved it following these steps:
1) Stop the server
2) Run php artisan config:clear .This command line will remove
bootstrap\cache\config.php file
3) Start the server and It should work fine now
create database in php admin then migrate your database again
php artisan migrate , it's work 100%
I was also experiencing the same error and implimented all the solutions mentioned above but the error kept reoccuring. But I edited the.env and env.example file where the database = "laravel" to the name of my project for instance transportsytem and it worked.The best solution to the problem is editing both .env and env.example files with the name of your database that you created .

Laravel seems to randomly use the default database.php configuration instead of .env ones

EDIT : Thanks to #PedroFaria99, clear config cache solved the problem, but if anybody want to bring an explanation about the randomness aspect feel free.
I have an issue with my laravel 5.5 local installation (production environnement isn't impacted). Here Laravel is used as an API and serves a client-sided VueJS application.
Sometimes (randomly), my laravel is returning 500 error to my client. It can happens on various routes, never the same one, after 1 to 10 successives HTTP request or not and when I check the storage
[2018-03-09 13:44:08] production.ERROR: PDOException: SQLSTATE[HY000] [1045] Access Denied for user: 'forge'#'#localhost' (password: NO) in [...] Illuminate\Database\Connectors\Connector.php:119
However, my .env file is settup, and my database.php is using env() with default parameters are "forge" and "localhost". So I tried to change this parameter to "test", and the next 500 errors was same but with "test" instead of "forge".
I'm very confused, since this error doesn't happen systematicly.
.env file
APP_ENV=local
APP_DEBUG=true
DB_HOST=localhost
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=
database.php
...
'default' => env('DB_CONNECTION', 'mysql'),
...
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'test'),
'password' => env('DB_PASSWORD', '')
...
Try
php artisan cache:clear
php artisan config:cache
It happens because laravel saves env values in its cache and if your config is not cached it will take the configuaration that are saved in the database.php file. You can not directly access env values on runtime.
I hate to revive a dead thread but after some soul searching I found the issue might just be php's lack of thread safety : https://github.com/laravel/framework/issues/28571 and the great writeup here : https://mattallan.me/posts/how-php-environment-variables-actually-work/

laravel 5.2 sqlite connection error

I have this situation in laravel 5.2 where if I do this:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=database/database.sqlite
DB_USERNAME=
DB_PASSWORD=
in the .env file I get this:
InvalidArgumentException in SQLiteConnector.php line 34:
Database (database/database.sqlite) does not exist.
on trying to manipulate the laravel error rendering system with this code from a controller:
<?php
namespace App\Http\Controllers;
use App\User;
class SampleController extends Controller
{
public function findUser()
{
$user = User::firstOrFail();
return $user->toArray();
}
}
And this goes well with php artisan migrate command, but to get the controller code to render the error as expected I have to do this:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=../database/database.sqlite
DB_USERNAME=
DB_PASSWORD=
To fix the problem so I get both php artisan migrate and SampleController to work I have put this in the config/database.php file:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
Why is the default means to access sqlite as given on the laravel website not working. Is it a bug I should be aware of?
The normal configuration in the config.php file is expected to be
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
Furthermore, the appropriate way to mention the DB_DATABASE in the .env file is to specify the absolute file name, such as /var/www/laravel/database/database.sqlite not the relative location.
For example, in Windows
DB_DATABASE=C:\wamp\www\laravel\database\database.sqlite
And this is what is mentioned in the Laravel Documentation
DB_DATABASE=/absolute/path/to/database.sqlite

Access denied for user 'root'#'localhost' - Laravel

I have an application with Laravel 4, that runs in localhost correctly,
but when I uploaded it in my host I received the error .
app>config>database.php file is:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'forum',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
And bootstrap>start.php is:
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));
This worked for me:
php artisan config:clear
Even though I changed the config details in the .env file, I was getting the Access denied error. Running the above command will clear configuration cache file and hence laravel will read the fresh data from the .env file.
You must find out the credentials of Database host, Database Name , Database Username and Database Password . (If any prefix for tables too) and then replace it with the current credentials.
Use the concept of environments and store values in ENV variables :
http://laravel.com/docs/4.2/configuration
You can store Env Variables as array in .{ENV_NAME}.env.php and access those variables as $_ENV['variable_name'].
Maybe you should add double-quotes for password in env:
# Config: database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD="db_password"
and
php artisan config:clear
Check your mysql database is running perfectly or not. Then check you db user root in mysql with no password and restart your server.
Laravel Access denied for user 'user'#'localhost' (using password: YES)
In .env I changed add ' ' in line 'user' and 'password':
DB_USERNAME=user
DB_PASSWORD=password
to:
DB_USERNAME='user'
DB_PASSWORD='password'
Worked for me!
Run -> php artisan optimize:clear
Good luck <3

Categories