Laravel Homestead- MySQL default credentials and database

user859375 picture user859375 · Aug 26, 2014 · Viewed 42.7k times · Source

I have setup Laravel and trying to run the artisan migrate command however I am getting the error below

[PDOException] SQLSTATE[HY000] [2002] Connection refused.

I am not sure how db is setup in Homestead. So I got the below questions.

  1. Is default database created automatically by artisan migrate or Homestead?
  2. If it is, what is the name of it?
  3. If its not created by default, should we create before running migration?

I tried logging in to MySQL db by connecting to Homestead VM using ssh and then running MySQL. However I get error Access denied for user.... for user name vagrant, Homestead and forge.

What is the default credentials? I understand that creating MySQL db is out of the scope of Laravel tutorial; So it would be helpful if anyone can answer these questions and point me in right direction.

Answer

Dwight picture Dwight · Aug 26, 2014

Homestead comes with a default database called homestead. Your app can either choose to hook into that database, or you will have to go and make a new database manually. You can either use a GUI (like Sequel Pro on Mac) or perform it via the command line through Vagrant.

// SSH into the box
vagrant ssh

// Connect to MySQL as the homestead user (password is: secret)
mysql -u homestead -p

// Create a new database in MySQL
CREATE DATABASE your_app_name;

// Leave MySQL
exit;

You can then migrate the database as usual, php artisan migrate.

If you need to do this with Postgres instead, it's pretty similar.

// Connect to Postgres (password is: secret)
psql -U homestead -h localhost

// Create a new database in Postgres
CREATE DATABASE your_app_name;

// Leave Postgres
\q