PDO从localhost连接到Web sql? [英] PDO to connect to web sql from localhost?
问题描述
我想做一些测试,我使用MAMP并尝试连接到我的SQL数据库在我的服务器上。我没有使用PDO之前,我有一个很难跟踪什么错误代码意味着什么。我得到这个:
I am trying to do some testing, i am using MAMP and trying to connect to my SQL database on my server. I haven't used PDO before and am having a hard time tracking down what error codes mean what. I am getting this:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'hostname' (60)
我的连接功能:
function getConnection() {
$dbhost="hostname";
$dbuser="user";
$dbpass="password";
$dbname="somedb";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
我在尝试执行查询:
function getTypes() {
$sql = "SELECT id, name FROM type";
try {
$db = getConnection();
$stmt = $db->query($sql);
$types = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"wine": ' . json_encode($types) . '}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
echo 'getTypes';
}
编辑:我可以使用相同的凭证与mysqli。
I can connect using the same credentials with mysqli.
推荐答案
这是一个安全问题。为了避免您的数据库淹没或用垃圾填充,配置设置说只有本地ip可以访问数据库。所有共享托管做到这一点,这是一个很好的做法,如果你有一个专用的服务器。
It's a security problem. To avoid your database beeing flooded or filled with garbage a configuration setting says "only local ip can access to the database". All shared hosting do that and it is a good practice to do it if you have a dedicated server.
在/var/mysql/my.conf在linux你有类似的东西:
in /var/mysql/my.conf on linux you have got something like that :
skip-external-locking
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
这篇关于PDO从localhost连接到Web sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!