Vagrant ssh - command not working

I am trying to pass a simple command to a vagrant machine.

vagrant ssh -c "mysql -u root -e'CREATE DATABASE testing';" 

db is not created. Instead, I just β€œlogged in” to the vagrant machine via ssh.

The same thing happens when I run

 vagrant ssh -c "touch test.txt" 

I also tried:

  • single quotes around the command
  • --command instead of the abbreviated -c

Any ideas what is going on?

Edit

I am going to distribute my script and it is going to integrate my script with Homestead, so it cannot really configure the configuration of the vagrants.

Edit

Since I use Homestead, the user and pw are different. It should be

 vagrant ssh -c "mysql -u homestead -psecret -e'CREATE DATABASE testing;'" 

without changing the code above to avoid confusion with the answers already submitted.

Nevertheless, this does not solve the problem. The same thing happens as described above.

+6
source share
4 answers

It works

 vagrant ssh -- -t 'touch test.txt' 

The -t flag creates pseudo-tty for bash to use. What is the answer to this question.

Running remote commands after ssh firewall

This is explained in more detail here.

https://unix.stackexchange.com/questions/119894/single-command-to-login-to-ssh-and-run-program/119899#119899

+6
source

You can try adding this command to your bootstrap file and then use

 vagrant provision 

.

Sample 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 

An example bootstrap.sh that creates a postgres database with your command. You must ensure that you have the correct 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 
0
source

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

0
source

try using ssh directly instead of strollers:

 ssh vagrant -c 'your command' 

you also probably need to define a roaming host in ~/.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 
0
source

Source: https://habr.com/ru/post/1012268/


All Articles