欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

post get guzzle curl post 提交 数据库连接错误 还是连接是原来的 发送 post get guzzle 的站的数据库 不是目标站的数据库 两个数据库 有大用

 

也许是因为共用同一个服务器的问题吧 它们会有冲突
当然了 .env里面的其它的变量(比如 
JWT_SECRET)也可以有冲突,这个要注意


The way to fix this as I am running both Laravel projects on the same server, is to change the environment variable names in the .env file.

DB_DATABASE=XXXX

becomes

XXX_DB_DATABASE=XXXX

This needs to be done on one of the Laravel setups then it all works properly.


Laravel calling Lumen - wrong DB connection?

PUBLISHED 1 YEAR AGO BY JDFORSYTHE

I have an Angular/Laravel app that is calling a Lumen API with Guzzle. This is in a local environment, but everything is on separate vhosts.

Angular front-end is at http://site1.dev Laravel back-end is at http://api.site1.dev Lumen API is at http://api.site2.dev

I can call Laravel or Lumen through Postman and everything is fine. I can call the Laravel back-end with Angular and everything is fine.

The problem is that I need to cause some side-effects on site2 by calling the Lumen API from the Laravel controller. I've set up a Guzzle facade in Laravel and a service provider. This is all working okay.

So when I call a test method in the Laravel controller to access the Lumen API, it sends the request, but gives an error:

ServerException in RequestException.php line 107:
Server error: GET http://api.site2.dev/testresource resulted in a 500 Internal Server Error
response:
{"error":{"message":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'site1_db.clients' doesn't exist

What? How does site2 know about the site1 DB config? site1_db is defined in the Laravel .env file, the Lumen .env file has site2_db configured, which does have the clients table. How is this configuration seeping through to a different host?

jdforsythe

The lumen.log shows this:

Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'site1_db.clients' doesn't exist (SQL: select * from `clients` where `api_token` = 123456789 limit 1)' in E:\dev\site_2\illuminate\database\Connection.php:669

This query would work (and presumably the API would work) if it connected to the correct database.

I originally had the db host as 127.0.0.1 in both sites, but I changed them both to their respective api.site.dev URIs to no avail.

Is this maybe something to do with the Apache/MySQL/PHP setup on WAMPServer?

 
jdforsythe

This was definitely an issue with having both sites on the same Apache instance on Windows. I pointed site2_db to a different MySQL server and the problem continued. I moved site2 to a linux server and everything works fine.

 
stocktain

For anyone else running into this problem on a Windows local/development environment you can fix it by changing the .env file variable names for the database connections in the Laravel set up, and matching these up in the database.php config file. For example:

Env file

DB_HOST_SITE1=localhost
DB_PORT_SITE1=3306
DB_DATABASE_SITE1=database
DB_USERNAME_SITE1=username 
DB_PASSWORD_SITE1=password
Database.php
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST_SITE1', 'localhost'),
            'database'  => env('DB_DATABASE_SITE1', 'forge'),
            'username'  => env('DB_USERNAME_SITE1', 'forge'),
            'password'  => env('DB_PASSWORD_SITE1', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ]
 
stocktain

For anyone else running into this problem on a Windows local/development environment you can fix it by changing the .env file variable names for the database connections in the Laravel set up, and matching these up in the database.php config file. For example:

Env file

DB_HOST_SITE1=localhost
DB_PORT_SITE1=3306
DB_DATABASE_SITE1=database
DB_USERNAME_SITE1=username 
DB_PASSWORD_SITE1=password
Database.php
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST_SITE1', 'localhost'),
            'database'  => env('DB_DATABASE_SITE1', 'forge'),
            'username'  => env('DB_USERNAME_SITE1', 'forge'),
            'password'  => env('DB_PASSWORD_SITE1', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ]

来自 https://laracasts.com/discuss/channels/laravel/laravel-calling-lumen-wrong-db-connection




I am trying to make a request from one Laravel project to another. The issue that I am getting is that the second Laravel is using the first Laravel Database Connection. So it is complaining that a table does not exist.

Here is the code that I am using.

$data = ['test' => 'foobar'];
$client = new \GuzzleHttp\Client();
$url = getenv('API_BASE') . 'stock-list';

$res = $client->request('POST', $url, [
    'headers' => [
        'X-Public' => getenv('API_PUBLIC'),
        'X-Hash' => ApiService::Encrypt(getenv('API_PRIVATE'), json_encode($data)),
    ],
    'json' => $data,
    'http_errors' => false,
]);

echo "<pre>" . print_r($res->getBody()->getContents(), true) . "</pre>";

Has anyone ever come across something like this?

 


   
Worth mentioning that getContents will only return the remainder of the stream, if you want to safely return the whole body, you should cast it to string. – mark.sagikazar Feb 4 at 21:10

You can change the default db connection runtime like this:

So let's say you have

  • 1 security db with credentials that is the default db design time.
  • 1 or more databases containing data for 1 or more users.

You log in using the security db and based on the user change the default db to the data db.

    config(['database.connections.data' => array(
                'driver'    => 'sqlsrv',
                'host' => $connection['Database_Server'],
                'database' => $connection['Database_Name'],
                'username' => $connection['Database_User'],
                'password' => $connection['Database_Password']

            )]);

            DB::setDefaultConnection('data');

If you don't need such flexibility you can define the connection per model:

class A extends Model {
    protected $connection = 'security';
    protected $table = 'A';
}
shareimprove this answer
 
   
The issue I was having was the the .env variables were being overridden when calling another Laravel project.– Sam_Benne Feb 3 at 15:44
   
Ok, so your problem is solved now? Otherwise not using the .env for connection but the model instead will work! – Thomas Moors Feb 3 at 15:46

The way to fix this as I am running both Laravel projects on the same server, is to change the environment variable names in the .env file.

DB_DATABASE=XXXX

becomes

XXX_DB_DATABASE=XXXX

This needs to be done on one of the Laravel setups then it all works properly.

shareimprove this answer
 

来自 https://stackoverflow.com/questions/42004472/laravel-guzzle-request-gets-wrong-db-connection


普通分类: