Search code examples
mysqlsshvagrantmysqlimportvagrant-windows

How to Import a MySQL Database When Using Vagrant


I am having trouble importing a database using vagrant.

I have run vagrant up and everything works fine. I then run vagrant ssh, sign-in to mysql, setup a database and user -- and again everything is working fine.

I then exit mysql (but NOT vagrant ssh) and run and run the following command:

mysql -u root -p mydatabase < "C:\Users\moshe\Websites\Projects\backup.sql"

And this point I get the following message:

-bash: C:\Users\moshe\Websites\Projects\backup.sql: No such file or directory

Any idea on what I am doing wrong?

P.S. In case it is relevant - I'm working on Windows 10.

P.S. 2: I took a look at this question and tried moving the backup.sql into the .vagrant folder and then running the mysql command, but it still didn't work.

UPDATE df command run in vagrant ssh

vagrant@scotchbox:~$ df
Filesystem                                    1K-blocks     Used Available Use% Mounted on
/dev/sda1                                      41251136  6750852  32764372  18% /
none                                                  4        0         4   0% /sys/fs/cgroup
udev                                            1019772       12   1019760   1% /dev
tmpfs                                            204992      380    204612   1% /run
none                                               5120        0      5120   0% /run/lock
none                                            1024956        0   1024956   0% /run/shm
none                                             102400        0    102400   0% /run/user
192.168.33.1:/C/Users/moshe/Websites/Projects 249478140 80929092 168549048  33% /var/www

Solution

  • I'm quite sure that you don't have Windows 10 in your VM, are you? At least the error message contains bash which is normally a Linux shell.

    So, if you have some Linux there, normally paths beginning with C:\ doesn't work. Instead, there must be some mount point in the Linux file system which maps to your Windows file system (is shared).

    Have a look at the /vagrant folder in your VM, it is very likely that you find your backup file in a sub folder of this directory. Try find /vagrant -name backup.sql to find the correct path.

    However, /vagrant is only the default for mounted folders in VirtualBoxes. So, if it doesn't exist, try the command mount | grep vboxsf in your VM. The output should look similar to this:

    vagrant on /vagrant type vboxsf (uid=1000,gid=1000,rw)
    var_www on /var/www type vboxsf (uid=33,gid=33,rw)
    

    One of the entries in your output (not the ones in this example) will be the one where your file is located.

    Alternatively you can use the command df which gives you the direct relation between the host directory (e.g. C:\...) and the folder in the VM (e.g. /var/www).

    In your case, it's the last line

    192.168.33.1:/C/Users/moshe/Websites/Projects 249478140 80929092 168549048  33% /var/www
    

    which shows that C:\Users\moshe\Websites\Projects (in Unix-like notation) maps to /var/www.