简体   繁体   English

如何在ubuntu服务器中创建.sql文件?

[英]How do I create a .sql file in ubuntu server?

I actually have this create.php code 我实际上有这个create.php代码

<?php 
    //ENTER THE RELEVANT INFO BELOW
    $mysqlUserName      = "root";
    $mysqlPassword      = "admin";
    $mysqlHostName      = "localhost";
    $DbName             = "corporativostr_inventario";
    $backup_name        = "corporativostr_inventario.sql";
    $tables             = array("articulos","articulos_eliminados","categorias","conceptos","proveedores","registros","responsables","responsables_eliminados","usuarios","usuarios_eliminados");

   //or add 5th parameter(array) of specific tables:    array("mytable1","mytable2","mytable3") for multiple tables

    Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );

    function Export_Database($host,$user,$pass,$name,  $tables=false, $backup_name=false )
    {
        $mysqli = new mysqli($host,$user,$pass,$name); 
        $mysqli->select_db($name); 
        $mysqli->query("SET NAMES 'utf8'");

        $queryTables    = $mysqli->query('SHOW TABLES'); 
        while($row = $queryTables->fetch_row()) 
        { 
            $target_tables[] = $row[0]; 
        }   
        if($tables !== false) 
        { 
            $target_tables = array_intersect( $target_tables, $tables); 
        }
        foreach($target_tables as $table)
        {
            $result         =   $mysqli->query('SELECT * FROM '.$table);  
            $fields_amount  =   $result->field_count;  
            $rows_num=$mysqli->affected_rows;     
            $res            =   $mysqli->query('SHOW CREATE TABLE '.$table); 
            $TableMLine     =   $res->fetch_row();
            $content        = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";

            for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0) 
            {
                while($row = $result->fetch_row())  
                { //when started (and every after 100 command cycle):
                    if ($st_counter%100 == 0 || $st_counter == 0 )  
                    {
                            $content .= "\nINSERT INTO ".$table." VALUES";
                    }
                    $content .= "\n(";
                    for($j=0; $j<$fields_amount; $j++)  
                    { 
                        $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) ); 
                        if (isset($row[$j]))
                        {
                            $content .= '"'.$row[$j].'"' ; 
                        }
                        else 
                        {   
                            $content .= '""';
                        }     
                        if ($j<($fields_amount-1))
                        {
                                $content.= ',';
                        }      
                    }
                    $content .=")";
                    //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
                    if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
                    {   
                        $content .= ";";
                    } 
                    else 
                    {
                        $content .= ",";
                    } 
                    $st_counter=$st_counter+1;
                }
            } $content .="\n\n\n";
        }
        //$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
        $backup_name = $backup_name ? $backup_name : $name.".sql";
        header('Content-Type: application/octet-stream');   
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"".$backup_name."\"");  
        echo $content; exit;
    }
?>

which creates a downloadable .sql file when I open the .php file in other PC with the web navigator 当我使用Web导航器在其他PC上打开.php文件时,它将创建一个可下载的.sql文件。

But when I run it with in ubuntu server I only see the db content. 但是,当我在ubuntu服务器中运行它时,我只能看到数据库内容。

How can I create a .sql file in the same directory where the create.php is located? 如何在create.php所在的目录中创建.sql文件?

You cannot create the .sql file with this code, this code is creating database with name prueba 您不能使用此代码创建.sql文件,此代码正在创建名称为prueba数据库

In order to create a .sql file file we use command line queries (can be used using application level too). 为了创建.sql文件文件,我们使用命令行查询(也可以在应用程序级别使用)。

mysqldump -u'user' -p'password' database_name > /path/filename.sql

This will create a .sql file and you can use the same query on the application level to create the database .sql provided the database use has the proper rights. 这将创建一个.sql文件,并且您可以在应用程序级别上使用相同的查询来创建数据库.sql,前提是该数据库具有正确的使用权限。

you can use other parameters like --routines in case your database has subroutines. 如果数据库具有子例程,则可以使用--routines等其他参数。 There are a number of optional parameters. 有许多可选参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM