无法使用php连接到远程数据库

时间:2017-02-05 08:30:58

标签: php mysql laravel laravel-5

我在Windows中安装了Xampp,我正在使用Laravel 5.3创建一个应用程序。我试图在本地网络上的另一台服务器上执行查询但是当我尝试这样做时,MySql服务器验证我的本地服务器上的用户是(username: "root" && password:""),而远程服务器有{{1} })我不知道为什么。这是我在config / database.php下的laravel连接

username: "root" && password:"root"

我如何使用连接

'smsgateway' => [
                'driver'    => 'mysql',
                'host'      => '**.**.**.**',
                'database'  => 'database',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
    ],

我尝试使用本机PHP代码进行连接,但我遇到同样的问题是我的代码

  $smsgateway =  \DB::connection('smsgateway');
        // dd($smsgateway);
        $smsgateway->statement($sql);

它给了我

  

连接失败:SQLSTATE [HY000] [1045]拒绝用户访问   '根' @' myIPAddress' (使用密码:是)

3 个答案:

答案 0 :(得分:2)

  1. 您需要授予从本地
  2. 访问数据库的权限
  3. 使用这些命令,然后在此处恢复,帮助网址 grant remote access of MySQL database from any IP address

答案 1 :(得分:0)

就我自己而言,由于有效的安全问题,我选择开发一个API来连接远程mysql数据库。我是使用php的CURL库完成的。在外域(您要连接的域)上,我对数据库域(拥有数据库的域)发了curl个帖子。

从这里开始它非常简单,因为您只需使用本地数据库上的预准备语句保存$_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'];`
?>

答案 2 :(得分:-1)

不要在密码中使用root。 XAMPP设置中的密码字段应为空白。