TimeSocks's avatar

Setting up MySQL with Laravel 5

Hi,

I'm completely new to Laravel (and relatively new to PHP in general) and I've been working through Jeff's Laravel 5 intro series. So far so good, using SQLite as per the videos. However, I have a pre-existing MySQL database that I'd like to use as well (or rather, instead). But I have no idea how to actually set it up in Laravel/Homestead.

Ideally I'd like to eventually export my existing database (currently just administered through XAMPP in Windows), import it into my Homestead VM and go from there, but I don't even know how to get going with MySQL in the first place to test things out. Can you help?

Thanks in advance

0 likes
7 replies
bobbybouwmann's avatar
Level 88

It's really easy to do! Open up your .env file and change these values to your needs

DB_HOST=localhost
DB_DATABASE=myDatabase
DB_USERNAME=homestead
DB_PASSWORD=secret

The example above uses the standard login credentials for homestead, if you didn't change them this should work. Also not the that you need to replace myDatabase with your database name

If you open your config/database.php you can see the keywords that are defined above. Make sure you have the database driver set to mysql instead of sqlite

TimeSocks's avatar

Thanks for the quick reply. Do I have to create the database from the MySQL command line first? I can't seem to actually start MySQL from the command line as it happens, I just get an error: ERROR 1045 (28000): Access denied for user 'vagrant'@'localhost' (using password: NO)

bobbybouwmann's avatar

You can access mysql with the command line using this

mysql -uhomestead -psecret

And then

CREATE DATABASE myDatabase;
TimeSocks's avatar

Ah-ha! I was leaving a space between the switches and the username/password. Moron. Thank you!

TimeSocks's avatar

Actually I was just about to ask about using Tinker, but then I heard Jeff's voice in my head, Obi-Wan Kenobi-like, and just used the same syntax as the videos. I realised it doesn't matter what kind of database it is because Tinker just uses the model as the interface (or something like that).

Coderooney's avatar

That's right, connections to DBMS (database management systems) are 'interfaced' in your database.php config file and you just need to make sure you have the driver installed as well as uncommented in your php.ini file :)

Please or to participate in this conversation.