我刚刚将我的网站上传到生产服务器上,但出现错误:
Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in file.php on line 106
Warning: mysql_real_escape_string(): A link to the server could not be established in file.php on line 106
函数的代码是
include('./../inc/conn.php');
if(isset($_GET['query']))$q = clean($_GET['query']);
function clean($var){
return(mysql_real_escape_string($var));
}
inc/conn.php 的代码:
try {
$dns = 'mysql:host=localhost;dbname=mydatabase';
$user = 'root';
$pw = 'rootpw';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
$db = new PDO( $dns, $user, $pw, $options );
} catch ( Exception $e ) {
echo "Connection error : ", $e->getMessage();
die();
}
我真的不知道发生了什么,因为我在我的本地开发 ubuntu 服务器上没有问题。 mysql、apache和php版本是一样的。唯一的事情是我在 apache prod 服务器上使用虚拟主机。不知道发生了什么……我在 apache 或 php 配置之一中遗漏了什么吗?
编辑 这是我文件夹的权限:
sudo ls -l /home/user/public/domain.com/www/
total 28
drwxrwxr-x 13 user www-data 4096 Aug 22 12:30 adodb5
drwxrwxr-x 2 user www-data 4096 Aug 22 12:30 ajax
drwxrwxr-x 2 user www-data 4096 Aug 22 12:31 css
drwxrwxr-x 9 user www-data 4096 Aug 22 12:33 gfx
drwxrwxr-x 2 user www-data 4096 Aug 22 12:33 inc
drwxrwxr-x 2 user www-data 4096 Aug 22 12:34 js
我的 apache 虚拟主机配置
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin contact@domain.com
ServerName www.domain.com
ServerAlias domain.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/user/public/domain.com/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/user/public/domain.com/www>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
# Log file locations
LogLevel warn
ErrorLog /home/user/public/domain.com/log/error.log
CustomLog /home/user/public/domain.com/log/access.log combined
</VirtualHost>
编辑2
好的,问题是我在 mysql 服务器上没有任何 www-data 用户。所以我只是在 mysql 中添加了没有密码且没有特权的用户 www-data,这工作正常。我将在未来尝试使用许多提到的 PDO 报价。 感谢所有试图帮助我的人。
最佳答案
要么使用 PDO,要么使用 mysql 扩展,不要同时使用两者。 mysql_real_escape_string
是mysql扩展的一个函数。它需要连接到数据库才能运行。调用它时,如果您之前没有使用 mysql_connect
建立连接,它会尝试建立连接,并猜测所需的登录凭据。在您的本地计算机上,您显然没有密码保护,并且 MySQL 用户的帐户名与运行 Web 服务器的名称相同,所以它恰好可以正常工作。在生产系统上,凭据不同,无法建立连接。
停止将 mysql_real_escape_string
与 PDO 一起使用。要么使用 PDO 的字符串引用函数,要么更好,use prepared and parameterized queries and bind
your values .
关于PHP mysql_real_escape_string() : Access denied for user 'www-data' @'localhost' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378976/