简体   繁体   English

用户“ @'localhost”的访问被拒绝(使用密码:NO)

[英]Access denied for user ''@'localhost' (using password: NO)

I have Ubuntu 14.04 LTS. 我有Ubuntu 14.04 LTS。 I set up apache, phpmyadmin and mysql. 我设置了Apache,phpmyadmin和mysql。 I can access databases through localhost/phpmyadmin using my set password and root as username. 我可以使用设置的密码和root作为用户名通过localhost / phpmyadmin访问数据库。 I have put a link inside /etc/apache2/sites-enabled that links to the original .conf file of the website located in /etc/apache2/sites-available. 我在/ etc / apache2 / sites-enabled中放置了一个链接,该链接指向位于/ etc / apache2 / sites-available中的网站的原始.conf文件。 And it has these lines ( mypage.conf 它具有以下行( mypage.conf

php_value mysql.default.user    root
php_value mysql.default.password    mypass
php_value mysql.default.host    localhost

When I try to connect to database using this in my index.php it works prettily: 当我尝试使用index.php中的此数据库连接数据库时,它可以正常工作:

mysql_connect('localhost', 'root', 'mypass') or die(mysql_error());

But when I try to connect to database using this below: 但是当我尝试使用下面的方法连接到数据库时:

mysql_connect(ini_get("mysql.default.host"), ini_get("mysql.default.user"), ini_get("mysql.default.password")) or die(mysql_error());

In return it posts this error message to the page and doesn't execute the rest of the code: 作为回报,它将此错误消息发布到页面,并且不执行其余代码:

Access denied for user ''@'localhost' (using password: NO)

As I can understand from this, what it gets from default values are ''. 据我了解,默认值是''。 So I must be doing something wrong about the .conf file. 因此,我必须对.conf文件做错什么。 Can you help me about it? 你能帮我吗?

As stated by @Vanitas, I changed the connection style to PDO instead of mysql. 如@Vanitas所述,我将连接样式更改为PDO而不是mysql。 Here's my 这是我的

connection.php connection.php

<?php
    DEFINE('DB_HOST', 'localhost');
    DEFINE('DB_NAME', 'mydb');
    DEFINE('DB_USER', 'usr');
    DEFINE('DB_PASS', 'pss');

    try{
        $db = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASS);
    }catch(PDOException $exeption){
        echo $exeption->getMessage(). '<br/>';
        die();
    }
?>

And I retrive it by using 我通过使用它来检索

require_once('../../0n/connection.php');

It's outside of the web directory. 它在Web目录之外。 So, it's safe. 因此,这很安全。 I suppose. 我想。 And it's working prettily. 它正在正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户&#39;XXX&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:否) - Access denied for user 'XXX'@'localhost' (using password: NO) 用户&#39;linuxuser&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:否) - Access Denied for user 'linuxuser'@'localhost' (using password: no) 用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:NO) - Access denied for user 'root'@'localhost' (using password: NO) 用户&#39;consult&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:否) - Access denied for user 'consult'@'localhost' (using password: NO) 用户&#39;@&#39;localhost&#39;的Webmatrix访问被拒绝(使用密码:否) - Webmatrix Access denied for user ''@'localhost' (using password: NO) 用户 &#39;apache&#39;@&#39;localhost&#39; 访问被拒绝(使用密码:NO) - Access denied for user 'apache'@'localhost' (using password: NO) 用户&#39;root&#39;@&#39;localhost&#39;访问被拒绝(使用密码:NO); - Access denied for user 'root'@'localhost' (using password: NO); 用户“ @&#39;localhost”的访问被拒绝(使用密码:NO)phpmyadmin - Access denied for user ''@'localhost' (using password: NO) phpmyadmin 用户&#39;name&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:NO) - Access denied for user 'name'@'localhost' (using password: NO) 用户 'user'@'localhost' 的访问被拒绝(使用密码:YES)错误 - Access denied for user 'user'@'localhost' (using password: YES) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM