How to import existing database to homestead?

Daolin picture Daolin · Oct 26, 2015 · Viewed 9k times · Source

I'm learning to use Homestead 2.0. I have a existing MySQL database on my local machine. If I create a new laravel 5.0 project on homestead virtual machine, what should I do to connect to existing database?

  1. If I need to migrate database on Homestead, how should it be done?
  2. If I can connect to my database from Homestead, how shall I configure it on Homestead?

Thanks.

Answer

chebaby picture chebaby · Jan 10, 2016

I had the same problem; this is how i resolve it

I was developing a laravel 4.2 project with WampServer as development environment, later I decided to change to laravel homestead as development environment

Steps:

Save your database as .sql file (my case: exported from phpMyAdmin and saved as db.sql)

Put the .sql file in the root of your project

Navigate to your homestead directory

C:\Users\chebaby\Homestead (master)

Get into the vagrant box

vagrant ssh

Navigate to your project root within the box (where you saved early the darabase .sql file in my case: db.sql)

vagrant@homestead:~$ cd Code/project-folder/

Login to mysql

vagrant@homestead:~/Code/project-folder$ mysql --user=homestead --password=secret

to check if your database already exist run

mysql> show databases;

Else create your database by running this command

mysql> create database yourdatabasename;

Now that you are sure your database is created, exit mysql back to the vagrant prompt

mysql> exit;

Imports the database file by running this

vagrant@homestead:~/Code/project-folder$ mysql --user=homestead --password=secret yourdatabasename < db.sql

All you have to do is to check if your existing database is succesfuly imported

Login to mysql

vagrant@homestead:~/Code/project-folder$ mysql --user=homestead --password=secret

then run

mysql> use yourdatabasename

To show the tables run

mysql> show tables;

hopefully this answers your question

Additional Resources

Laravel Homestead- MySQL default credentials and database

How to import an SQL file using the command line in MySQL?