Laravel Connection Stopped working - php

I have a local Laravel site installed and it was working fine and now it's throwing a DB connection error. Here is the function it's failing on:
My local database is set up, the username is root and there is no password (or the password is blank) so that should work fine.
I'm getting an error:
PDOException
SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)
It doesn't make sense because it was working and as you can see, it's not even recognizing the 'root' username anymore.
I have a file named '.env' in the root of my whole installation and it has the right credentials as far as I know:
DB_HOST=localhost
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
And those are all correct.
Any ideas why this would stop working all of a sudden?
For more data, my app/config/database.php file is:
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),

Just change the root password and do appropriate action in .env and database.php
Please dont use root without having password.after all this do a mysql restart and an apache restart.all the best. :)

Related

Database connection settings with laravel 5.2

I'm developing an application with laravel 5.2, I'm trying to connect a remote database, if I do that in the command line I have to write this:
mysql -u root -p -h 'remote_ip' --skip-secure-auth
if I write that in the command line I don't have any problem.
But now I have to do the connection with laravel, how should I write the options?
--skip-secure-auth on my database.php file
'estudiantesdb' => [
'driver' => 'mysql',
'host' => 'remote_ip',
'port' => '3306',
'database' => 'estudiantesdb',
'username' => 'root',
'password' => 'root_password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
mysql -u root -p -h 'remote_ip' --skip-secure-auth
Here, your password is empty. So in configurations too you have to set password as empty:
'password' => ''
Your config looks like this:
'estudiantesdb' => [
'driver' => 'mysql',
'host' => 'remote_ip',
'port' => '3306',
'database' => 'estudiantesdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],

Laravel 4.2 Session Database Driver Read / Write Host

I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.

pdoexception sqlstate hy000 1044 access denied for user ''#'localhost' to database 'laravel'

code app/config/database.php. i set database name as laravel.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Edit your config file in app/config/local/database.php instead. Or in the config file you have specified the database laravel_crud. Do a search for it if you can't find it.

how to change database name in laravel? changing config>database.php doesn't work

I created a database "mydatabase" and I changed config>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
now inside route.php i have:
Route::get('/', function()
{
$data=DB::table('user')->get();
return $data;
});
laravel sends an Exception which shows that it tries to access:
homestead.user
instead of
mydatabase.user
now if i change route.php to:
Route::get('/', function()
{
$data=DB::table('mydatabase.user')->get();
return $data;
});
it will work!
Also according to this question I changed config>local>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
But this time, even
$data=DB::table('mydatabase.user')->get();
doesn't work either! This time it thrown another exception :
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
My question is why laravel tries to use "homestead" database instead of "mydatabase"? should I change something else?
EDIT:
I changed the config/local/database.php to
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and everything works fine! (I changed mysite.local to localhost)
I've not define local host in my /etc/hosts so why laravel looks for that host?
Your host should be localhost. The term localhost means the computer which laravel is running on. mysite.local is presumably a virtual site residing on this computer. It doesn't have its own installation of Mysql. All virtual sites will share the same mysql. They will just use different databases.
Thats how my setups work anyway.
The problem is in your config/database.php with default connection, currently default connection setting is getting from .env file as
'default' => env('DB_CONNECTION', 'mysql'),
So change it to :
'default' => 'mysql',

Laravel 4 migration:install ErrorException

php artisan migrate:install
{"error":{"type":"ErrorException","message":"PDO::__construct(): [2002] Connection refused (trying to connect via tcp:\/\/127.0.0.1:3306)","file":"\/Applications\/MAMP\/htdocs\/DRCSports\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connectors\/Connector.php","line":47}}
In my database.php I have updated the information to mysql
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'Laravel_DRCSports',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am not sure if I am understanding the error right, but to me it looks like my laravel isn't connecting to mysql right. If that is the case I have no clue how to fix it.
The problem was that mysql is running on port 8888, while Laravel's default port value is 3306 (as it's the default port of mysql servers).
The solution is to add 'port' key to the array (For example: 'port' => 8888) and it'll
do the work.
This is what i did... in /app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'pic',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and at the bottom of the php code
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 8888,
'database' => 0,
),
),
it has to work it...
I experienced problems (Laravel 4) when I used MySQL on a port other than 3306.
A browser-run app expects the following app/config/database syntax:
'mysql' => array(
...
'host' => 'localhost',
'port' => '8889',
...
)
While the command-line run artisan expects the following syntax:
'mysql' => array(
...
'host' => 'localhost:8889',
...
)
The issue is described here:
https://github.com/laravel/laravel/issues/1182
Most articles suggest a workaround using Laravel environments, but it results in duplicate config files and violates the DRY principle (Don't Repeat Yourself), so here's another alternative:
At the top of app/config/database.php:
$my_hostname = 'localhost';
$my_port = '8889';
$my_database = 'database';
$my_username = 'username';
$my_password = 'password';
if (App::runningInConsole()) { // artisan runs from the command line
// change 'localhost' to 'localhost:8889'
$my_hostname = $my_hostname.':'.$my_port;
}
And further down:
'mysql' => array(
'driver' => 'mysql',
'host' => $my_hostname,
'port' => $my_port,
'database' => $my_database,
'username' => $my_username,
'password' => $my_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Cheers
Change your database information in your config/database.php and .env file.
I did these changes and worked like a champ :
in database.php :
host : localhost:8889
Port: 8889
and my mamp has a password so I there was two way to putting that password either in database.php file or in .env file I changed the password '' value to 'forge' and then use my MAMP password in the .env file
by the way, you can see your specific information about MAMP in MAMP application in the port tab (MySQL one)
Make sure to edit this part of ".env" file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
This worked for me.
i guess you can solve this problem by add the following code inside app/config/database syntax:
'mysql' => array(
...
'pconnect' => 'TRUE',
...
)

Categories