微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP mysql_real_escape_string():拒绝访问用户’www-data’@’localhost’

我刚刚在生产服务器上上传了我的网站,我收到错误

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用户.所以我刚刚添加用户www-data,没有密码,也没有MysqL的权限,这个工作正常.我将在未来尝试使用PDO引用,正如许多人提到的那样.
谢谢大家试图帮助我.

解决方法:

Note that this answer is terrible and opens you up to security vulnerabilities. It is the wrong solution. Do not do this. See further down for the actual solution to the problem. The fact that this answer got accepted just means that two people didn’t kNow what they were doing.

这是因为你的MysqL数据库中没有www数据!

您可以通过PHPmyadmin添加www-data用户并为该用户提供无权限,它应该可以正常工作

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐