Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

前端 未结 21 2143
春和景丽
春和景丽 2020-11-22 06:31

Warning: mysqli_connect(): (HY000/1045): Access denied for user \'username\'@\'localhost\' (using password: YES) in C:\\Users\\xampp\\htdocs\\PHP_Login_Script

相关标签:
21条回答
  • 2020-11-22 07:10

    mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

    use DB_HOST instead of DB_SERVER

    0 讨论(0)
  • 2020-11-22 07:12

    This is your code:

    <?php
    
        define("DB_HOST", "localhost");
        define("DB_USER", "root");
        define("DB_PASSWORD", "");
        define("DB_DATABASE", "databasename");
    
        $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    
    ?>
    

    The only error that causes this message is that:

    1. you're defining a DB_USER but you're calling after as DB_USERNAME.

    Please be more careful next time.

    It is better for an entry-level programmer that wants to start coding in PHP not to use what he or she does not know very well.

    ONLY as advice, please try to use (for the first time) code more ubiquitous.

    ex: do not use the define() statement, try to use variables declaration as $db_user = 'root';

    Have a nice experience :)

    0 讨论(0)
  • 2020-11-22 07:12

    Try changing:

    define("DB_SERVER", "localhost");
    

    to this:

    define("DB_SERVER", "127.0.0.1");
    
    0 讨论(0)
  • 2020-11-22 07:14

    Same problem occurd with me, with AWS RDS MySQL. Just now, answered similar question here. Looked various sources, but as of this thread, almost all lack of answer. While this thread helped me, tweak in mind to update hostnames at server. Access your SSH and follow the steps:

    cd /etc/
    sudo nano hosts
    

    Now, Appending here your hostnames: For example:

    127.0.0.1 localhost
    127.0.0.1 subdomain.domain.com [if cname redirected to endpoints]
    127.0.0.1 xxxxxxxx.xxxx.xxxxx.rds.amazonaws.com [Endpoints]
    

    and now, configuring config/database.php as follows:

    $active_group = 'default';
    $query_builder = TRUE;
    
    $db['default'] = array(
        'dsn'   => '',
        'hostname' => '35.150.12.345',
        'username' => 'user-name-here',
        'password' => 'password-here',
        'database' => 'database-name-here',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
    

    where 35.150.12.345 is your IPv4 Public IP, located at ec2-dashboard > Network Interfaces > Description : RDSNetworkInterface >> IPv4 Public IP('35.150.12.345') there.

    Note: Please note that only IPV4 will work at 'hostname' option. hostname, cname, domains will output database connection error.

    0 讨论(0)
  • 2020-11-22 07:16

    Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.

    Note: 12345 mentioned below is your plain password

    GRANT ALL PRIVILEGES ON dbname.* TO 'yourusername'@'%' IDENTIFIED BY '12345';
    
    0 讨论(0)
  • 2020-11-22 07:17

    That combination of username, host, and password is not allowed to connect to the server. Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server.

    0 讨论(0)
提交回复
热议问题