Laravel Access denied - php

I get following error in my laravel application. ( I am using Laravel 5.1)
PDOException in Connector.php line 55:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
at the details it says that I do not parse any password:
array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'gen', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22
My .env file looks like this in Laravel:
APP_ENV=local
APP_DEBUG=true
APP_KEY=************************************
DB_HOST=localhost
DB_DATABASE=gen
DB_USERNAME=miad2
DB_PASSWORD=***************
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
I use phpmyadmin and created that user there.
database.php looks like this:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
'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,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
I stuck here for some hours and do not know how to fix this, need some help.

I figured it out, so what I did now was changing the server name which was set to root and the password which was empty in the config.inc.php to that user i created. And Voila it worked.

Related

Laravel connect to sqlite: No connection could be made because the target machine actively refused it

I'am trying to connect a Laravel API to a sqlite file. But when i try to access a controller- method this error is given:
No connection could be made because the target machine actively refused it.
Here is my .env file:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=xxxx
APP_DEBUG=true
APP_URL=xxxx
LOG_CHANNEL=stack
DB_CONNECTION=sqlite
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
And my config\database.php:
'default' => env('DB_CONNECTION', 'sqlite'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('my-database.db'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'my-database'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
Does anyone have a clue what is wrong in my configuration?
Thanks in advance!

Not able to set multiple connections in laravel

I want to set multiple connections in Laravel application.
I tried using database.php is:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'reports_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'a2z'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
],
and .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reports_db
DB_USERNAME=root
DB_PASSWORD=pass
CL_DB_CONNECTION=mysql1
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=a2z
DB_USERNAME=root
DB_PASSWORD=root
Inside controller
public function index()
{
$ad= new Ad;
$ad->setConnection('mysql1');
$ad = ad::get();
echo $job;
}
But I am not able to set up mysql1 connection.Can you please tell me how to achieve this?
you are actually using the same connection parameters both the time as you are using the same env values in your database.php:
Your .env file should contain something like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reports_db
DB_USERNAME=root
DB_PASSWORD=pass
TWO_DB_CONNECTION=mysql1
TWO_DB_HOST=127.0.0.1
TWO_DB_PORT=3306
TWO_DB_DATABASE=a2z
TWO_DB_USERNAME=root
TWO_DB_PASSWORD=root
and database.php should be something like this:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'reports_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql1' => [
'driver' => 'mysql',
'host' => env('TWO_DB_HOST', 'localhost'),
'port' => env('TWO_DB_PORT', '3306'),
'database' => env('TWO_DB_DATABASE', 'a2z'),
'username' => env('TWO_DB_USERNAME', 'root'),
'password' => env('TWO_DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
],

databse.php is not working in laravel

I am using laravel 5.4. I have configured my config\database.php for mysql but its not inserting the records into mysql. Please see below and please give the solutions for this. why it is not inserting. when I try to insert no response.
<?php
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => 27017,
'database' => env('DB_DATABASE', 'userr'),
//'username' => env('DB_USERNAME', ''),
//'password' => env('DB_PASSWORD', ''),
],
'couchdb' => array(
'driver' => 'couchdb',
'type' => 'socket',
'host' => 'localhost',
'ip' => null,
'port' => 5984,
'dbname' => 'userr',
//'user' => 'username',
//'password' => 'password',
'logging' => false,
),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'insurance'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
?>
here is my .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=insurance
DB_USERNAME=root
DB_PASSWORD=
DB_PORT_MONGO=27017
my controller code
public function insertlogin()
{
//$user = DB::connection('mongodb')->collection('users')->get();
$id = DB::table('login')->insert([
['username' => 'rajeshhhhh', 'password' => 'seven']
]);
return $id;
}
If I try manual connection then it will insert. below manual connection code
public function insertlogin()
{
//$user = DB::connection('mongodb')->collection('users')->get();
$id = DB::connection('mysql')->table('login')->insert([
['username' => 'rajeshhhhh', 'password' => 'seven']
]);
return $id;
}
If you have a Model to you table login, then you can use this method:
Login::create(['username' => 'set_your_username_here', 'password' => bcrypt('set_your_password_here')]);

Getting local.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge''

I'm ocassionally getting this error on my local environment (XAMPP):
local.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge''
.Env file is written as follows:
APP_ENV=local
APP_DEBUG=true
APP_KEY=Dg4KFyDhAePtob94RUZ4frMCak4nHSF2
DB_HOST=localhost
DB_DATABASE=items
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
config/database.php:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path().'/database.sqlite',
'prefix' => '',
],
'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,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
It's Laravel 5.1. It happens very rarely, but it still happens.
What might cause the problem?

How to connect to MySQL database on port 3308 using laravel framework 5.1?

I am trying to use laravel for the first time. I opned the database.php file located in the config directory and then update the mysql config.
but every time I try to do this command
php artisan migrate:install
I get this
[PDOException]
SQLSTATE[HY000] [2002] No connection could be made because the target machi
ne actively refused it.
I have to let laravel to connect to a different port somehow.
I have tried the following and none worked.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5'),
'port' => '3308',
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5:3308'),
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5'),
'port' => env('DB_PORT', '3308'),
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
finally, I tried this
'mysql' => [
'driver' => 'mysql',
'host' => '10.15.1.5:3308',
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
this gives me a different error
Access denied for user 'homestead'#'10.xxxxxx' (using password: YES)
I am not sure where is the user homestead is coming from.
How can I tell laravel to connect to mysql on port 3308?
I know you figured it out, but of all the attempts you provided, the answer you gave wasn't clear. For those looking in the future, here is what you need:
(This is assuming Laravel 5.1 using a Postgres DB, but should work with alternate versions of Laravel, and different DBs... also, don't mind the alternate/different config settings that my database.php has as opposed to yours, these were for advanced configurations.)
Add a 'port' section to your config/database.php, that looks like follows:
'pgsql' => [
'read' => [
'host' => env('DB_READ', 'localhost')
],
'write' => [
'host' => env('DB_WRITE', 'localhost')
],
'port' => env('DB_PORT', '5432'),
'driver' => 'pgsql',
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DB_SCHEMA', 'public'),
'options' => array(
PDO::ATTR_PERSISTENT => env('DB_PERSISTENT', false),
),
],
Then in your .env you can override the port setting as follows:
DB_PORT=32769
I figured out the issue.
the file .env needs to be updated with the correct information

Categories