Przemysuaw83's avatar

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

Hi i'm new in laravel. I know that this is fairly popular problem. I've beenn searching for the solution, but it seems that my case is litle bit different.

Because i can do the "php artisan migrate" and my database is changing, but when i try to see results in my browser then i gets that SQLSTATE

Here is my .env : APP_ENV=local APP_DEBUG=true APP_KEY=G4snymV7W8EQ0vJTgVQ55R9eYB1qZyF0

DB_HOST=localhost DB_DATABASE=todolists DB_USERNAME=root DB_PASSWORD=mysql

my database.php: 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'todolists'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', 'mysql'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],

My routes.php: Route::resource('lists', 'ListsController');

My ListsController.php : public function index() { $lists = Todolist::all(); return view('lists.index')->with('lists', $lists); }

I'm searching the solution for week and I got stuck.

thank you in advance :)

0 likes
21 replies
Przemysuaw83's avatar
Here is my .env : 
APP_ENV=local
APP_DEBUG=true
APP_KEY=G4snymV7W8EQ0vJTgVQ55R9eYB1qZyF0

DB_HOST=localhost
DB_DATABASE=todolists
DB_USERNAME=root
DB_PASSWORD=mysql
Przemysuaw83's avatar
'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'todolists'),
    'username'  => env('DB_USERNAME', 'root'),
    'password'  => env('DB_PASSWORD', 'mysql'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
    ],
Przemysuaw83's avatar

My routes.php:

Route::resource('lists', 'ListsController');

My ListsController.php :

public function index() { 
    $lists = Todolist::all(); 
    return view('lists.index')->with('lists', $lists); 
}
mezie's avatar

This error is as a result of not been able to connect to MySQL. Double check your login details (username and password) to make sure it is correct.

bashy's avatar

Firstly, where are you hosting your code? Homestead? XAMPP? Mamp?

Secondly, go on command line and do some login tests

mysql -u root -ppass
Przemysuaw83's avatar

I installed MySQL Server 5.7 and with that CLI is working perfectly and i can see changes also from phpMyAdmin WAMP. So

mysql -u root -ppass

works.

I've got localhost WAMP and homestead

Homestead.yaml file:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

aliases: ["dev.todoparrot.com"]

folders:
    - map: E:\wamp\www
      to: /home/vagrant/www

sites:
    - map: dev.todoparrot.com
      to: /home/vagrant/www/dev.todoparrot.com/public

databases:
    - homestead

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

and when i'm trying to open from Homestead then i got that errror.

When I'm opening site from localhost WAMP i'got:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12 Server at localhost Port 80

config.inc.php file from phpMyAdmin:

$i = 0;

/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = '';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'mysql';
$cfg['Servers'][$i]['AllowNoPassword'] = true;

/* End of servers configuration */

$cfg['blowfish_secret'] = '56c1d20b783cc5.24464231';
$cfg['DefaultLang'] = 'en';
$cfg['ServerDefault'] = 1;
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';
?>

Another strange thing is that the php artisan migrate is forks fine.

I get that error when i'm trying to lists the content of database.

I hope that this will help solve my problem :).

mezie's avatar

@Przemysuaw83 if you are using homestead, the default username is homestead and password is secret.

If you want to use wamp, you will have to do some other config like vhost to allow is work.

Przemysuaw83's avatar

@mezie you're right . I've changed :

.env file:

APP_ENV=local
APP_DEBUG=true
APP_KEY=G4snymV7W8EQ0vJTgVQ55R9eYB1qZyF0

DB_HOST=localhost
DB_DATABASE=todolists
DB_USERNAME=homestead
DB_PASSWORD=secret

database.php file:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'todolists'),
    'username'  => env('DB_USERNAME', 'homestead'),
    'password'  => env('DB_PASSWORD', 'secret'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
    ],

i've created database 'todolists' using mysql CLI with ssh vagrant@homestead and now I can't do the migration. When i try i get:

 SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

and of course when i try to lists my todolists table from todolists database i've got:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'todolists.todolists' doesn't exist (SQL: select * from `todolists`)

which is quite obvious :).

I'm on Windows 7 if it matters.

Snapey's avatar

try switching to using 127.0.0.1 instead of localhost.

If this does not work straight away, check your port number for SQL. If its 3306 then it should be fine. If its 8889 then you will need to add a port statement to the database config entries.

Snapey's avatar

And you previously proved that "root" and "pass" work so why try changing? Unless you are confused by having a database in Homestead and another in WAMP?

Przemysuaw83's avatar

@Snapey i changed because i'm new in L and i was confused. I want first get familiar whith one DB at least :). I tried with 127.0.0.1 and my port is 3306. Still same result.

Snapey's avatar

Yes, but are you clear? There is one database in homestead and one in WAMP. if you SSH to the homestead box and then PHP Artisan Migrate you are accessing the MYSQL database on homestead server. Yes?

Choose to do your development using one or the other and then forget the one that you don't want to use.

To access the database on Homestead from your windows machine;

To connect to your MySQL or Postgres database from your host machine via Navicat or Sequel Pro, you should connect to 127.0.0.1 and port 33060 (MySQL) or 54320 (Postgres). The username and password for both databases is homestead / secret.

Your .env file needs to contain the details for the mysql install in homestead since these setting are from the perspective of the software running within the virtual machine.

DB_HOST=127.0.0.1
DB_DATABASE=todolists
DB_USERNAME=homestead
DB_PASSWORD=secret
bashy's avatar

@Snapey I would even go as far as to use the Homestead VM IP for MySQL. Saves local/vm confusion when running commands or viewing it from the browser.

Przemysuaw83's avatar

Ok so i did everything all over again

  1. CREATE new user 'homestead' pass 'secret'
  2. grant all priviliges
  3. switch user to 'homestead'
  4. create database 'todolists'
  5. connect from mysql worbench 6.3 CE - OK
  6. I did the migrations - everything is working i see changes
  7. i use php tinker and create new Todolist model its working

And then i try to list this in browser and i got PDOException in Connector.php line 55: SQLSTATE[HY000] [1049] Unknown database 'todolists'

i did the: php artisan cache:clear php artisan config:clea

Snapey's avatar

you could still be using the wrong database.

Przemysuaw83's avatar

@Snapey i think you're right But I totally don't understand. When in my welcome.blade i do:

<?php if(DB::connection()->getDatabaseName())
{
    echo "Connected sucessfully to database ".DB::connection()->getDatabaseName().".";
} else {
    echo "not connected";
}
?>

I get true and in my browser is displayed: "Connected sucessfully to database todolists."

but when i try:

<?php if(DB::connection()->getDatabaseName())
{
    echo "Connected sucessfully to database ".DB::connection()->getDatabaseName().".";
    $user = DB::table('todolists')->where('name', 'Jose')->first();

    echo $user->name;
} else {
    echo "not connected";
}
?>

i get :

"ErrorException in Connector.php line 55: SQLSTATE[HY000] [1049] Unknown database 'todolists' (View: /home/vagrant/www/dev.todoparrot.com/resources/views/home/welcome.blade.php)"

(in database 'todolists' there is a table 'todolists')

Przemysuaw83's avatar
Przemysuaw83
OP
Best Answer
Level 3

Ok. Problem solved of course @Snapey had right i didn't understand. I had to read a litle, about what am i messing with. I messed up two different databases :).

First of all i didn't add the DB to homestead.yaml now is:

databases:
    - homestead
    - todolists

Next i created DB in homestead not in WAMP or Mysql server 5.7 And change .env file to:

DB_HOST=127.0.0.1:3306
DB_DATABASE=todolists
DB_USERNAME=homestead
DB_PASSWORD=secret

and database.php:

 'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', '127.0.0.1:3306'),
    'database'  => env('DB_DATABASE', 'todolists'),
    'username'  => env('DB_USERNAME', 'homestead'),
    'password'  => env('DB_PASSWORD', 'secret'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
    ],

Thanks for your time guys :).

shefali's avatar

i am using xampp in ubantu. facing the problem 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: no)' while submiting a form in laravel. can anyone help??

omidahmadyani's avatar

if you config your .env file and other methods. remove file /bootstap/cache/config.php

2 likes

Please or to participate in this conversation.