1. 程式人生 > >DBA禁止mysqldump操作如何匯出資料

DBA禁止mysqldump操作如何匯出資料

* 安裝mysqlworkbench

 

* 新建查詢sql

select * from W_SalesPackages order by guid desc limit 1000;

Copy Row (with names)

 

* 貼上到 W_SalesPackages.txt檔案中

# guid, pids, name, coursecontent, standard, teacherids, grades, pricedescription, packageimage, servicecontent, improvecontent, opentime, closetime, ispubli\
sh, publishdate, isvalid, inserttime, type, tutortimes, subject
'p000', 'p12345', '測試', '<p>文科:包含高二數學、英語上下學期同步複習課程、高二語文同步複習課程、高中地理同步複習課程一、高中地理同步複習課程二,共7個課程,優惠價1500元(按單科報名原價:2100元)!贈送課程:加贈高一英語上下學期同步複習課程,共2個課程,價值600元!</p>', '2', '凃潔,黎寧,林斌,孟衛東,鄭克強,林祖榮,田佩淮', 'g2', '理科,2300,3300/文科,1500,2100', 'edm.jpg', 'service2_img.png', '', '2012-05-16 02:00:00', '2012-05-30 03:15:00', '0', '0000-00-00 00:00:00', '0', '2012-05-16 07:06:00', '0', '0', ''
'P0001', 's187927,s184480', '內部測試打包課', '內部測試打包課請勿釋出', '0', '內部測試打包課', '0', ',,/,,/', 'banner_register_20170911.jpg', 'banner_register_20170911.jpg', 'banner_register_20170911.jpg', '0000-00-00 00:00:00', '1970-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '2018-01-18 08:56:10', '0', '0', ''
'p1', 'p1', '圖片入口', '測試測試', '1', '凃潔,黎寧,林斌,孟衛東,鄭克強,林祖榮,田佩淮', 'g1', '優惠價,2300,3300', '20141114huyhi.jpg', '2014020807.png', '', '2012-05-09 00:00:00', '2021-08-31 00:00:00', '1', '0000-00-00 00:00:00', '0', '2012-05-09 06:48:25', '0', '0', ''


* LineProcessor.php
 

<?php

class LineProcessor {
    /** @var \SplFileObject */
    private $file;

    /** @var int */
    private $linum;

    /** @var  callable */
    protected $handler;

    public function __construct(string $path) {
        $this->file = new \SplFileObject($path, 'r');
        $this->linum = 0;
    }

    public function getline() /* :string */ {
        return $this->file->fgets();
    }

    /**
     * 試著讀取$limit行, 對每一行執行$callback
     * @param callable $callback
     * @param int $limit default unlimited
     * @return int 實際讀取的行數
     */
    public function forEach(callable $callback, int $limit = 0) {
        // unlimited
        if (0===$limit) {
            while ($this->file->valid()) {
                $line = $this->file->fgets();
                call_user_func($callback, $line, $this->linum);
                $this->linum++;
            }
            return 0;
        }
        for ($i = 0; $i < $limit && $this->file->valid(); $this->linum++) {
            $line = $this->file->fgets();
            if (!empty($line)) {
                call_user_func($callback, $line, $i);
                $i++;
            }
        }
        return $i;
    }

    public function setHandler(callable $cb) {
        if (!$cb) {
            $cb = function($cur, $i) {
                printf("%d %s\n", $i, $cur);
            };
        }
        $this->handler = $cb;
    }

    public function run() {
        $this->forEach($this->handler);
    }

}

* index.php

<?php

function __autoload($className) {
    include $className.'.php';
}

if ($argc < 2) {
    echo 'Usage: php '.__FILE__.' input.txt > output.sql'.PHP_EOL;
    exit;
}
$p = new \LineProcessor($argv[1]);
$a = explode(DIRECTORY_SEPARATOR, $argv[1]);
$filename = array_pop($a);
$parts = explode('.', $filename);
$tableName = $parts[0];

$fs = $p->getline();
$fs = substr($fs, 1);
$fs[strlen($fs)-1] = ' ';
$fields = explode(', ', $fs);

foreach ($fields as &$field) {
    $field = trim($field);
}
$fs = '`'.implode('`,`', $fields).'`';

$sql = "insert into `$tableName`(".$fs.") values";

$p->setHandler(function($cur) use ($sql) {
     // 去掉結尾\n, window \r\n 需要-2
    $cur = substr($cur, 0, strlen($cur)-1);
    if (!empty($cur)) {
        echo $sql.'('.$cur.');'.PHP_EOL;
    }
});

$p->run();

* 匯出建表語句 建立表

show create table  W_SalesPackages;

copy filed (unquoted)

命令列連線mysql 貼上進去建立表 (先建立資料庫)

mysql -uroot -hlocalhost -pXXXX--database XXXDB

 

* 生成sql插入語句

php ~/utility/lineprocessor/index.php ~/data/W_SalesPackages.txt > ~/data/W_SalesPackages.sql

W_SalesPackages.txt 檔名與表名 W_SalesPackages 一致

* 匯入資料

(mysql命令列中)> 

source ~/data/W_SalesPackages.sql