最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

config - Laravel uses wrong MySQL user despite .env configuration (SQLSTATE[HY000] [1698]) - Stack Overflow

programmeradmin2浏览0评论

I am working on a Laravel project and trying to configure my database connection. I have set the correct MySQL credentials in my .env file, but Laravel is still trying to connect using the root user instead of the one I specified.

My Setup

  • Laravel version: Laravel Framework 11.41.3
  • Database: MySQL
  • OS: Ubuntu 22.04

My .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=react_breeze
DB_USERNAME=other_username_than_root
DB_PASSWORD=password

Issue:

When I run php artisan migrate or php artisan cache:clear, I get the following error:

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (Connection: mysql, SQL: delete from cache)

When I try I get same problem

rm -rf bootstrap/cache/config.php

php artisan config:clear
php artisan cache:clear
php artisan config:cache

I am working on a Laravel project and trying to configure my database connection. I have set the correct MySQL credentials in my .env file, but Laravel is still trying to connect using the root user instead of the one I specified.

My Setup

  • Laravel version: Laravel Framework 11.41.3
  • Database: MySQL
  • OS: Ubuntu 22.04

My .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=react_breeze
DB_USERNAME=other_username_than_root
DB_PASSWORD=password

Issue:

When I run php artisan migrate or php artisan cache:clear, I get the following error:

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (Connection: mysql, SQL: delete from cache)

When I try I get same problem

rm -rf bootstrap/cache/config.php

php artisan config:clear
php artisan cache:clear
php artisan config:cache
Share Improve this question edited 9 hours ago Mark Rotteveel 109k226 gold badges155 silver badges219 bronze badges asked yesterday Mensour FekharMensour Fekhar 1 New contributor Mensour Fekhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 4
  • Do you have another .env file apart from .env.example – Emeka Mbah Commented yesterday
  • Ensure the DB_HOST values are correct. Especially using sail – Soneye Oluwasina Commented 20 hours ago
  • This question is similar to: Laravel does not accept new database user in the .env file. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Soneye Oluwasina Commented 20 hours ago
  • If you're using sail, you have to sail build --no-cache. More information can be found here. – Marc H. Commented 17 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0
  1. Laravel is still using the old cached configuration If you recently updated your .env file, Laravel may still be using cached settings.

Solution: Run the following commands to clear and refresh the configuration:

php artisan config:clear
php artisan cache:clear
php artisan config:cache
  1. MySQL User Permission Issue Your MySQL user (other_username_than_root) might not have sufficient privileges on the react_breeze database.

Solution: Log in to MySQL using root (if you have access) and grant the necessary privileges:

sudo mysql -u root -p

Then, execute:

  GRANT ALL PRIVILEGES ON react_breeze.* TO 
 'other_username_than_root'@'localhost' IDENTIFIED BY 'password';
  FLUSH PRIVILEGES;
  EXIT;

Try restarting MySQL:

 sudo systemctl restart mysql
发布评论

评论列表(0)

  1. 暂无评论