Laravel - MySql connection help

I have added the Laravel framework to my site but cannot connect to my MySql database.
config > database I have
‘host’ => env(‘DB_HOST’, ‘’),
‘port’ => env(‘DB_PORT’, ‘3306’),

and in .env file I have


and get the error message SQLSTATE[HY000] [2002] Connection refused (SQL: SELECT * FROM users)

When I change the host to localhost in both files I get the error message

SQLSTATE[HY000] [2002] No such file or directory (SQL: SELECT * FROM users)

For reference I created the following route in app > http > routes.php and in routes > web.php


I created the following controller in app > http > controllers > StudViewController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use App\Http\Requests; use App\Http\Controllers\Controller; class StudViewController extends Controller { public function index() { $users = DB::select('SELECT * FROM users'); return view('stud_view',['users'=>$users]); } } but the view gives the error messages above. Can you help please.

Thanks for your response. I read a recent post regarding SQL help and found that replacing localhost in both files with has worked. I am now connected

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.