Access denied for 'user'@'localhost'

假装没事ソ 提交于 2021-02-05 07:56:07

问题


I'm trying to create model using phalcon. But when I type phalcon model polls I get an error as ERROR: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES).

I can get into mysql using mysql -p. So I tried to create new user and grant him all privileges, but it didn't help. I also can login into phpmyadmin using root and password. I also tried to specify the port as 3306 in config file, but still nothing.

My config file:

defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');

return new \Phalcon\Config([
    'database' => [
        'adapter' => 'Mysql',
        'host' => 'localhost',
        'port' => 3306,
        'username' => 'root',
        'password' => '$Pass1234 ',
        'dbname' => 'poll_db',
        'charset' => 'utf8',
    ],
    'application' => [
        'appDir' => APP_PATH . '/',
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir' => APP_PATH . '/models/',
        'migrationsDir' => APP_PATH . '/migrations/',
        'viewsDir' => APP_PATH . '/views/',
        'pluginsDir' => APP_PATH . '/plugins/',
        'libraryDir' => APP_PATH . '/library/',
        'cacheDir' => BASE_PATH . '/cache/',

        // This allows the baseUri to be understand project paths that are not in the root directory
        // of the webpspace.  This will break if the public/index.php entry point is moved or
        // possibly if the web server rewrite rules are changed. This can also be set to a static path.
        'baseUri' => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
    ]
]);

P.S I did all operation about granting privileges entering to mysql by mysql -u root -p

P.P.S I also did FLUSH PRIVILEGES and then restarted mysql, but nothing. Thanks for future help.

UPD: I also tried to created project in laravel, and made migration and everything works fine. I also used root + password.

SOLVED: I don't know why, but I decided to create new project, and everything worked fine even with 'root'@'localhost'. Seems like something wrong was with first project. Even so, thank for all people who participated in helping.

P.S I marked EternHour's answer as solution just for people who might get same error in future.


回答1:


I don't think this is a port issue, the request is reaching it's destination. Using root@localhost will work when logging in through the command line (mysql -u root -p) but you don't want to use it for connecting with your code. Keep in mind that when establishing your connection, you need to use host=localhost or host=127.0.0.1 explicitly. If you use the IP address (even on the same server), you will get access denied.

[user@localhost ~]# mysql --host=127.0.0.1 --protocol=TCP -u root -p
Enter password:
mysql>
[user@localhost ~]# mysql --host=192.168.1.10 --protocol=TCP -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'hostname' (using password: YES)

Here are the steps I'd recommend taking:

  1. Create a dedicated user that you can use for connecting in your scripts.
  2. If source of script is same server as MySQL.

    CREATE USER '<user>'@'localhost'
    IDENTIFIED BY 'password';
    GRANT ALL
    ON <database>.*
    TO '<user>'@'localhost';
    
  3. If the connection is always established from the same place but a different location than MySQL, run the following on the command line.

    CREATE USER '<user>'@'<IP_address>'
    IDENTIFIED BY 'password';
    GRANT ALL
    ON <database>.*
    TO '<user>'@'<IP_address>';
    
  4. If the source of the connection varies, run the following command.

    CREATE USER '<user>'@'<IP_address>'
    IDENTIFIED BY 'password';
    GRANT ALL
    ON <database>.*
    TO '<user>'@'%';
    

Here is a link to the documentation if you have any questions.



来源:https://stackoverflow.com/questions/57950164/access-denied-for-userlocalhost

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!