[英]Cann't connect to remote database using php
我在Windows中安裝了Xampp,並且正在使用Laravel 5.3創建應用程序。 我正在嘗試在本地網絡上的另一台服務器上執行查詢,但是當我嘗試執行該操作時,MySql服務器將對本地服務器上的用戶進行身份驗證,而遠程服務器上的用戶身份是(username: "root" && password:""
)有( username: "root" && password:"root"
),我不知道為什么。 這是我在config / database.php下的laravel連接
'smsgateway' => [
'driver' => 'mysql',
'host' => '**.**.**.**',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
我如何使用連接
$smsgateway = \DB::connection('smsgateway');
// dd($smsgateway);
$smsgateway->statement($sql);
我嘗試使用本機PHP代碼進行連接,但是我遇到的問題是我的代碼
$servername = "**.**.**.**;
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
它給我
連接失敗:SQLSTATE [HY000] [1045]對用戶'root'@'myIPAddress'的訪問被拒絕(使用密碼:是)
就我自己而言,出於有效的安全考慮,我選擇開發一個API以連接到遠程mysql數據庫。 我使用php的CURL
庫做到了這一點。 在外部域(從中連接的域)上,我向數據庫域(保存數據庫的域)發送了curl
post。
從這里開始,您所做的一切非常簡單,就是使用本地數據庫上的准備好的語句保存$_POST
參數。 我只是想把這個答案放在這里。 它可能會幫助某個人
例如,您想將兩個值保存到遠程數據庫。
<?php
$array = array('key1' => $key1, 'key2' => $key2);
$url = 'www.site.com/api.php';
#do curl post to remote database
$ch = curl_init();
if ($ch !== false) {
curl_setopt($ch, CURLOPT_URL, $url); //this is the url of the domain where the database is being hosted
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); //if you are returning json
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
#on the #url script, all you do is access the values via `$_POST`. example: `$_POST['key1'];`
?>
不要使用root用戶密碼。 XAMPP設置中的“密碼”字段應為空白。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.