1

I created a database "mydatabase" and I changed config>database.php to:

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'mysite.local',
        'database'  => 'mydatabase',
        'username'  => 'myusername',
        'password'  => 'mypassword',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

now inside route.php i have:

    Route::get('/', function()
{
    $data=DB::table('user')->get();
    return $data;
});

laravel sends an Exception which shows that it tries to access:

homestead.user

instead of

mydatabase.user

now if i change route.php to:

    Route::get('/', function()
{
    $data=DB::table('mydatabase.user')->get();
    return $data;
});

it will work!

Also according to this question I changed config>local>database.php to:

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'mysite.local',
        'database'  => 'mydatabase',
        'username'  => 'myusername',
        'password'  => 'mypassword',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

But this time, even

$data=DB::table('mydatabase.user')->get();

doesn't work either! This time it thrown another exception :

PDOException (2002) 
SQLSTATE[HY000] [2002] Connection refused

My question is why laravel tries to use "homestead" database instead of "mydatabase"? should I change something else?

EDIT: I changed the config/local/database.php to

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydatabase',
        'username'  => 'myusername',
        'password'  => 'mypassword',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

and everything works fine! (I changed mysite.local to localhost) I've not define local host in my /etc/hosts so why laravel looks for that host?

1
  • check your default key in config/databse.php, does it have mysql or homesead. Yes you might be getting db config from env file, so change default " 'default' => env('DB_CONNECTION', 'mysql'), " to " 'default' => 'mysql', " Commented Jul 19, 2017 at 4:21

2 Answers 2

1

Your host should be localhost. The term localhost means the computer which laravel is running on. mysite.local is presumably a virtual site residing on this computer. It doesn't have its own installation of Mysql. All virtual sites will share the same mysql. They will just use different databases.

Thats how my setups work anyway.

Sign up to request clarification or add additional context in comments.

Comments

0
  • The problem is in your config/database.php with default connection, currently default connection setting is getting from .env file as

    'default' => env('DB_CONNECTION', 'mysql'),

So change it to :

'default' => 'mysql',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.