When trying to access my Laravel website I'm getting an SQL connection refused error when trying to execute $candidate = App\Models\Candidate::find(400);
SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (SQL: select * from `candidates` where `candidates`.`candid` = 400 limit 1)
However, I can connect fine using php artisan tinker
from the command line:
root@454ab4403b8f:/app# php artisan tinker
Psy Shell v0.9.6 (PHP 7.2.7-1+ubuntu18.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> App\Models\Candidate::find(400)
=> App\Models\Candidate {#2865
candid: 400,
firstname: "",
...
}
Is there a different configuration for artisan than web? Why would one be working but not the other?
The ROOT account on a MySQL database is usually blocked for login that is not console. Artisan is console and your user is recognised.
Just make in your database a dedicated database and database user for your development purposes.
And never use root for web applications. Always make a dedicated user for databases and such. Root has too much power, and some errors that would possibly pop up on a normal user won't pop up under root which could lead to unexpected bugs when moving to production. In your local environment you wish to mimic production as close as possible, with all the same limitations.
Also if your website would end up with a weakness and on production you also use ROOT as database user, then if a hacker found the weakness the hacker could read/modify/wipe all the data in all your databases and possibly have access to your filesystem and gather other files that way. Just never use root except to configure the database.