Laravel Homestead Vagrant Box Database Problems

后端 未结 7 2241
轮回少年
轮回少年 2020-12-30 17:11

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.

If my database.php se

相关标签:
7条回答
  • 2020-12-30 17:28

    You have to execute the migration command from inside the VM. First, you log into the VM:

    ssh vagrant@127.0.0.1 -p 2222
    

    After that, you just cd into your project's folder and run the migration

    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.

    0 讨论(0)
  • 2020-12-30 17:28

    After using homestead ssh and paste your ssh vagrant@127.0.0.1 -p 2222 our php artisan migrate working properly

    0 讨论(0)
  • 2020-12-30 17:38

    Quick fix:

    1. ssh into your homestead virtual machine (homestead ssh)
    2. navigate to the project directory
    3. run your command from there (php artisan migrate)
    0 讨论(0)
  • 2020-12-30 17:39

    This is what I answered in the Laracasts forums, in case it helps:

    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.

    Unfortunately that is why you can't use the same database stanza for both.

    Two ideas come to mind:

    1. 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.

    2. 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.

    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.

    0 讨论(0)
  • 2020-12-30 17:39

    Ben Swinburne's answer worked for me.

    I added the following to my Homestead.yaml file

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-30 17:41

    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

    You can change this by editing /etc/mysql/my.cnf and adding the line:

    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.

    0 讨论(0)
提交回复
热议问题