Vagrant database setup

Bedrock isn’t up and running with Vagrant yet and since I’m trying to work Vagrant into my workflow, I have a “best practices” type of question. I’m sure will be answered once Vagrant is worked into Bedrock, but in the meantime:

I didn’t find any detailed info on how best to maintain a MySQL (or any, really) database using Vagrant. If I were to simply destroy my vagrant VM (since I’m done with a project, let’s say) and I want to keep the sql from the development site, how best to go about doing that?

Should I just go about backing it up to a folder or is there a more elegant way? Ideally there would be some way to have a vagrant command or at least a shell script to dump the database to a folder in my project directory before destroying the VM.

Anyway, just looking for some suggestions, thanks!

Not really sure about best practices for this. That seems like a use case that wouldn’t happen too often so I’d just probably just do it manually when needed. Just running a mysqldump on the VM and write it to your shared local folder.

This might help you (in development stage, at least).

Create a folder called db in your project folder.

Then in your Vagrantfile add this line:

config.vm.synced_folder "db/", "/var/lib/mysql/db_wp", disabled: false

(That’s the correct location for Ubuntu).

Rename db_wp to whatever you want to call your db.

When you first vagrant up the folder will be empty. But if you will see the db is in there (but it’s empty).

Import your sql file into that folder. After that you’ll see the files extract to uncompressed sql files.

Now when you destroy your VM, you’ll still have your sql files in place. No need to reimport them everytime.

Hope this helps.

2 Likes

Thanks wynnepirini, I will try that!

hi, i’m trying to do the same thing manually just so i can get an understanding of what is going on as server side stuff isn’t my strong point. but i’m having difficulties getting wordpress to run.

what i’m doing at the moment is using a simple shell script to provision my VM with a LAMP stack but i have absolutely no idea what the db_name, user and password is so i can fill in the wp-config.php file?

I’ve been playing around in the VM using ssh but i can’t seem to find out how to check my DB name, user and password or even if one exists.

How did you set up your Vagrant VM? If you set everything up manually, then you should have set a password for the root mysql user when it was set up.

If you downloaded a Vagrantfile from somewhere, it should probably say somewhere inside the Vagrantfile or possibly the Puppet/Chef/Shell script it uses to provision the server.

Going by that shell script:

db_user = root
db_password = PASSWORD

You’d need to create a database first using those credentials, then use that db name for db_name.

Example: mysql -u root -e "create database wp_site_name;"

1 Like