且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在php的共享主机上创建和导入mysql数据库

更新时间:2022-11-25 13:27:21

KISS原理:只是使用phpMyAdmin?几乎可以肯定已经安装了.如果不是,请安装它.

KISS principle: just use phpMyAdmin? It's almost certainly installed. If it's not, install it.

其强大的导入能力.如果您的数据库太大,则将其gzip.如果仍然很大,请尝试将其分成几部分.我怀疑您是否需要将其作为一项大交易进行转移.你呢?

Its import capability is magnificent. If your database is by any chance too big, gzip it. If it's still to big, try splitting it up in a few pieces. I doubt you need to transfer it as a single big transaction. Do you?

在第一个评论中的解释之后,就到了.这是我做的很简单的脚本.除了不看分隔符:一个查询==一行.

After the explanation in first comment, well, here goes. This is my very simplistic script which does what you want. Except it doesn't take a look at the separators: one query == one line.

<link rel="stylesheet" href="style/contents.css"/>
<?

function timesanitize($v) {
    if ($v > 0)
        return round($v, 4);
    else
        return 0;
}

$startmt = microtime();
include_once 'include/db.php';
$f = fopen("db.sql","r");
echo dbGetEngine() . "<br>";
echo "<ul>";
do {
    $l = rtrim(fgets($f));
    if (strlen($l) == 0)
        continue;
    if (substr($l, 0, 1) == '#')
        continue;
    $l = str_replace(
        array("\\n"),
        array("\n"),
        $l);
    if (dbGetEngine() == "pgsql")
        $l = str_replace(
            array("IF NOT EXISTS", "LONGBLOB"),
            array("", "TEXT"),
             $l);
    try {
        echo "<li>".nl2br(htmlspecialchars($l));
        $mt = microtime();
        $db->query($l);
        echo "<ul><li>ok - " . timesanitize(microtime() - $mt) . "</ul>";
    } catch (PDOException $e) {
        echo "<ul><li>".$e->getMessage() . "</ul>";
    }
} while (!feof($f));
fclose($f);

echo 'total: ' . timesanitize(microtime() - $startmt);
?>

它还会输出有关每个查询花费了多长时间的少量统计信息.它基于PDO;我相信PDO是在PHP5.1或PHP5.2中引入的.我认为修改它以直接与mysql_*()函数一起使用应该是微不足道的,如果出于某些原因您更喜欢这样做.

It also outputs a small statistic of how long each query took. It's based around PDO; I believe PDO was introduced in PHP5.1 or PHP5.2. I think it should be trivial to modify it to work directly with mysql_*() functions, if for some reason you prefer that.

再一次:是的,我知道这很糟糕.但只要它对我有效(tm),甚至对您有效::-)

And once again: yes, I know it sucks. But as long as it Works For Me (tm), and possibly You... :-)

要完成代码,请参见include/db.php和示例include/config.php:

To complete the code, here are include/db.php and a sample include/config.php:

include/db.php:

<?
include_once 'include/config.php';

try {

        $attribs =  
                array(
                        PDO::ATTR_PERSISTENT => $config['db']['persistent'],
                        PDO::ATTR_ERRMODE => $config['db']['errormode']
                );


        $db = new PDO(
                $config['db']['uri'],
                $config['db']['user'],
                $config['db']['pass'],
                $attribs
        );
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");

} catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
}

function dbGetEngine() {
        global $config;
        return substr($config['db']['uri'], 0, strpos($config['db']['uri'], ':'));
}
?>

include/config.php:

<?

//$config['db']['uri'] = 'sqlite:' . realpath('.') . '/site.db'; // PDO's database access URI
$config['db']['uri'] = 'mysql:host=localhost;dbname=sitedb'; // server should be : 195.78.32.7
//$config['db']['uri'] = 'pgsql:host=localhost;dbname=sitedb';
$config['db']['user'] = 'user_goes_here'; // database username
$config['db']['pass'] = 'pass_goes_here'; // database password
$config['db']['persistent'] = false; // should the connection be persistent
$config['db']['errormode'] = PDO::ERRMODE_EXCEPTION; // PDO's error mode

?>

包括用于SQLite,MySQL和PostgreSQL的示例连接字符串.

Included are sample connection strings for SQLite, MySQL and PostgreSQL.