我有一个用laravel开发的应用程序,它与2个数据库进行通信,一次是mysql,它在linux共享主机中。另一个是sql,位于远程服务器中。
在localhost中运行良好,我使用sqlsrv driver
与sql数据库进行通信,但是当我将项目传递给共享主机时,它无法与数据库连接,只需使用mysql数据库。
我具体要问托管服务提供商?
这是我的连接参数:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'dbname'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'prefix' => '',
],
'sql' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'remote_ip'),
'database' => env('DB_DATABASE', 'dbname'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'prefix' => '',
],
答案 0 :(得分:0)
首先你应该在服务器上安装php_sql_server驱动程序(作为php_extension)。
了解更多信息请阅读:https://docs.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server和(下载文件)https://www.microsoft.com/en-us/download/details.aspx?id=20098
第二,您应该在env中将连接添加为单独的连接,例如:
[HttpPut]
[Route("editpaymenttype/{id}")]
public async Task<RequestResult> EditPaymentType(int id, [FromBody]PaymentTypeVM model)
{
if (!ModelState.IsValid)
{
return RequestResult.FailureResult(Messages.InvalidEntry);
}
var pType = await db.PaymentTypes.FindAsync(id);
if (pType == null) return RequestResult.FailureResult(Messages.NotFound);
pType.Name = model.Name ?? pType.Name;
await db.SaveChangesAsync();
return RequestResult.SuccessResult($"Payment type {Messages.EditSuccessful}");
}
adn在你的config / database.php上用作:
DB_HOST=127.0.0.1
DB_PORT=null
DB_DATABASE=GRH
DB_USERNAME=sa
DB_PASSWORD=sfsd42563
DB_HOST2=remote_ip
DB_DATABASE2=db_name
DB_USERNAME2=username
DB_PASSWORD2=password