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?
Thanks.
Connecting To Your Databases 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 .
A homestead database is configured for both MySQL and PostgreSQL out of the box. To connect to your MySQL or PostgreSQL database from your host machine's database client, you should connect to 127.0. 0.1 on port 33060 (MySQL) or 54320 (PostgreSQL). The username and password for both databases is homestead / secret .
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With