Laravel database connection error

5

Use mamp and set up the database.php file so

'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 testo returns 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;
    }
}

Routes.php file

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 you solve it? Thanks

    
asked by anonymous 13.11.2015 / 17:52

2 answers

4

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

php artisan cache:clear
php artisan config:cache
    
10.04.2016 / 01:51
3

You can do it in two ways:

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

In the file .env you'll find something like this:

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

Just put the data for your database.

Then in the file 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 env() function will fetch the data you filled in the .env file.

A second way is to delete the env() function from the database.php file and put it 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 will work that way too.

Using the .env file makes your application more secure, since it is a hidden file inside the hosting, as is .htaccess . There nobody could see the file that shows the data to enter your base.

    
26.01.2016 / 11:17