簡體   English   中英

如何恢復mysqldump雙重編碼的數據庫

[英]How to restore the database double encoded by mysqldump

我使用 mysqldump 來備份我的數據庫。 我的數據庫被意外破壞了,現在我想恢復它。 但是 SQL 文件由 bug#28969 雙重編碼。 http://bugs.mysql.com/bug.php?id=28969 Is there any solution for my data to go back? 我只有mysqldump制作的SQL文件。 謝謝。


我找回了我的數據。 感謝大家。

通過這種方式,

1.導入雜亂的數據

2.使用 sqldump 作為 'mysqldump -h "$DB_HOST -u "$DB_USER" -p"$DB_PASSWORD" --opt --quote-names --skip-set-charset --default-character-set=latin1 "$ DB_NAME" > /tmp/temp.sql'

參考

http://pastebin.com/iSwVPk1w

我找回了我的數據。 感謝大家。

通過這種方式,

1.導入雜亂的數據

2.使用sqldump作為mysqldump -h "$DB_HOST -u "$DB_USER" -p"$DB_PASSWORD" --opt --quote-names --skip-set-charset --default-character-set=latin1 "$DB_NAME" > /tmp/temp.sql

參考

#!/bin/bash -e

DB_HOST="$1"
DB_USER="$2"
DB_PASSWORD="$3"
DB_NAME="$4"


mysqldump -h "$DB_HOST -u "$DB_USER" -p"$DB_PASSWORD" --opt --quote-names \
    --skip-set-charset --default-character-set=latin1 "$DB_NAME" > /tmp/temp.sql

mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" \
    --default-character-set=utf8 "$DB_NAME" < /tmp/temp.sql

如果您的數據庫包含正確的排序規則,但數據庫中的完整數據是雙重編碼的,那么這將幫助您記住您只執行一次並備份您的數據庫。

<?php
/**
 * DoublyEncodeCorrection.php
 *
 * NOTE: Look for 'TODO's for things you may need to configure.
 * PHP Version 5
 *
 */
ini_set('display_errors','1');
//error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
// TODO: Pretend-mode -- if set to true, no SQL queries will be executed.  Instead, they will only be echo'd
// to the console.
$pretend = true;

// TODO: Should SET and ENUM columns be processed?
$processEnums = false;

// TODO: The collation you want to convert the overall database to
$defaultCollation = 'utf8_general_ci';

// TODO Convert column collations and table defaults using this mapping
// latin1_swedish_ci is included since that's the MySQL default
$collationMap = array(
    'latin1_bin'        => 'utf8_bin',
    'latin1_general_ci' => 'utf8_general_ci',
    'latin1_swedish_ci' => 'utf8_general_ci'
);

$mapstring = '';
foreach ($collationMap as $s => $t) {
    $mapstring .= "'$s',";
}

$mapstring = substr($mapstring, 0, -1); // Strip trailing comma
//echo $mapstring;

// TODO: Database information
$dbHost = 'localhost';
$dbName = 'tina';
$dbUser = 'root';
$dbPass = 'root';

// Open a connection to the information_schema database
$infoDB = mysql_connect($dbHost, $dbUser, $dbPass);

mysql_select_db('information_schema', $infoDB);

// Open a second connection to the target (to be converted) database
$targetDB = mysql_connect($dbHost, $dbUser, $dbPass, true);
mysql_select_db($dbName, $targetDB);

if (!is_resource($targetDB)) {
    echo "Could not connect to db!: " . mysql_error();exit;
}

if (mysql_select_db($dbName, $targetDB) === FALSE) {
    echo "Could not select database!: " . mysql_error();exit;
}

//
// TODO: FULLTEXT Indexes
//
// You may need to drop FULLTEXT indexes before the conversion -- execute the drop here.
// eg.
//    sqlExec($targetDB, "ALTER TABLE MyTable DROP INDEX `my_index_name`", $pretend);
//
// If so, you should restore the FULLTEXT index after the conversion -- search for 'TODO'
// later in this script.
//

// Get all tables in the specified database
$tables = sqlObjs($infoDB,
    "SELECT TABLE_NAME, TABLE_COLLATION
     FROM   TABLES
     WHERE  TABLE_SCHEMA = '$dbName'");

foreach ($tables as $table) {
    $tableName      = $table->TABLE_NAME;
    $tableCollation = $table->TABLE_COLLATION;

    // Find all columns that aren't of the destination collation
    $cols = sqlObjs($infoDB,
        "SELECT *
         FROM   COLUMNS
         WHERE  TABLE_SCHEMA    = '$dbName'
            AND TABLE_Name      = '$tableName'
            ");

    $intermediateChanges = array();
    $finalChanges = array();

    foreach ($cols as $col) {

        // If this column doesn't use one of the collations we want to handle, skip it
        if (in_array($col->COLLATION_NAME, $collationMap)) {
            //echo "<pre>";print_r($col->COLUMN_NAME);exit;
           sqlExec($targetDB,"UPDATE $dbName.$tableName SET $col->COLUMN_NAME = CONVERT(CAST(CONVERT($col->COLUMN_NAME USING latin1) AS BINARY) USING utf8)") ;
        }
    }
}


/**
 * Executes the specified SQL
 *
 * @param object  $db      Target SQL connection
 * @param string  $sql     SQL to execute
 * @param boolean $pretend Pretend mode -- if set to true, don't execute query
 *
 * @return SQL result
 */
function sqlExec($db, $sql, $pretend = false)
{
    echo "$sql;\n";

    if ($pretend === false) {
        $res = mysql_query($sql, $db);
        //echo "<pre>";print_r($res);exit;
        $error = mysql_error($db);
        if ($error !== '') {
            print "!!! ERROR: $error\n";
        }

        return $res;
    }

    return false;
}

/**
 * Gets the SQL back as objects
 *
 * @param object $db  Target SQL connection
 * @param string $sql SQL to execute
 *
 * @return SQL objects
 */
function sqlObjs($db, $sql)
{
    $res = sqlExec($db, $sql);

    $a = array();

    if ($res !== false) {
        while ($obj = mysql_fetch_object($res)) {
            $a[] = $obj;
        }
    }

    return $a;
}

?> 

如果它只是將 UTF-8 字節加倍或添加一些東西,我建議將一個快速的 sed/awk 命令放在一起來匹配和糾正它們。

http://www.osnews.com/story/21004/Awk_and_Sed_One-Liners_Explained

如果您對此不滿意,任何支持正則表達式的腳本語言都可以用來輕松地做同樣的事情,盡管這可能需要幾分鍾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM