Access denied for 'user'@'localhost'

前端 未结 1 1811
灰色年华
灰色年华 2021-01-23 23:45

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\'

相关标签:
1条回答
  • 2021-01-24 00:22

    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.

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