Reputation: 1170
I'm trying to pass a simple command to the vagrant machine.
vagrant ssh -c "mysql -u root -e'CREATE DATABASE testing';"
The db doesn't get created. Instead I just get 'logged in' to the vagrant machine via ssh.
The same thing happens when I run
vagrant ssh -c "touch test.txt"
I've also tried with:
--command
instead of shorthand -c
Any ideas what's going on?
Edit
I'm going to distribute my script and it's going to integrate my script with Homestead, so can't really tweak the vagrant configuration.
Edit
Since I'm using Homestead the user and pw is different. It should have been
vagrant ssh -c "mysql -u homestead -psecret -e'CREATE DATABASE testing;'"
not changing the code above to avoid confusion with already submitted answers.
But nonetheless, this does not solve the problem. The same thing is happening as described above.
Upvotes: 7
Views: 5720
Reputation: 5277
This works
vagrant ssh -- -t 'touch test.txt'
The flag -t creates a pseudo-tty for bash to use. Which is a variation on the answer to this question.
Running remote commands after vagrant ssh
Which is explained in more detail here.
Upvotes: 11
Reputation: 1306
try to directly use ssh instead of vagrant:
ssh vagrant -c 'your command'
you also probably needs to define vagrant host in your ~/.ssh/config
Host vagrant
HostName 127.0.0.1
User vagrant
Port 2222
IdentityFile ~/.vagrant.d/insecure_private_key
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes
Upvotes: -1
Reputation: 2246
you are missing a semicolon .
vagrant ssh -c "mysql -u root -e'CREATE DATABASE testing;'"
ref: https://laracasts.com/discuss/channels/servers/cant-create-a-mysql-database-when-sshing-into-vagrant
Upvotes: 0
Reputation: 5277
You could try adding that command to your bootstrap file and then use
vagrant provision
.
Example Vargant File
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.provider "virtualbox" do |v|
v.memory = 1024
end
config.vm.synced_folder "./", "/vagrant", id: "vagrant-root",
owner: "vagrant",
group: "www-data",
mount_options: ["dmode=777,fmode=777"]
end
Example bootstrap.sh that creates a postgres database with your command in it. You'll have to make sure you have the right mysql packages installed.
#!/usr/bin/env bash
add-apt-repository ppa:ondrej/php5-5.6
apt-add-repository ppa:chris-lea/redis-server
apt-get update
apt-get install -y --force-yes python-software-properties
apt-get install -y --force-yes postgresql postgresql-contrib
apt-get install -y --force-yes redis-server
apt-get install -y --force-yes apache2
apt-get install -y --force-yes php5
apt-get install -y --force-yes php5-imagick
apt-get install -y --force-yes php5-redis
apt-get install -y --force-yes php5-mcrypt
apt-get install -y --force-yes phppgadmin
apt-get install -y --force-yes php5-gd
apt-get install -y --force-yes php5-intl
apt-get install -y --force-yes php5-sqlite
apt-get install -y --force-yes php5-curl
apt-get install -y --force-yes git
if ! [ -L /var/www ]; then
rm -rf /var/www
mkdir /var/www
ln -fs /vagrant /var/www/vagrant-referron-com
#
sed -i "s#DocumentRoot /var/www/html#DocumentRoot /var/www#g" /etc/apache2/sites-available/000-default.conf
fi
sed -i "s#AllowOverride None#AllowOverride All#g" /etc/apache2/sites-available/000-default.conf
sed -i "s#AllowOverride None#AllowOverride All#g" /etc/apache2/apache2.conf
# copy the phppgadmin configuration
cp -f /vagrant/development/postgres/phppgadmin /etc/apache2/conf.d/
cp -f /vagrant/development/postgres/phppgadmin.conf /etc/apache2/conf-enabled/
cp -f /vagrant/development/postgres/config.inc.php /etc/phppgadmin/
# create the postgres database and user
sudo -u postgres createdb sometestdatabase
sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password 'postgres';"
# calling mysql to create your testing database.
sudo mysql -u root -e 'CREATE DATABASE testing ;'
# enable server to listen to port 8080
cp -f /vagrant/development/ports.conf /etc/apache2/
# install the other apache modules
a2enmod rewrite
php5enmod mcrypt
apachectl restart
Upvotes: 0