gpt4 book ai didi

php - 使用 php 将 mysql 转储导入到机器上

转载 作者:行者123 更新时间:2023-11-30 22:51:49 25 4
gpt4 key购买 nike

我需要创建一个 install.php 文件,让我的讲师将数据库转储(我将提供)导入到他的机器上。

SQL 转储将称为 dump.sql,我有一个包含凭据的 config.php 文件和应该用于导入 sql 转储的 install.php。

配置.php

<?php
//defining variables
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','database');

//connection to mysql server
$conn=mysql_connect(DB_HOST, DB_USER, DB_PASS) or die ("Error connecting to server: ".mysql_error());
?>

安装.php

<?php

include_once ('config.php');

echo "starting install...";

//insert dbdump in the same folder as install.php (put them in a separate folder)
$command = "C:\xampp\mysql\bin\mysql -u".DB_USER." -p".DB_PASS." < dump.sql";
//echo $command;
exec($command, $output, $return_var);

$output = shell_exec($command);

if ($output) {
echo "database created";
}

?>

我正在使用 $command 变量,因为我读到这是一个复制 cmd 命令的标准变量,但仍未创建数据库。

我测试了相同的命令“mysql -uroot < C:\xampp\htdocs\Recipes\dump.sql”,它有效。

谁能指出可能是什么问题?

谢谢

最佳答案

1.在xampp文件夹中找到mysql.exe:

E:\xampp\mysql\bin\mysql.exe

2.在xampp文件夹中打开CMD

E:\xampp\mysql\bin\mysql.exe

3.在CMD中使用以下东西

mysql -h localhost -u root -p

4.显示数据库

show databases;

示例:

 MariaDB [(none)]> show databases;
+--------------------------+
| Database |
+--------------------------+
| information_schema |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
+--------------------------+
7 rows in set (0.101 sec)

MariaDB [(none)]>

5.使用数据库

use databases;

例子

MariaDB [(none)]> use test
Database changed
MariaDB [test]>

6.转储数据库 最后给出sql文件的源码路径

source E:\your\sql\file\path\mysqlfile.sql

例子

MariaDB [test]> >source E:\your\sql\file\path\mysqlfile.sql

关于php - 使用 php 将 mysql 转储导入到机器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com