Venom Rush's avatar

Local VM database has incorrect name

I setup VirtualBox and Homestead on my local Windows 7 machine and while going through the Migrate video in the L5 Fundamentals series I found that my database is called "main" instead of "homestead".

I don't know how this happened and would appreciate any help in changing it because when I run php artisan migrate, the migration works fine, but I don't know where it's migrating to. The database "main" doesn't have any tables that I can see.

EDIT: One thing I've noticed is that there is no database.sqlite file inside my learning-laravel-5 folder on my harddrive but when I SSH into the VM I can see one in the storage folder. I don't know why this is the case because I think there was mention in one of the videos that the VM is supposed to sync with the files on your hdd.

vagrant@homestead:~/learning-laravel-5$ php artisan migrate:refresh
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
vagrant@homestead:~/learning-laravel-5$ sqlite3 storage/database.sqlite
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
sqlite> .databases
seq  name             file
---  ---------------  ----------------------------------------------------------
0    main             /home/vagrant/learning-laravel-5/storage/database.sqlite
sqlite>
0 likes
9 replies
Venom Rush's avatar

My database.php file

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_CLASS,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => 'sqlite',

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => storage_path().'/database.sqlite',
            'prefix'   => '',
        ],

        '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,
        ],

        'pgsql' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ],

        'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'prefix'   => '',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => false,

        'default' => [
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ],

    ],

];
lindstrom's avatar

First, it's a sqlite thing, not a homestead thing. If you want to see the tables, type .tables at the sqlite prompt. If you want to see more commands, type help.

Second, Jeffrey uses sqlite as a convenience in a lot of the casts. It saves him from having to switch back and forth from running commands in a terminal where he is ssh'd into homestead or where he doesn't have a server (homestead) running at all. Feel free to use MySQL. The only difference will be that you will SSH into homestead to run your artisan commands like php artisan migrate.

Third, if you want to use Sequel Pro, Navicat or MySQL Workbench outside of the VM, that's not a problem either. Your connection details for the client are:

Host name/IP address: 127.0.0.1
Port: 33060
User name: homestead
Password: secret
bashy's avatar

@lindstrom I would use the IP of the VM instead of using ports and 127.0.0.1. Can't run the above inside the VM. I also use MySQL via SSH so it's easy.

Venom Rush's avatar

Hi lindstrom. Thank you for responding but your post doesn't address my problem. If you read through my post you'll see that I've tried the .tables command and nothing shows up. This is evident in the code block of my original post. My migration works but I don't know where it's migrating to.

Can you tell me what's responsible for setting up the database because as far as I know the database name should be "homestead", not "main".

lindstrom's avatar

@bashy Hmm. This config works for me outside of the VM. I'm on Windows 7. It does work either way for me though 192.168.10.10 /3306. Not sure why I did that, but it may have had something do with the IP homestead set being 192.168.33.10 at some point as I see that for a few hosts entries.

@Venom Rush Let's do this the long way to be perfectly certain. First, cd directly into the directory /app/storage/database. In that directory there should be a file named database.sqlite. Now:

sqlite3 database.sqlite // type this into your terminal
sqlite> .databases
seq  name             file
---  ---------------  ----------------------------------------------------------
0    main             /path/to/your/project/app/storage/
sqlite> .tables
// listing of your tables

Sqlite does not have database names so it assigns the name of "main". There is only one "database" per sqlite file so it doesn't even matter. When you specify your database name in your Homestead.yaml file, it is only used when creating the database for MySQL (and probably Postgres but I don't use Postgres). When vagrant provisions your Homestead box, it does not create an /app/storage/database.sqlite file. You must create this file manually.

Venom Rush's avatar

@lindstrom I don't mean to sound rude but are you even reading my posts? I have the database.sqlite file in my storage directory and running the .tables command after running "sqlite3 storage/database.sqlite" doesn't show me any tables.

lindstrom's avatar

Meh, redacted. I apparently can't read. Your migration ran. So, delete the database.sqlite file in storage and re-run it then try again. There is no reason why .tables shouldn't show you the migrated tables.

I'll add that I just tested on a fresh install of Laravel. It's also odd that the file didn't sync--you are correct it should. I was able to open the file and run .tables both locally and from homestead. I also created locally, ran the migration, checked, deleted, created within homestead, reran and checked. Wasn't able to reproduce your issue. Same version of sqlite3 on homestead, newer build local.

Hope you get it sorted.

Venom Rush's avatar

I uninstalled VirtualBox, restarted my machine and then reinstalled it. Went through the entire process again with some hiccups along the way but in the end it seems to be working as it should. Thanks for your input @lindstrom.

lindstrom's avatar

@Venom Rush Geez. I guess with the file syncing issue that could have been a clue, but really strange that everything was seemingly working. Glad you have it working.

1 like

Please or to participate in this conversation.