茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    Perl訪(fǎng)問(wèn)MSSQL并遷移到MySQL數(shù)據(jù)庫(kù)腳本實(shí)例
    來(lái)源:易賢網(wǎng) 閱讀:987 次 日期:2014-09-25 11:58:50
    溫馨提示:易賢網(wǎng)小編為您整理了“Perl訪(fǎng)問(wèn)MSSQL并遷移到MySQL數(shù)據(jù)庫(kù)腳本實(shí)例”,方便廣大網(wǎng)友查閱!

    Linux下沒(méi)有專(zhuān)門(mén)為MSSQL設(shè)計(jì)的訪(fǎng)問(wèn)庫(kù),不過(guò)介于MSSQL本是從sybase派生出來(lái)的,因此用來(lái)訪(fǎng)問(wèn)Sybase的庫(kù)自然也能訪(fǎng)問(wèn)MSSQL,F(xiàn)reeTDS就是這么一個(gè)實(shí)現(xiàn)。

    Perl中通常使用DBI來(lái)訪(fǎng)問(wèn)數(shù)據(jù)庫(kù),因此在系統(tǒng)安裝了FreeTDS之后,可以使用DBI來(lái)通過(guò)FreeTDS來(lái)訪(fǎng)問(wèn)MSSQL數(shù)據(jù)庫(kù),例子:

    代碼如下:

    using DBI;

    my $cs = "DRIVER={FreeTDS};SERVER=主機(jī);PORT=1433;DATABASE=數(shù)據(jù)庫(kù);UID=sa;PWD=密碼;TDS_VERSION=7.1;charset=gb2312";

    my $dbh = DBI->connect("dbi:ODBC:$cs") or die $@;

    因?yàn)楸救瞬辉趺从脀indows,為了研究QQ群數(shù)據(jù)庫(kù),需要將數(shù)據(jù)從MSSQL中遷移到MySQL中,特地為了QQ群數(shù)據(jù)庫(kù)安裝了一個(gè)Windows Server 2008和SQL Server 2008r2,不過(guò)過(guò)幾天評(píng)估就到期了,研究過(guò)MySQL的Workbench有從MS SQL Server遷移數(shù)據(jù)的能力,不過(guò)對(duì)于QQ群這種巨大數(shù)據(jù)而且分表分庫(kù)的數(shù)據(jù)來(lái)說(shuō)顯得太麻煩,因此寫(xiě)了一個(gè)通用的perl腳本,用來(lái)將數(shù)據(jù)庫(kù)從MSSQL到MySQL遷移,結(jié)合bash,很方便的將這二十多個(gè)庫(kù)上百?gòu)埍斫o轉(zhuǎn)移過(guò)去了,Perl代碼如下:

    代碼如下:

    #!/usr/bin/perl

    use strict;

    use warnings;

    use DBI;

    die "Usage: qq db\n" if @ARGV != 1;

    my $db = $ARGV[0];

    print "Connectin to databases $db...\n";

    my $cs = "DRIVER={FreeTDS};SERVER=MSSQL的服務(wù)器;PORT=1433;DATABASE=$db;UID=sa;PWD=MSSQL密碼;TDS_VERSION=7.1;charset=gb2312";

    sub db_connect

    {

    my $src = DBI->connect("dbi:ODBC:$cs") or die $@;

    my $target = DBI->connect("dbi:mysql:host=MySQL服務(wù)器", "MySQL用戶(hù)名", "MySQL密碼") or die $@;

    return ($src, $target);

    }

    my ($src, $target) = db_connect;

    print "Reading table schemas....\n";

    my $q_tables = $src->prepare("SELECT name FROM sysobjects WHERE xtype = 'U' AND name != 'dtproperties';");#獲取所有表名

    my $q_key_usage = $src->prepare("SELECT TABLE_NAME, COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE;");#獲取表的主鍵

    $q_tables->execute;

    my @tables = ();

    my %keys = ();

    push @tables, @_ while @_ = $q_tables->fetchrow_array;

    $q_tables->finish;

    $q_key_usage->execute();

    $keys{$_[0]} = $_[1] while @_ = $q_key_usage->fetchrow_array;

    $q_key_usage->finish;

    #獲取表的索引信息

    my $q_index = $src->prepare(qq(

    SELECT T.name, C.name

    FROM sys.index_columns I

    INNER JOIN sys.tables T ON T.object_id = I.object_id

    INNER JOIN sys.columns C ON C.column_id = I.column_id AND I.object_id = C.object_id;

    ));

    $q_index->execute;

    my %table_indices = ();

    while(my @row = $q_index->fetchrow_array)

    {

    my ($table, $column) = @row;

    my $columns = $table_indices{$table};

    $columns = $table_indices{$table} = [] if not $columns;

    push @$columns, $column;

    }

    $q_index->finish;

    #在目標(biāo)MySQL上創(chuàng)建對(duì)應(yīng)的數(shù)據(jù)庫(kù)

    $target->do("DROP DATABASE IF EXISTS `$db`;") or die "Cannot drop old database $db\n";

    $target->do("CREATE DATABASE `$db` DEFAULT CHARSET = utf8 COLLATE utf8_general_ci;") or die "Cannot create database $db\n";

    $target->disconnect;

    $src->disconnect;

    my $total_start = time;

    for my $table(@tables)

    {

    my $pid = fork;

    unless($pid)

    {

    ($src, $target) = db_connect;

    my $start = time;

    $src->do("USE $db;");

    #獲取表結(jié)構(gòu),用來(lái)生成MySQL用的DDL

    my $q_schema = $src->prepare("SELECT COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = ? ORDER BY ORDINAL_POSITION;");

    $target->do("USE `$db`;");

    $target->do("SET NAMES utf8;");

    my $key_column = $keys{$table};

    my $ddl = "CREATE TABLE `$table` ( \n";

    $q_schema->execute($table);

    my @fields = ();

    while(my @row = $q_schema->fetchrow_array)

    {

    my ($column, $nullable, $datatype, $length) = @row;

    my $field = "`$column` $datatype";

    $field .= "($length)" if $length;

    $field .= " PRIMARY KEY" if $key_column eq $column;

    push @fields, $field;

    }

    $ddl .= join(",\n", @fields);

    $ddl .= "\n) ENGINE = MyISAM;\n\n";

    $target->do($ddl) or die "Cannot create table $table\n";

    #創(chuàng)建索引

    my $indices = $table_indices{$table};

    if($indices)

    {

    for(@$indices)

    {

    $target->do("CREATE INDEX `$_` ON `$table`(`$_`);\n") or die "Cannot create index on $db.$table$.$_\n";

    }

    }

    #轉(zhuǎn)移數(shù)據(jù)

    my @placeholders = map {'?'} @fields;

    my $insert_sql = "INSERT DELAYED INTO $table VALUES(" .(join ', ', @placeholders) . ");\n";

    my $insert = $target->prepare($insert_sql);

    my $select = $src->prepare("SELECT * FROM $table;");

    $select->execute;

    $select->{'LongReadLen'} = 1000;

    $select->{'LongTruncOk'} = 1;

    $target->do("SET AUTOCOMMIT = 0;");

    $target->do("START TRANSACTION;");

    my $rows = 0;

    while(my @row = $select->fetchrow_array)

    {

    $insert->execute(@row);

    $rows++;

    }

    $target->do("COMMIT;");

    #結(jié)束,輸出任務(wù)信息

    my $elapsed = time - $start;

    print "Child process $$ for table $db.$table done, $rows records, $elapsed seconds.\n";

    exit(0);

    }

    }

    print "Waiting for child processes\n";

    #等待所有子進(jìn)程結(jié)束

    while (wait() != -1) {}

    my $total_elapsed = time - $total_start;

    print "All tasks from $db finished, $total_elapsed seconds.\n";

    這個(gè)腳本會(huì)根據(jù)每一個(gè)表fork出一個(gè)子進(jìn)程和相應(yīng)的數(shù)據(jù)庫(kù)連接,因此做這種遷移之前得確保目標(biāo)MySQL數(shù)據(jù)庫(kù)配置的最大連接數(shù)能承受。

    然后在bash下執(zhí)行

    代碼如下:

    for x in {1..11};do ./qq.pl QunInfo$x; done

    for x in {1..11};do ./qq.pl GroupData$x; done

    就不用管了,腳本會(huì)根據(jù)MSSQL這邊表結(jié)構(gòu)來(lái)在MySQL那邊創(chuàng)建一樣的結(jié)構(gòu)并配置索引。

    更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

    更多信息請(qǐng)查看腳本欄目
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

    2026國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類(lèi)型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專(zhuān)用圖標(biāo)
    聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
    咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)