【问题标题】:How to create a MySql database using shell_exec php?如何使用 shell_exec php 创建 MySql 数据库?
【发布时间】:2015-09-01 08:39:06
【问题描述】:

我在 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 之间的空格。

【问题讨论】:

    标签: php mysql shell


    【解决方案1】:

    好的,我找到了解决方案:

    1. 我按照本教程添加系统环境变量。开始于 7:40。它是为 PHP 制作的,但我们可以为 MySql 做同样的事情。 https://www.youtube.com/watch?v=AEyzKm2pTBw&index=51&list=PL1C87CCACA68A94A3 .只需找到mysql文件夹中bin文件夹的路径是什么 安装。就我而言,它是 C:Program Files (x86)\MySQL\MySQL 服务器 5.6\bin。只需在 System VARiables Path 中复制并粘贴即可。
    2. 重启电脑,windows可以识别新变量 什么时候启动系统。
    3. 进行测试。运行命令行。只需输入 mysql -uUsername -p密码。应该可以进入mysql。
    4. 转到php代码并删除mysql的路径。新的 shell_exec 应该是这样的:

      echo shell_exec("mysql -umYusername -pmYpassword -e \"如果某些数据库不存在则创建数据库\" ");

    5. 注意:AsConfused 是正确的。我们必须避免 -u 和 -p 之间有空格

    6. 运行代码。应该创建数据库!

    【讨论】:

      【解决方案2】:

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

      另外,如果你还是在 php 中,你为什么要这样做?只需从 php 中删除一个 mysql 命令,而无需 shell。如果您拥有的活动连接缺少执行此操作的凭据,请使用具有正确凭据的新连接对象,执行此操作,然后关闭该连接。

      这确实给我带来了一个尴尬但必要的问题,当你使用 shell 时,那个 shell 运行在什么盒子上,不是服务器的客户端机器?

      【讨论】:

      • 嗨,你是对的。对于 Windows 服务器,它不像空间。但没有解决问题。仍然没有创建数据库。检查我发布的更新。
      • Fair enuf 所以有点怪癖,但为什么要壳?用适当的信用在 php 中做这件事没有答案 eh
      • 好像php不是一个慢速的enuf平台,我们来shell吧。这就像非常自豪地参加 perl 或 cgi 脚本年度大会
      • 为什么要使用 shell_script?有时我们需要动态运行一些 sql 脚本。这是我找到的选项。谢谢。
      • 后者。请注意我在回答中写的关于可能需要凭据的内容
      猜你喜欢
      • 1970-01-01
      • 2013-08-30
      • 2012-11-19
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多