gpt4 book ai didi

php - 如何使用 shell_exec php 创建 MySql 数据库?

转载 作者:行者123 更新时间:2023-11-29 03:30:52 26 4
gpt4 key购买 nike

我在 IIS 和 Windows 7 中运行 MySql + PHP。这是我用来尝试创建新数据库 somedb 的 php 代码:

$cmd = escapeshellcmd('mysql -u root -p myPasswd  -h localhost -Bse "create database somedb;" ');
$test = shell_exec("C:\\Program^ Files^ ^(x86)\\MySQL\\MySQL^ Server^ 5.6\\bin\\".$cmd);

var_dump($test);
echo "<br>============<br>";
print_r($test);

我放置 var_dump 和 print_r 只是为了检查输出。运行代码后,我得到了这个输出:

string 'C:\Program  Ver 14.14 Distrib 5.6.23, for Win32 (x86)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: C:\Program [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table'... (length=12085)
=========================================================================================
C:\Program Ver 14.14 Distrib 5.6.23, for Win32 (x86) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Usage: C:\Program [OPTIONS] [database] -?, --help Display this help and exit. -I, --help Synonym for -? --auto-rehash Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash. (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect. --auto-vertical-output Automatically switch to vertical output mode if the result is wider than the terminal width. -B, --batch Don't use history file. Disable interactive behavior. (Enables --silent.) --bind-address=name IP address to bind to...

看来 shell_exec 可以调用 bin 目录中的 mysql,但它没有创建数据库。这不是权限问题。我尝试在命令行中使用 sql 语句创建并且效果很好。我已经尝试在没有 -h localhost 的情况下创建,或者将 -Bse 更改为 -e 或 --execute。我已经尝试了这篇文章的所有建议 Create database in Shell Script - convert from PHP但它对我不起作用。我错过了什么?

============================================= ============================

只是为了更新,如果我在 Apache+win7 中运行,这是一个运行良好的代码:

echo shell_exec("C:\\xampp\\mysql\\bin\\mysql   -u root    -e \"CREATE DATABASE IF NOT EXISTS somedb\" ");

它创建数据库并在浏览器中不显示任何内容。注意:与-u和root之间的空格无关。

最佳答案

开始时去掉 -u 和 -p 之后的空格。我知道它看起来很奇怪,但如果你不这样做,mysql 就会有动脉瘤。

此外,如果您无论如何都在使用 php,您为什么要这样做?只需从 php 中删除 mysql 命令,无需 shell。如果您拥有的事件连接缺少执行此操作的凭据,请使用具有适当凭据的新连接对象,执行此操作,然后关闭该连接。

这确实让我想到了一个尴尬但必要的问题,即当您使用 shell 时,那个 shell 运行在哪个机器上,是不是服务器的客户端机器?

关于php - 如何使用 shell_exec php 创建 MySql 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30869086/

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