0

Ubuntu 14.04 でGrails 2.4.5 org.codehaus.groovy.runtime.ProcessGroovyMethodsを使用している場合:

def command = "mysqldump -h${databaseProperties.host} -u'${databaseProperties.username}' -p'${databaseProperties.password}' ${databaseProperties.name} " + table
print command
def proc = command.execute()
def oneMinute = 60000
proc.waitForOrKill(oneMinute)
if(proc.exitValue()!=0){
    println "[[return code: ${proc.exitValue()}]]"
    println "[[stderr: ${proc.err.text}]]"
    return null
}else{
    return proc.in.text.readLines()
}

私が持っている

[[return code: 2]]
[[stderr: mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect]]

しかし、printlinedコマンドをコピーして bash に貼り付けると、適切なダンプが表示されます。何が起こっている?


私も試しました:

  • mysqldumpをフルパスに変更: /usr/bin/mysqldump

  • 引数を文字列配列として送信しますが、結果は同じです。

  • 実行する通常の文字列としてコマンドを送信します。

    "mysqldump -hlocalhost -u'root' -p'password' database table"
    

システムbashで機能しますが、ProcessGroovyMethodとしては機能しません...

4

1 に答える 1

0

更新

これについて一晩考えた後、私は (今でも) 問題があなたのパスワードに関連していると確信しています. コマンド ラインでパスワードを提供することは実際にはベスト プラクティスではないため (mysqldump はこれについても警告します)、ログイン パスを作成して戦術を変更する必要があると思います。

次のコマンドを使用して、ログイン パスを作成します (これは 1 回限りの手順です)。

mysql_config_editor set --login-path=name --host=localhost --user=youruser --password

次に、実行しようとしているコマンドを Groovy から次のように変更します。

def command="mysqldump --login-path=name database table"

これにより、発生している問題が回避され、より安全になります。

元の答え:

問題を再現できました。String.execute()はコマンド シェルを使用しないため、一重引用符がパスワードの一部であるかのように mysqldump に渡されます。

編集String.execute():さらに考えた後、予想外の引用符の処理のため、Groovyはここに行く方法ではないと思います。パスワードにスペースが含まれていなくても問題ありませんが、脆弱になる可能性があります。

さらに制御が必要な場合は、次の使用を検討する必要がありますProcessBuilder

ProcessBuilder pb = new ProcessBuilder("mysqldump", "-h${databaseProperties.host}", "-u${databaseProperties.username}", "-p${databaseProperties.password}", databaseProperties.name, table);
pb.inheritIO();
Process p = pb.start();

編集:さらに調査して、スペースを含むパスワードでこれをテストしました。command.execute()これは適切に処理されませんが、ProcessBuilderメソッドを使用すると機能します。

String.execute() メソッドの予期しない動作の一部を説明する別の投稿を次に示します。

Groovy: 引用符が埋め込まれた文字列が期待どおりに実行されない

于 2015-11-15T04:24:19.617 に答える