简体   繁体   English

如何从Laravel Homestead连接到远程数据库?

[英]How to connect to remote database from Laravel Homestead?

I'm currently working on a Laravel 5.5 project using Homestead on Windows 10. The situation is: my application needs to read data (read-only, no write) from an external database which is located on my physical machine. 我目前正在Windows 10上使用Homestead进行Laravel 5.5项目的工作。情况是:我的应用程序需要从位于物理计算机上的外部数据库读取数据(只读,不写入)。 Because in production environment, this application has its own database and also needs to fetch data from a remote database. 因为在生产环境中,此应用程序具有自己的数据库,还需要从远程数据库中获取数据。 This external database is hosted on localhost using XAMPP. 该外部数据库使用XAMPP托管在localhost上。

I've searched the Internet and haven't got any answer that applies to my scenario. 我搜索了Internet,但没有任何适用于我的情况的答案。 I'm totally confused now. 我现在很困惑。 I'm a student new to programming so can anyone please give me some tips on how should I configure to make this happen? 我是编程的新手,所以任何人都可以给我一些有关如何配置以实现此目标的提示吗? Thank you so much! 非常感谢!


[Update] About this app I'm working on: It is a web application which allows librarians and academic support staff to record details of each service, like which student came in asking about how to do referencing etc. [更新]关于我正在使用的这个应用程序:这是一个网络应用程序,它使图书管理员和学术支持人员可以记录每种服务的详细信息,例如哪个学生来询问如何进行引用等。

It will only run within my school, kind of like an internal app? 它只会在我的学校内运行,有点像内部应用程序吗? Anyway, no one can access it outside the school, and it is for staff use only, not for students. 无论如何,没有人可以在学校外访问它,它仅供教职员工使用,不适合学生使用。 This app has its own local database to store staff accounts and service details, and also as I mentioned above, it needs to fetch student information from the school database, for now during the development, I set up a database on my physical machine as the school database, and dump some dummy data in there. 这个应用程式拥有自己的本地资料库来储存员工帐户和服务详情,而且如上所述,它需要从学校资料库中获取学生信息,现在在开发过程中,我在物理机上建立了数据库学校数据库,并在其中转储一些虚拟数据。

Then I created a new model called Student using command line. 然后,我使用命令行创建了一个名为Student的新模型。 When I tried to read data from the external database using php tinker, it showed error like this: 当我尝试使用php tinker从外部数据库读取数据时,它显示如下错误:

>>> App\Modles\Student::all()
PHP Fatal error:  Class 'App/Modles/Student' not found in Psy Shell code on line 1

Below I included my Student.php, .env, and database.php files 下面,我包括了Student.php,.env和database.php文件

App/Models/Student.php App / Models / Student.php

 <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    protected $connection = 'mysql_campus';
}

.env: .env:

    ......
    //Local Database
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1

    DB_DATABASE=w2_support
    DB_USERNAME=homestead
    DB_PASSWORD=secret

    //External Database
    DB_CONNECTION=mysql_campus
    DB_EXT_HOST=10.0.2.2

    DB_EXT_DATABASE=campus
    DB_EXT_USERNAME=root
    DB_EXT_PASSWORD=

    BROADCAST_DRIVER=log
    CACHE_DRIVER=file
    SESSION_DRIVER=file
    QUEUE_DRIVER=sync
    ......

database.php: database.php:

    ......
    'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    //Local Database
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    //External Database
    'mysql_campus' => [
        'driver' => 'mysql',
        'host' => env('DB_EXT_HOST', '10.0.2.2'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_EXT_DATABASE', 'forge'),
        'username' => env('DB_EXT_USERNAME', 'forge'),
        'password' => env('DB_EXT_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
    ......

You should be able to connect to 10.0.2.2 from your homestead to access the host machine. 您应该可以从家中连接到10.0.2.2来访问主机。 This is the standard loopback address for the host machine. 这是主机的标准回送地址。 It's sort of like how you would connect to 127.0.0.1 or localhost for a local database. 有点像您如何连接到127.0.0.1或本地数据库的localhost。

My laravel version is 5.1. 我的laravel版本是5.1。

You can edit .env file and replace the DB_HOST value with the remote database host. 您可以编辑.env文件,并将DB_HOST值替换为远程数据库主机。

The other way is to edit config/database.php file. 另一种方法是编辑config / database.php文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM