Calling MySQL exe using PHP exec doesn't work

后端 未结 6 1093
一整个雨季
一整个雨季 2020-12-20 02:52

I have an extremely simple script with PHP exec, calling mysql command:

  $dbhost = \'localhost\';
  $dbuser = \'root\';
  $dbpass          


        
6条回答
  •  旧巷少年郎
    2020-12-20 03:13

    This was happening to me too even on Ubuntu. The problem was that i was doing the following code:

    $mysql = shell_exec("sudo which mysql");
    $cmd = "$mysql --host=$host --user=$user --password=$pass -D $dbname -e 'source $base_sql_file'";
    exec($cmd, $output, $retvar);
    

    Only a slight detail was missing... the shell_exec returns the output with trailling characters. When executing the command i was getting a MySQL error 127 (Record is crashed).

    The solution was simple: add a trim function to shell_exec :)

    $mysql = trim(shell_exec("sudo which mysql"));
    $cmd = "$mysql --host=$host --user=$user --password=$pass -D $dbname -e 'source $base_sql_file'";
    exec($cmd, $output, $retvar);
    

提交回复
热议问题