I've seen that many users here on stackoverflow have a similar issue but following the answers I'm not able to make it work.
When I run:
php artisan migrate
I get:
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)
This is my app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mio_sito',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
The problem is that I don't actually have a password, if I login to vagrant with:
vagrant ssh
I can access mysql with:
mysql -u root
and no password is required.
How can I fix this issue?
According to Laravel docs:
To connect to your MySQL or Postgres database from your main 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.
How did I test it (windows 7 x64 - git bash):
$ vagrant up
$ vagrant ssh
vagrant@homestead:~$ mysql -u root -p
vagrant@homestead:~$ secret
mysql>
I suppose you use php artisan migrate
command outside vagrant and it's why you got this error.
Use vagrant ssh
, move to your project folder (cd /var/www/laravel-project
for example) and use php artisan migrate
.
And it should works.
Since you're able to connect to MySQL, just set a password for root and update your db credentials:
mysql> update user set password=PASSWORD("mynewpass") where User='root';
mysql> flush privileges;
Also make sure your database credentials exist in app/config/database.php
not app.php
.
If you are in the root of your project (mine was called quickstart
), you should first check your .env
file:
nano .env
This should show you something like the following:
APP_ENV=local
APP_DEBUG=true
APP_KEY=6IaYf3hzUt280AGMh2DtMQ2hOKyDa4
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
The make note of the values for DB_DATABASE
,DB_USERNAME
and DB_PASSWORD
, as you will need them in second.
Based on this website, you then want to do the following (note that you might need to add sudo
before, or if you have no password you can just not include -p
:
mysql -u mysql_user -p
You will then be prompted for the password (if you have no password just press enter). Press Enter
after typing it in.
Your next step (now that you are in the mysql database) is to enter the database name that you made of at the beginning, in my case it is homestead
:
create database homestead;
Double check you've created the database with the following:
show databases;
Now create a user (in my case, the user is actually the same name as the database name):
create user homestead;
Now you want to give permissions to that user for that database with the password that you noted at the top (in my case it was secret)
. Also, replace db_name
, db_user
and db_password
with the values you noted in the first step:
grant all on db_name.* to 'db_user'@'localhost' identified by 'db_password';
Now, when you run php artisan migrate
you will hopefully get
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table