skip to Main Content

I tried to migrate the project but got an error

php artisan migrate

SQLSTATE[HY000] [1044] Access denied for user 'api'@'localhost' to database 'api' (Connection: mysql, SQL: select * from information_schema.tables where table_schema = api and table_name = migrations and table_type = 'BASE TABLE')

  at vendorlaravelframeworksrcIlluminateDatabaseConnection.php:801
    797▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    798▕                 );
    799▕             }
    800▕
  ➜ 801▕             throw new QueryException(
    802▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    803▕             );
    804▕         }
    805▕     }

  1   vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php:65
      PDOException::("SQLSTATE[HY000] [1044] Access denied for user 'api'@'localhost' to database 'api'")

  2   vendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php:65
      PDO::__construct()

Connection to mysql with login and password from .env is working

Paste "extension=pdo_mysql" in php.ini is also not working

2

Answers


  1. Verify the user api:

    • the password is correct?
    • user has permission to schema api and information_schema?
    Login or Signup to reply.
  2. Issue: Error: SQLSTATE[HY000] [1044] Access denied for user ‘api’@’localhost’ to database ‘api’

    Solution:

    This error indicates that the user ‘api’ does not have sufficient privileges to access the database ‘api’. To resolve this issue, follow these steps:

    1. Check Database Credentials: Verify that the database credentials in your .env file are correct. Ensure that DB_DATABASE, DB_USERNAME, and DB_PASSWORD are set appropriately.

    2. Database User Privileges: Make sure that the database user specified in your .env file has the necessary privileges to access the ‘api’ database. You can do this by logging into your MySQL or MariaDB server using a tool like phpMyAdmin or the MySQL command-line interface and granting the required privileges to the user.

    GRANT ALL PRIVILEGES ON api.* TO 'api'@'localhost';
    FLUSH PRIVILEGES;

    Replace ‘api‘ with your actual database username and ‘localhost‘ with your host if it’s different.

    After making changes, try reconnecting to the database to ensure that the issue is resolved.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search