I am able to connect to have a Laravel app connect to mysql. This is on a a website server not on my local environment if that makes any difference. My issue is I keep getting a error that says
SQLSTATE[HY000] [1045] Access denied for user 'fsmith'@'localhost' (using password: YES)
However I am able to connect to mysql if I just have a reguler php file setup with the below
$servername = "localhost"; $username = "fsmith"; //no this is not my real username $password = "12345"; //no this is not my real password try { $conn = new PDO("mysql:host=$servername;dbname=devcenter", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
So why does my test php page work but not laravel using the same credentials?
My database.php file looks like this
'mysql' => [ 'driver' => 'mysql', //'url' => env('DATABASE_URL','localhost'), 'host' => env('DB_HOST', 'localhost'), //'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'devcenter'), 'username' => env('DB_USERNAME', 'fsmith'), 'password' => env('DB_PASSWORD', '12345'), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],
Advertisement
Answer
It seems like Laravel is using the .env file instead of the values you have assigned in database.php.
Take this line for example:
'database' => env('DB_DATABASE', 'devcenter'),
Laravel will first check if the key DB_DATABASE exists and use the value assigned. If DB_DATABASE does not exist, it will use ‘devcenter’.