Laravel Homestead Vagrant Box 数据库问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24048461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Homestead Vagrant Box Database Problems
提问by MCG
I can't use the same database.php settings when browsing the local website in a browser (example.app:8000) and when using php artisan migrate.
在浏览器中浏览本地网站 (example.app:8000) 和使用 php artisan migrate 时,我无法使用相同的 database.php 设置。
If my database.php settings are:
如果我的 database.php 设置是:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'port' => '33060'
)
Artisan works but I get this when browsing the site:
Artisan 可以工作,但在浏览网站时我得到了这个:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111) (View: /home/vagrant/Code/playnamics/app/views/hello.blade.php)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111) (View: /home/vagrant/Code/playnamics/app/views/hello.blade.php)
If my database.php settingd are:
如果我的 database.php 设置是:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'port' => '33060'
)
Browsing the site works but artisan give this error:
浏览网站有效,但工匠给出了这个错误:
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
[PDOException] SQLSTATE[HY000] [2002] 没有那个文件或目录
Basically, I need the host set to localhost when browsing and 127.0.0.1 when using artisan. How can I simply use the same host so I don't have to keep changing the value every 2 minutes?
基本上,我需要在浏览时将主机设置为 localhost,在使用 artisan 时需要将主机设置为 127.0.0.1。我怎样才能简单地使用同一个主机,这样我就不必每 2 分钟更改一次值?
Really confused. Seemed simpler before I started using homestead & vagrant... :/
真的很迷茫。在我开始使用宅基地和流浪者之前似乎更简单......:/
回答by noeldiaz
This is what I answered in the Laracasts forums, in case it helps:
这是我在 Laracasts 论坛中的回答,以防万一:
Inside the VM the sql port is 3306. Outside of the VM the host machine just has a forward to the SQL port on the VM. That is why 33060 points to 3306.
在 VM 内部,sql 端口是 3306。在 VM 外部,主机只转发到 VM 上的 SQL 端口。这就是为什么 33060 指向 3306。
Unfortunately that is why you can't use the same database stanza for both.
不幸的是,这就是您不能对两者使用相同的数据库节的原因。
Two ideas come to mind:
想到两个想法:
Change the sql port from 33060 to be 3306 also on the host inside the homestead.rb file. I know machines get picky if you choose something under port 10000 so you might get prompted for admin credentials (if it even lets you). As long as you are not running something on that port it "should" work.
You might consider setting up two Laravel environments for when working outside of the VM and one for inside. That way, you can override the database.php settings for when running artisan commands on the VM or if running artisan on the Host. In reality, you only care about changing the port number since all other settings would be identical. You can leave everything else as it is.
在 homestead.rb 文件中的主机上也将 sql 端口从 33060 更改为 3306。我知道如果你在端口 10000 下选择某些东西,机器会变得挑剔,所以你可能会收到管理员凭据的提示(如果它允许的话)。只要您不在该端口上运行某些东西,它就“应该”工作。
您可能会考虑在 VM 外部工作时设置两个 Laravel 环境,一个在内部工作。这样,您可以在 VM 上运行 artisan 命令或在主机上运行 artisan 时覆盖 database.php 设置。实际上,您只关心更改端口号,因为所有其他设置都是相同的。您可以保留其他所有内容。
Just something to try. I just leave a SSH session open to the VM and run commands there since connecting to it is pretty fast after resuming the machine.
只是想尝试一下。我只是让 SSH 会话对 VM 保持打开状态并在那里运行命令,因为在恢复机器后连接到它的速度非常快。
回答by Gerson Azevedo
You have to execute the migration command from inside the VM. First, you log into the VM:
您必须从 VM 内部执行迁移命令。首先,您登录虚拟机:
ssh [email protected] -p 2222
After that, you just cd into your project's folder and run the migration
之后,您只需 cd 进入您的项目文件夹并运行迁移
cd Code/blog
php artisan migrate
The reason why this happens is because localhost:3306 from the perspective of your machine is one thing, from inside the VM is another.
发生这种情况的原因是因为从您的机器的角度来看 localhost:3306 是一回事,从 VM 内部的角度来看又是另一回事。
回答by Ben Swinburne
The way that I do this is set variables in the homestead.yml
file. PhpDotenv doesn't populate the values if they exist so you can set the environment variables on the Homestead VM and config
我这样做的方法是在homestead.yml
文件中设置变量。如果值存在,PhpDotenv 不会填充这些值,因此您可以在 Homestead VM 和配置上设置环境变量
# homestead.yml
variables:
- key: APP_ENV
value: local
- key: DB_HOST
value: 127.0.0.1
- key: DB_PORT
value: 3306
# .env
DB_HOST=192.168.10.10 # Homestead VM IP
DB_PORT=33060 # Port which is forwarded to 3306 on the VM
--
——
vagrant@homestead:~/Sites/example$ php artisan migrate
Nothing to migrate.
vagrant@homestead:~/Sites/example$ php artisan migrate
没什么好迁移的。
--
——
Ben in ~/Sites/example on develop$ php artisan migrate
Nothing to migrate.
Ben in ~/Sites/example on develop$ php artisan migrate
没什么好迁移的。
This obviously makes the assumption that all sites running on Homestead are using MySQL (or some database on 3306) and all databases are on the Homestead VM, as opposed to on the host, or at a 3rd party, say AWS etc.
这显然假设在 Homestead 上运行的所有站点都使用 MySQL(或 3306 上的某些数据库)并且所有数据库都在 Homestead VM 上,而不是在主机上,或者在第三方,比如 AWS 等。
At the time of writing, homestead doesn't allow you to configure per site variables.
在撰写本文时,homestead 不允许您配置每个站点的变量。
回答by ilumos
In homestead, MySQL is configured to bind to the VM's 10.x.x.x address, and does not bind to 127.0.0.1
在homestead中,mysql配置为绑定VM的10.xxx地址,不绑定127.0.0.1
You can change this by editing /etc/mysql/my.cnf
and adding the line:
您可以通过编辑/etc/mysql/my.cnf
和添加以下行来更改此设置:
bind-address = 0.0.0.0
bind-address = 0.0.0.0
This will allow you to use 127.0.0.1 in your database configuration for both web and CLI use of the database.
这将允许您在数据库配置中使用 127.0.0.1 以用于数据库的 Web 和 CLI 使用。
回答by Fairuz Sulaiman
After using homestead ssh and paste your ssh [email protected] -p 2222 our php artisan migrate working properly
使用 homestead ssh 并粘贴您的 ssh [email protected] -p 2222 后,我们的 php artisan migrate 工作正常
回答by Mahmoud Zalt
Quick fix:
快速解决:
- ssh into your homestead virtual machine (
homestead ssh
) - navigate to the project directory
- run your command from there (
php artisan migrate
)
- ssh 进入你的 homestead 虚拟机 (
homestead ssh
) - 导航到项目目录
- 从那里运行您的命令 (
php artisan migrate
)
回答by user1860465
Ben Swinburne's answer worked for me.
Ben Swinburne 的回答对我有用。
I added the following to my Homestead.yaml file
我在 Homestead.yaml 文件中添加了以下内容
variables:
- key: APP_ENV
value: local
- key: DB_HOST
value: 127.0.0.1
- key: DB_PORT
value: 3306
This updates what our code uses to connect to mysql from within the VM. Because they are set within the VM, they will take precedence over what is in the .env file. I believe values within the .env file are only set if they don't already exist within the server environment variables.
这更新了我们的代码用于从 VM 连接到 mysql 的内容。因为它们是在 VM 中设置的,所以它们将优先于 .env 文件中的内容。我相信 .env 文件中的值只有在服务器环境变量中不存在时才会设置。
Then within the .env running on the host machine, I have what is needed to connect to mysql from the outside of the VM going in.
然后在主机上运行的 .env 中,我有从 VM 外部连接到 mysql 所需的内容。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=mydb
DB_USERNAME=homestead
DB_PASSWORD=mypassword
Now, I can use artisan migrate
outside of the VM and inside the VM without any connection and port issues. Browsing the website also works as expected.
现在,我可以artisan migrate
在 VM 外部和 VM 内部使用,而不会出现任何连接和端口问题。浏览网站也按预期工作。