使用php将sql文件导入远程mysql数据库

使用php将sql文件导入远程mysql数据库,php,mysql,Php,Mysql,php和mysql的新功能, 我正在尝试将导出的sql文件插入远程数据库。 我正在尝试此代码,但它不工作 $create_install_db_server = 'testdb.example.com'; $create_install_db_username = 'test123'; $create_install_db_password = 'test789'; $create_install_db_name = 'test'; $sqlfile = '/home/path/to/local

php和mysql的新功能, 我正在尝试将导出的sql文件插入远程数据库。 我正在尝试此代码,但它不工作

$create_install_db_server = 'testdb.example.com';
$create_install_db_username = 'test123';
$create_install_db_password = 'test789';
$create_install_db_name = 'test';
$sqlfile = '/home/path/to/localsql.sql';

$command='mysql -h' . $create_install_db_server .' -u' . $create_install_db_username .' -p' . $create_install_db_password .' ' . $create_install_db_name .' < ' . $sqlfile;
exec($command,$output=array(),$worked);
switch($worked){
    case 0:
        echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';
        break;
    case 1:
        echo 'There was an error during import.';
        break;
}
$create_install_db_server='testdb.example.com';
$create_install_db_username='test123';
$create_install_db_password='test789';
$create_install_db_name='test';
$sqlfile='/home/path/to/localsql.sql';
$command='mysql-h'$创建\u安装\u数据库\u服务器。“-u”$创建\u安装\u数据库\u用户名。“-p”$创建“安装数据库”密码。“”$创建\u安装\u数据库\u名称。“<”$sqlfile;
exec($command,$output=array(),$worked);
交换机(已工作){
案例0:
回显“导入文件”。$mysqlImportFilename。已成功导入数据库“$mysqlDatabaseName”;
打破
案例1:
echo“导入过程中出错。”;
打破
}

尝试以下方法

  • 在尝试执行命令之前,对命令进行回显

  • 尝试在终端上运行命令-检查是否成功

  • 如果成功,用一个简单的命令(如“touch/tmp/abc.txt”)替换exec,并检查是否正在创建文件


通过这样做,您试图找出您的mysql命令是否有问题,或者php中的exec函数是否有问题,我有另一种方法,试试这个

<?php
// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'testdb.example.com';
// MySQL username
$mysql_username = 'test123';
// MySQL password
$mysql_password = 'test789';
// Database name
$mysql_database = 'Test';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
    continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
    // Perform the query
    mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
    // Reset temp variable to empty
    $templine = '';
}
}
 echo "Tables imported successfully";

?>

对于执行大型sql文件,我上面的回答是不够的,因此我建议您查看@Abu sadat answer

您具体得到了什么错误?代码没有显示任何具体错误,只是“导入时出错”。总之,我可以得到具体的错误消息:“不工作”它是否会导致您的头部着火?这就是您的代码正在执行的回显操作…但是为什么不回显您在运行命令时遇到的错误呢?$output是一个空数组,因此我无法获得有关错误的任何详细信息。如果sql文件很大,那么哪个选项更好?高管()还是你的?问得好@srikanth。。对于大文件,我通常更喜欢exec()。你可以看看这个解释,我已经根据你的问题编辑了我的答案,如果你的命令行查询有效,而php exec不起作用,请在@srikanth处进行检查。。。打开终端并键入visudo-找到行“requirety”,并将其替换为“!要求'
$db = new PDO($mysql_host, $mysql_username, $mysql_password);

$sql = file_get_contents('churc.sql');

$qr = $db->exec($sql);