我是网络开发的新手。我在DigitalOcean上买了一台服务器,创建了一个Droplet并安装了LAMP。我有一个简单的PHP脚本连接到MySQL。这是我的PHP脚本(conn.php)
<?php
$servername = "xxx.xx.xx.xxx";
$username = "root";
$password = "my_password";
try {
$conn = new PDO("mysql:host=$servername;dbname=mysql", $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();
}
?>
现在,当我尝试在我的本地“xxx.xx.xx.xxx/conn.php”上运行时,它正在打印
Connection failed: SQLSTATE[HY000] [2002] Connection refused