Storing mysql data on the host from a linux guest

171 views
Skip to first unread message

Tom Butler

unread,
Mar 8, 2016, 7:19:09 AM3/8/16
to Vagrant
I want to be able to store MySQL's data on the host machine using a shared folder so when typing vagrant destroy then vagrant up, the data from MySQL is retained. This works fine for a httpdocs directory.


I managed it in this way:

- Stop the server
- Copy the original mysql data from /var/lib/mysql to /var/lib/mysql-data
- Stop the server from running on startup
- Package the box

Then mount a shared folder, ./mysql (host) to /var/lib/mysql on the guest 

and run a provisioning script that, on the guest:

- Copies /var/lib/mysql-data to /var/lib/mysql
- Starts the server


This works fine, *most* of the time. However, sometimes the database gets corrupted. I'm not sure why this is but the ibdata1 file gets corrupted and becomes unrecoverable. I'm not sure why this is but it must be to do with stopping/starting the server and accessing the data folder across a shared folder. Obviously this isn't ideal.

To fix this, I created a systemd unit that runs mysqldump and dumps the database to /vagrant/database.sql and then changed vagrant to import the database in the provisioning script.

The systemd unit works and when shutting down the box without any shared folders set up, the database export is created before the server is shut down. However, when the .sql file is being written to a shared folder it's always zero bytes.  I am 90% sure this is because the vagrant halt command unmounts the shared folders and sends the shutdown signal. It's likely the shared folder is being umounted before the systemd shutdown script is run (or at least completed).

Is there any way to avoid this and have it save/load mysql data on the host rather than the guest?

Alvaro Miranda Aguilera

unread,
Mar 8, 2016, 8:16:51 AM3/8/16
to vagra...@googlegroups.com
hello,

Seems you can check vagrant-triggers plugin, and run those commands either before halt or destroy.

Alvaro.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/2aad586a-a7c7-426e-837a-1e98e7a4504a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Butler

unread,
Mar 8, 2016, 8:19:21 AM3/8/16
to Vagrant
That looks like it's exactly what I need, but I'd like anyone to be able to run the vagrant up command to use the Vagrantfile, is there any way to install the plugin when `vagrant up` is run by specifying it in the Vagrantfile?
Reply all
Reply to author
Forward
0 new messages