Daolin
Daolin

Reputation: 634

How to import existing database to homestead?

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.

Upvotes: 15

Views: 11113

Answers (3)

Raghavendra N
Raghavendra N

Reputation: 5179

Note that Homestead is a separate environment and cannot access the DB on your host machine. Laravel Homestead comes with a mysql database installed. You should use this mysql for the code running in the Homestead. One easy way to solve your problem is to install phpMyAdmin on Homestead box and and then import the DB to mysql in Homestead.

Steps to install phpMyAdmin: Got the below answer from here and added my notes

  1. Download latest version of phpMyAdmin from https://www.phpmyadmin.net and extract it into a directory in your host machine.
    Note: I extracted to /var/www/phpmyadmin/public. Here public directory is important otherwise you will receive No input file specified. error when you access phpmyadmin.
  2. Configure homestead.yaml file to add a site where you can access phpmyadmin

    folders:
        - map: /var/www/phpmyadmin
             to: /home/vagrant/code/phpmyadmin

    sites:
        - map: phpmyadmin.app
             to: /home/vagrant/code/phpmyadmin/public

  3. Add this line to your \etc\hosts file:
    192.168.10.10 phpmyadmin.app
    Note: 192.168.10.10 is the ip in my homestead.yaml configuration file.

Now start the vagrant by running vagrant up and you should be able to access the mysql of homestead by going to the url phpmyadmin.app. Now that phpmyadmin is installed you can import your DB easily.

Upvotes: 1

maiorano84
maiorano84

Reputation: 11971

Treat your Homestead Installation as if it's a remote host operating on a completely different machine. Homestead doesn't know anything about your localhost, and therefore cannot connect directly to your locally hosted MySQL databases.

Your Homestead installation operates on its own IP address. Whatever IP that is (likely 192.168.10.10), you can connect to it using MySQL Workbench or SequelPro using the following credentials:

Host: Your Homestead IP
Port: 3306
Username: homestead
Password: secret

You can find your IP by opening the following file:

~/.homestead/Homestead.yaml

If you're on Windows, then it would likely be:

C:\Users\your-username\.homestead\Homestead.yaml

In order to interact with your database, make sure Homestead is running first by running vagrant up in your Homestead directory, otherwise it will error out.

After that, you can export your local database and import it back into one of your Homestead databases using the credentials described above.

Upvotes: 3

chebaby
chebaby

Reputation: 7750

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?

Upvotes: 26

Related Questions