SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP

后端 未结 5 1750
温柔的废话
温柔的废话 2020-11-27 19:23

I am new to PHP and CakePHP. I am finding problems while wiring my database using CakePHP.

Below is my application configuration.

I am on Bitnami WAMP stack

相关标签:
5条回答
  • 2020-11-27 20:03

    That error message usually means that either the password we are using doesn't match what MySQL thinks the password should be for the user we're connecting as, or a matching MySQL user doesn't exist (hasn't been created).

    In MySQL, a user is identified by both a username ("test2") and a host ("localhost").

    The error message identifies the user ("test2") and the host ("localhost") values...

      'test2'@'localhost'
    

    We can check to see if the user exists, using this query from a client we can connect from:

     SELECT user, host FROM mysql.user
    

    We're looking for a row that has "test2" for user, and "localhost" for host.

     user     host       
     -------  -----------
     test2     127.0.0.1  cleanup
     test2     ::1        
     test2     localhost  
    

    If that row doesn't exist, then the host may be set to wildcard value of %, to match any other host that isn't a match.

    If the row exists, then the password may not match. We can change the password (if we're connected as a user with sufficient privileges, e.g. root

     SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword')
    

    We can also verify that the user has privileges on objects in the database.

     GRANT SELECT ON jobs.* TO 'test2'@'localhost' 
    

    EDIT

    If we make changes to mysql privilege tables with DML operations (INSERT,UPDATE,DELETE), those changes will not take effect until MySQL re-reads the tables. We can make changes effective by forcing a re-read with a FLUSH PRIVILEGES statement, executed by a privileged user.

    0 讨论(0)
  • 2020-11-27 20:03

    I saw it's solved, but I still want to share a solution which worked for me.

    .env file:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=[your database name]
    DB_USERNAME=[your MySQL username]
    DB_PASSWORD=[your MySQL password]
    

    MySQL admin:

     SELECT user, host FROM mysql.user
    

    Thank you, spencer7593

    Console:

    php artisan cache:clear
    php artisan config:cache
    

    Now it works for me.

    Laracasts answers

    0 讨论(0)
  • 2020-11-27 20:06

    If you use MAMP, you might have to set the socket: unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock

    0 讨论(0)
  • 2020-11-27 20:20

    I want to add to the answers posted on above that none of the solutions proposed here worked for me. My WAMP, is working on port 3308 instead of 3306 which is what it is installed by default. I found out that when working in a local environment, if you are using mysqladmin in your computer (for testing environment), and if you are working with port other than 3306, you must define your variable DB_SERVER with the value localhost:NumberOfThePort, so it will look like the following: define("DB_SERVER", "localhost:3308"). You can obtain this value by right-clicking on the WAMP icon in your taskbar (on the hidden icons section) and select Tools. You will see the section: "Port used by MySQL: NumberOfThePort"

    This will fix your connection to your database.

    This was the error I got: Error: SQLSTATE[HY1045] Access denied for user 'username'@'localhost' on line X.

    I hope this helps you out.

    :)

    0 讨论(0)
  • 2020-11-27 20:28

    Check Following Things

    • Make Sure You Have MySQL Server Running
    • Check connection with default credentials i.e. username : 'root' & password : '' [Blank Password]
    • Try login phpmyadmin with same credentials
    • Try to put 127.0.0.1 instead localhost or your lan IP would do too.
    • Make sure you are running MySql on 3306 and if you have configured make sure to state it while making a connection
    0 讨论(0)
提交回复
热议问题