【问题标题】:Vagrant ssh --command not workingVagrant ssh --命令不起作用
【发布时间】:2017-03-26 20:02:51
【问题描述】:

我正在尝试将一个简单的命令传递给 vagrant 机器。

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

没有创建数据库。相反,我只是通过 ssh “登录”到 vagrant 机器。

当我跑步时也会发生同样的事情

vagrant ssh -c "touch test.txt"

我也试过:

  • 命令周围的单引号
  • --command 而不是简写 -c

有什么想法吗?

编辑

我将分发我的脚本,它会将我的脚本与 Homestead 集成,因此无法真正调整 vagrant 配置。

编辑

由于我使用的是 Homestead,所以用户和密码是不同的。应该是的

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

不要更改上面的代码以避免与已提交的答案混淆。

但是,这并不能解决问题。如上所述,同样的事情正在发生。

【问题讨论】:

    标签: ssh vagrant homestead


    【解决方案1】:

    这行得通

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

    标志 -t 创建一个伪 tty 供 bash 使用。 这是此问题答案的变体。

    Running remote commands after vagrant ssh

    这里有更详细的解释。

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

    【讨论】:

      【解决方案2】:

      尝试直接使用 ssh 而不是 vagrant:

      ssh vagrant -c 'your command'
      

      您可能还需要在 ~/.ssh/config 中定义 vagrant 主机

      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
      

      【讨论】:

        【解决方案3】:

        您缺少分号。

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

        参考:https://laracasts.com/discuss/channels/servers/cant-create-a-mysql-database-when-sshing-into-vagrant

        【讨论】:

        • 谢谢,但没有解决问题
        【解决方案4】:

        您可以尝试将该命令添加到您的引导文件中,然后使用

        vagrant provision
        

        .

        Vargant 文件示例

        # -*- 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
        

        示例 bootstrap.sh 创建一个包含您的命令的 postgres 数据库。您必须确保安装了正确的 mysql 软件包。

            #!/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
        

        【讨论】:

        • 谢谢,但我打算分发我的脚本,不能搞乱 vagrantfiles。我正在尝试专门与宅基地整合。不过谢谢。抱歉我没有指定
        猜你喜欢
        • 1970-01-01
        • 2017-12-06
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        • 1970-01-01
        • 2014-09-19
        • 2016-09-06
        • 2023-04-05
        相关资源
        最近更新 更多