Laravel database connection error

Asked

Viewed 4,705 times

5

Use Mamp and configure the database.php file like this

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost:8889'),
        'database'  => env('DB_DATABASE', 'estoque_laravel'),
        'username'  => env('DB_USERNAME', 'teste'),
        'password'  => env('DB_PASSWORD', '123'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

But when I test it returns to me :

PDOException in Connector.php line 55:

SQLSTATE[HY000] [2002] Connection refused

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;

class ProdutoController extends Controller { // cria controller para ser exibido
    public function lista(){
        $html = '<h1>Listagem de produtos com Laravel</h1>';
        $html .= '<ul>';
        $produtos = DB::select('select * from produtos');
        foreach ($produtos as $p) {
            $html .= '<li> Nome: ’. $p->nome .';
        }
        Descrição: '. $p->descricao .’</li>';

        $html .= '</ul>';
        return $html;
    }
}

File Routes.php

Route::get('/', function() { // func exec ao acessar o public - no caso retonamos um txt na func padrao retorna uma view
    return '<h1>Primeira lógica com Laravel</h1>';
});

Route::get('/outra', function() // ao passar o parametro outra ele envia outra msg
{
    return '<h1>Outra lógica com Laravel</h1>';
});

Route::get('/produtos', 'ProdutoController@lista'); // cria rout para o produtocontroller

How could it solve ? Thank you

  • Please avoid long discussions in the comments; your talk was moved to the chat

2 answers

6

I had the same problem and solved using the following commands:

php artisan cache:clear
php artisan config:cache

3

You can do it two ways:

To first is edit the file .env that comes in the project root folder when you create the project in Laravel.

In the archive .env you’ll find something like this:

DB_HOST= HOST
DB_DATABASE= DATABASE NAME
DB_USERNAME= USERNAME 
DB_PASSWORD= PASSWORD

Just put the data related to your database.

From there in the archive database.php, you leave like this:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

The function env() will fetch the data you filled in the file `.env.

One second way is to delete the function env() of the archive database.php and put directly into the value of the variable.

'mysql' => [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database_name',
    'username'  => 'username',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

It’ll work that way too.

Use the file .env makes your application safer since it is a hidden file within the hosting, as well as the .htaccess. Then no one could see the file that shows the data to enter your base.

  • I’m in the same trouble, you managed to succeed?

  • Yes @Pedrosoares. What’s your problem ?

  • Enter this URL, this is my problem http://answall.com/q/168218/35723

  • If you can help me, man, you’re gonna help me really, really

Browser other questions tagged

You are not signed in. Login or sign up in order to post.