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?
If I need to migrate database on Homestead, how should it be done?
If I can connect to my database from Homestead, how shall I configure it on Homestead?
Thanks.
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.
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
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.
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
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.
Related
I am using Sequel Pro on Mac and trying to see my database schemas that Laravel created. In the migrations folder I see a users and password reset tables. I am trying to see them in sequel pro which I do not.
I used homestead and the vagrant box is up. The homestead.yaml is set to defaults to:
IP: 192.168.10.10
MySQL db: homestead
In the .env file I see the defaults too:
IP: 127.0.0.1
MySQL: homestead
User: homestead
Password: secret
I try to use both IP's but can't seem to connect and if I do connect with 192.168.10.10 I see 0 tables. What am I missing? New to Vagrant and Laravel too.
You should try to declare the DB connection to
config/databases.php
note that you can declare multiple DB connections here if you don't use .env for course.
then try php artisan cache:clear
I am using phinx for migrations in my web app.
my phinx.yml:
paths:
migrations: %%PHINX_CONFIG_DIR%%/migrations
environments:
default_migration_table: app_migrations
default_database: app_database
development:
adapter: mysql
host: localhost:8888
name: app_database
user: ''
pass: ''
port:8889
I have mamp with ports apache:8888 and mysql:8889 running.
The database app_database does exist.
The table app_migrations does not exist(it will be created while migrating right?)
I already created some migrations and now i try to run them:
php vendor/bin/phinx migrate -e development
output in console:
Phinx by Rob Morgan - https://phinx.org. version 0.5.1
using config file ./phinx.yml
using config parser yaml
using migration path /path/to/directory/migrations
using environment development
using adapter mysql
using database app_database
Then it stops and nothing happens...
Anybody could help me with this issue?
Thanskj and Greetings!
Maybe there is some config-hickup. I know this phenomenon from conneting to a mysql-database that is not reachable via a specific host / port. This always leads to timeouts but only after eg. 60 seconds.
You wrote this in your config:
host: localhost:8888
[...]
port: 8889
First remove the port from the hostname:
host: localhost
port: 8889
and as second verify the mysql-server is really listening on localhost / port 8889.
You'll find more on in the "Configuration"-chapter of phinx-documentation: http://docs.phinx.org/en/latest/configuration.html
Regards
If you're using the default settings for MAMP, the username for the database would be root. Try changing the value of user in phinx.yml to root.
I have been using XAMPP for quite a time, and after discovering Laravel and finding out, that I quite like it, I also wanted to use Homestead. The problem I'm having is, that I can't seem to be able to run multiple sites.
I have tried various things, but the main problem currently is, that one project works, while all the others are getting a connection timeout, when trying to access their webpage.
These are the steps I've taken to use Homestead
Installing VirtualBox
Installing Vagrant
Adding homestead with vagrant box add laravel/homestead
Clonging the repository git clone https://github.com/laravel/homestead.git Homestead
Create Homestead.yaml file in the Homestead directory with the bash init.sh script
Create a new project laravel new projectA
Require homestead composer require laravel/homestead
Generate Vagrantfile php vendor/bin/homestead make
Modify the Homestead.yaml to have an IP that ends with 10
Create another project laravel new projectB
Require homestead composer require laravel/homestead
Generate Vagrantfile php vendor/bin/homestead make
Modify the Homestead.yaml to have an IP that ends with 11
Adding those two sites to the hosts file sudo nano /etc/hosts => xxx.xx.xx.10 projecta.app & xxx.xx.xx.11 projectb.app
Starting vagrant from one of the two directories vagrant up
Now, I'm having the problem, that only one of the projects is reachable. It's always the one from where I called vagrant up. So if I call vagrant up from Project A I can access http://projecta.app and http://projectb.app times out. The same the other way around, sadly.
This is my vagrant global-status
id name provider state directory
------------------------------------------------------------------------
fc6fadb default virtualbox running /Users/mknb/work/projectA
I thought I would just do another vagrant up from the projectB directory but that doesn't work of course.
I don't want to use the global Homestead, because Laravel said, that it is possible to have a per project installation, so how do I achieve it? Do you need more information?
I didn't modify the Homestead.yaml except of the IP and the domainname homestead.app => projecta.app
It seems like a global installation is fairly easy with Homestead, since I would just have to add more sites to the Homestead.yaml, but as I said I'd like to have a per project installation. Can anybody help?
Starting vagrant from the Homestead directory doesn't work of course.
By using Homestead in your way, you create a virtual machine for each projects. Therefore, the VirtualBox cannot forward the HTTP request from your host machine for all of virtual machine. You can only run one machine (so, one project) each time.
To run multiple projects with Homestead, you can do as follow:
Clone Homestead git clone https://github.com/laravel/homestead.git Homestead
Inside the Homestead folder, run bash init.sh
Edit the folders property of ~/.homestead/Homestead.yaml to share your code of both projects with VM:
folders:
- map: ~/pj1
to: /path/to/project1
- map: ~/pj2
to: /path/to/project2
Edit the sites property of ~/.homestead/Homestead.yaml to make Nginx enable the domain of both site:
sites:
- map: project1.local
to: /home/vagrant/pj1/public
- map: project2.local
to: /home/vagrant/pj2/public
Edit your hosts file to forward these domain fo localhost
127.0.0.1 project1.local
127.0.0.1 project2.local
Run vagrant up at the folder that you cloned the Homestead code inside it (which contains the init.sh file).
Now, you can run as many project as you want with just one Homestead virtual machine.
There are some important steps missing in the accepted answer although it helped me lot. I have added those necessary steps. Thanks #Hieu Le for answer.
I assume you have correctly installed your fist site as by the instructions of Laravel docs. Now you have another laravel site which you want to shift on vagrant. Follow the following steps.
cd into the directory of new Laravel project which you want to add. I assume you have all laravel files in it and its working using MAMP or any non-vagrant solution.
run vagrant init laravel/homestead. This command will add the necessary VagrantFile in this new project.
open the directory of your first original project file and open its
Homestead.yaml file in editor.
Now follow the steps defined by #Hieu Le in accepted answer to
modify .yaml file
folders:
- map: ~/pj1
to: /path/to/project1
- map: ~/pj2
to: /path/to/project2
sites:
- map: project1.local
to: /home/vagrant/pj1/public
- map: project2.local
to: /home/vagrant/pj2/public
Edit your hosts file to forward these domain fo localhost
127.0.0.1 project1.local
127.0.0.1 project2.local
On terminal cd into your first original original project
directory.
Run command vagrant reload --provision. This will reload the
vagrant machine so that the changes which we made in .yaml file come in effect. You database of original project will remain intact.
Run vagrant ssh
Run ls and make sure you can see the folder of your new project. If its there
you have configured your new site correctly.
Hit the url of new site with addition of http:// and your are
DONE.
Like how here says, you can install Homestead directly into your project, require it using this composer require laravel/homestead --dev at root directory of each project you have. Now by make command you can generate Vagrantfile and Homestead.yaml file into your project's root directory.
Mac/Linux:
php vendor/bin/homestead make
Windows:
vendor\bin\homestead make
On each project root you will have a Homestead.yaml file to edit:
Project-A
ip: "192.168.10.10"
...
folders:
- map: "~/Code/projecta"
to: "/home/vagrant/projecta"
sites:
- map: project.a
to: "/home/vagrant/projecta/public"
Project-B
ip: "192.168.10.10"
...
folders:
- map: "~/Code/projectb"
to: "/home/vagrant/projectb"
sites:
- map: project.b
to: "/home/vagrant/projectb/public"
Add this to /etc/hosts:
192.168.10.10 project.a
192.168.10.10 project.b
Then you have to cd to each project's root and vagrant up.
Now if you vagrant ssh from each project, you will have that project in your VM environment.
there is a short cut command to proxy the sites you want to add..
without having to messed up your Homestead.yaml file and provision your vagrant box all over again...
This applies to BOTH GLOBAL AND PER PROJECT INSTALLATION
Just Make sure if you are adding another project...
You add it the (whole project) on your Shared Folder
Declared in your Homestead.yaml
Assuming your shared folder is
C:/Users/MYACCOUNT/Codes
Add another project in that Folder
laravel new homestead.app
Then
Assuming your are ssh in your Homestead
Type
a.) if your using nginx
serve homestead.app /home/Vagrant/Code/homestead/public
b.) if your using hhvm
serve-hhvm homestead.app /home/Vagrant/Code/homestead/public
Just change your domain name and path to public folder of your project
Then Edit your etc/hosts file as Administrator
What ever ip address you define in your Homestead.yaml
usually the default is 192.168.10.10
Use it instead of 127.0.0.1
Why? because if you use 127.0.0.1 your url will look like
homestead.app:8000
If you Use the IP address in the Homestead.yaml
192.168.10.10 homestead.app
you can access your site without port 8000
and just use homestead.app
This Solution is Much Better than Provision... And is Faster...
This is what i Do
Step 1: First you just map your other project and database in the homestead.yaml file enter image description here.
Step 2: Create the database you mentioned in the existing workbench connection enter image description here.
Step 3: Mention that database in your project .env file. enter image description here
Step 4: Add another path in your drivers/etc/hosts file and run homestead reload --provision. Your problem is solved. :)enter image description here.
How can i add a new site without running ?
homestead destroy
homestead up
Im using Laravel Homestead version 2.0.13
When i add a new site, i add the following to my homestead.yaml file
folders:
- map: ~/web/code
to: /code/websites
sites:
- map: laravel5.dev
to: /code/websites/laravel5/public
- map: anothersite.dev
to: /code/websites/anotherproject/public
I've added to my hosts file :
192.168.10.10 laravel5.dev
192.168.10.10 anothersite.dev
I then proceed to check the status of my machine using:
vagrant global-status
90be623 default virtualbox running /Users/user/.composer/vendor/laravel/homestead
I run vagrant provision 90be623 to provision my new site but sadly anothersite.dev still points to laravel5.dev
The only way i solve this is running homestead destroy and then homestead up.
But this destroys my database.
When i run vagrant provision 90be623 i notice alot of times the appearance of:
==> default: stdin: is not a tty (In red)
Versus when i do homestead destroy and homestead up where everything is in green..
Thank you for reading this and helping :)
Ok, so i've found that if i want to add a new site, all i got to do is
homestead halt
homestead edit
Homestead edit will open the homestead.yaml file
Here i add the mapping for the site
Edit the hosts file /etc/hosts and add url i want for example app.dev and save
homestead up --provision
It worked like a charm without me having to destroy my machine or databases.
In Laravel 5.2, there is no need of halting and then starting.
After adding the new site inside the ".yaml" and "hosts" files, you only need to perform one command in the terminal, from within the Homestead directory:
vagrant reload --provision
See here: https://laravel.com/docs/5.2/homestead#adding-additional-sites
I have now a new MabBook with 10.8 and I am trying to set up all the Apache & MySQL etc.
Apache successfully runs including vhosts.
In my /etc/hosts I have all the local hosts set including 127.0.0.1 localhost etc.
When I try to coennct to one of my local vhost, e.g. alpensonne, I can connect to MySQL without any troubles. If I try to have the MySQL host to be localhost, I get the follwoing error:
Database connection error (2): Could not connect to MySQL.
I know the error (2) means username or password incorrect, but they are fine.
If I connect with
mysql -u root
SELECT password,host FROM user where user='root';
Also I have just done to be double sure:
SET PASSWORD FOR 'root'#'localhost' = '';
Is it possible that localhost uses a different MySQL socket? Maybe it then tries to connect to a different MySQL?
Ok I found the mistake...
The problam was, that I have forgot to change the php.ini file to point to the mysql.default_socket...
My default_socket was set to /var/mysql/mysql.sock
so I just created a symlink to /tmp/mysql.sock and now all works fine! :)
#shadyyx
Thanks for the proposal of MAMP, I know this of course but not such a big fan of MAMP, XAMPP, LAMP etc. for windows user its good so you can use apache instead of IIS, but for Linux, Unix and also Mac user you can use the build in apache server... so no need to use an other third party software. also you are depended then on the php version etc. I like it more to be able to configure it myself.
To elaborate on the answer from #schurtertom...
The problem is that mysql.default_socket setting in PHP.INI defaults to a different location than where MySQL actually puts that file.
Instead of editing the config files, create an alias in the location that PHP is looking that connects to the real mysql.sock by running these two commands (no restart needed):
mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
With MySQL from MacPorts it works with this path:
mysql.default_socket = /opt/local/var/run/mysql56/mysqld.sock
In case MySQL still does not connected, run:
brew switch openssl 1.0.2s