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
Verify the user api:
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:
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.
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.