我在 Windows 中安装了 Xampp,并且正在使用 Laravel 5.3 创建一个应用程序。我正在尝试在本地网络上的另一台服务器上执行查询,但是当我尝试执行此操作时,MySql 服务器使用 (username: "root"&& password:""
)而远程服务器有(用户名:“root”&&密码:“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);
我尝试使用 native 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();
}
它给了我
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'@'myIPAddress' (using password: YES)
最佳答案
- 您需要授予本地访问数据库的权限
- 使用这些命令然后恢复,帮助网址在这里 grant remote access of MySQL database from any IP address
关于php - 无法使用php连接到远程数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135331/