用户 ''@'localhost' 拒绝访问数据库

Posted

技术标签:

【中文标题】用户 \'\'@\'localhost\' 拒绝访问数据库【英文标题】:access denied for user ''@'localhost' to database用户 ''@'localhost' 拒绝访问数据库 【发布时间】:2014-06-14 03:46:03 【问题描述】:

我有 Database1 和 Database2

我的主要项目Database1 无法运行,但是当我使用Database2 时,它可以运行。但是,我无法修复 Database1 的错误。我很好奇为什么 Database1 不起作用。

这是我的代码。

<?php

            require_once("models/config.php");
            if (!securePage($_SERVER['PHP_SELF']))die();

            require_once("models/header.php");

            if(isset($_POST["product_name"]) && isset($_POST["product_description"]) && isset($_POST["product_price"]))
            
                $sql = "INSERT INTO preapprove (name,descr,price) VALUES ('".$_POST["product_name"]."', '".$_POST["product_description"]."', '".$_POST["product_price"]."')";
                $sql_query = mysql_db_query($db_name, $sql) or die(mysql_error());
                mysql_close();
            


            echo "
            <div class='container'>
                <div class='col-md-3'>";

            include("left-nav.php");

            echo "
                </div>

                <div id='main'>";

            echo resultBlock($errors,$successes);

            echo "
                <div id='regbox' class='col-md-9'>
                    <form name='addProducts' action='".$_SERVER['PHP_SELF']."' method='post'>
                    <p>
                        <div class='row'>
                            <div class='col-md-2'>
                                <label>Name</label>
                            </div>
                            <div class='col-md-6'>
                                <input type='text' name='product_name' class='form-control' />
                            </div>
                        </div>
                    </p>
                    <p>
                        <div class='row'>
                            <div class='col-md-2'>
                                <label>Description</label>
                            </div>
                            <div class='col-md-6'>
                                <textarea class='form-control' rows='3' name='product_description'></textarea>
                            </div>
                        </div>
                    </p>
                    <p>
                        <div class='row'>
                            <div class='col-md-2'>
                                <label>Price</label>
                            </div>
                            <div class='col-md-6'>
                                <input type='text' name='product_price' class='form-control' />
                            </div>
                        </div>
                    </p>
                    <p>
                        <div class='row'>
                            <div class='col-md-2 col-md-offset'>
                            </div>
                            <div class='col-md-4'>
                                <button type='submit' class='submit btn btn-default'>Submit</button>
                            </div>
                        </div>
                    </p>
                    </form>
                </div>
                </div>
            </div>
            <div id='bottom'></div>
            </body>
            </html>";

            ?>

models/db-settings.php

<?php
        /*
        UserCake Version: 2.0.2
        http://usercake.com
        */

        //Database Information
        $db_host = "localhost"; //Host address (most likely localhost)
        $db_name = "bookstore"; //Name of Database
        $db_user = "root"; //Name of database user
        $db_pass = "1234"; //Password for database user
        $db_table_prefix = "uc_";

        GLOBAL $errors;
        GLOBAL $successes;

        $errors = array();
        $successes = array();

        /* Create a new mysqli object with database connection parameters */
        $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
        GLOBAL $mysqli;

        if(mysqli_connect_errno()) 
            echo "Connection Failed: " . mysqli_connect_errno();
            exit();
        

        //Direct to install directory, if it exists
        if(is_dir("install/"))
        
            header("Location: install/");
            die();

        

        ?>

models/config.php

<?php
        /*
        UserCake Version: 2.0.2
        http://usercake.com
        */
        require_once("db-settings.php"); //Require DB connection

        //Retrieve settings
        $stmt = $mysqli->prepare("SELECT id, name, value
            FROM ".$db_table_prefix."configuration");   
        $stmt->execute();
        $stmt->bind_result($id, $name, $value);

        while ($stmt->fetch())
            $settings[$name] = array('id' => $id, 'name' => $name, 'value' => $value);
        
        $stmt->close();

        //Set Settings
        $emailActivation = $settings['activation']['value'];
        $mail_templates_dir = "models/mail-templates/";
        $websiteName = $settings['website_name']['value'];
        $websiteUrl = $settings['website_url']['value'];
        $emailAddress = $settings['email']['value'];
        $resend_activation_threshold = $settings['resend_activation_threshold']['value'];
        $emailDate = date('dmy');
        $language = $settings['language']['value'];
        $template = $settings['template']['value'];

        $master_account = -1;

        $default_hooks = array("#WEBSITENAME#","#WEBSITEURL#","#DATE#");
        $default_replace = array($websiteName,$websiteUrl,$emailDate);

        if (!file_exists($language)) 
            $language = "models/languages/en.php";
        

        if(!isset($language)) $language = "models/languages/en.php";

        //Pages to require
        require_once($language);
        require_once("class.mail.php");
        require_once("class.user.php");
        require_once("class.newuser.php");
        require_once("funcs.php");

        session_start();

        //Global User Object Var
        //loggedInUser can be used globally if constructed
        if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"]))
        
            $loggedInUser = $_SESSION["userCakeUser"];
        

        ?>

【问题讨论】:

您必须先连接到您的数据库。您发布的代码不相关。你在哪里连接到数据库? mysql_*已弃用 请使用PDOMySQLi 强制性评论。请不要对新代码使用 mysql 函数,它现在已被弃用,建议使用 mysqli 或 PDO 通信形式。 抱歉,Adrian,我已经连接到 models/config.php 中的数据库 然后贴出相关行。 显示您的mysql_connect 【参考方案1】:

此错误来自 Mysql 服务器。

access denied for user ''@'localhost' to database

您需要向用户授予权限。

access denied for user @ 'localhost' to database ''

请运行此 sql 来检查您的数据库用户: 我的例子:

mysql> SELECT Host,User FROM mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| %         | xxx_user  |
| %         | dda_user2 |
| 127.0.0.1 | root             |
| localhost | dddd_user  |
| localhost | asd_user2 |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

【讨论】:

对于一个空用户,当然;) 我认为 root 拥有做任何事情所需的所有权利,不是吗? (Mysql localhost != 127.0.0.1) ***.com/questions/19712307/mysql-localhost-127-0-0-1

以上是关于用户 ''@'localhost' 拒绝访问数据库的主要内容,如果未能解决你的问题,请参考以下文章

用户 ''@'localhost' 拒绝访问数据库

phpMyAdmin - #1044 - 用户 ''@'localhost' 拒绝访问数据库

错误 1044 拒绝用户 ''@'localhost' 访问数据库

用户'root'@'localhost'的访问被拒绝)

#1044 - 用户 'someusr'@'localhost' 拒绝访问数据库 'somedb'

#1044 - 用户 'someuser'@'localhost' 拒绝访问数据库 'somedb'