如何使用 shell_exec php 创建 MySql 数据库? [英] How to create a MySql database using shell_exec php?

查看:47
本文介绍了如何使用 shell_exec php 创建 MySql 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running MySql + PHP inside a IIS and windows 7. This is the php code that I am using to try to create a new database somedb:

$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 只用来检查输出.运行代码后,我有这个输出:

I put var_dump and print_r only to check the output. After running the code I have this output:

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.我已经尝试了这篇文章的所有建议 在 Shell 脚本中创建数据库 - 转换来自 PHP 但它对我不起作用.我错过了什么?

So it seems that shell_exec can call the mysql inside the bin directotry but it is not creating the database. It is not a permission issue. I've tried to create using sql statement inside command line and it works good. I already tried to create without -h localhost, or changing -Bse for -e or --execute. I've tried all suggestions of this post Create database in Shell Script - convert from PHP but it is not working for me. What am I missing?

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

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

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

Only to update, this is a code that works good if I run in Apache+win7:

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

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

It creates the database and display nothing in the browser. Note: no matter with the space between -u and root.

推荐答案

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

Ok I found the solution:

  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.只需复制并粘贴到系统变量路径中即可.
  2. 重新启动计算机,以便 windows 可以识别新变量启动系统时.
  3. 进行测试.运行命令行.只需输入 mysql -uUsername-p密码.应该可以进入mysql.
  4. 进入php代码,删除mysql的路径.新的 shell_exec应该是这样的:

  1. I follow this tutorial to add system environment variables. Start at 7:40. It was made to PHP but we can do the same for MySql. https://www.youtube.com/watch?v=AEyzKm2pTBw&index=51&list=PL1C87CCACA68A94A3 . Just find what is the path to the bin folder inside mysql folder instalation. In my case it is C:Program Files (x86)\MySQL\MySQL Server 5.6\bin. Just copy and paste inside System VAriables Path.
  2. Restart the computer, so windows can recognize the new variables when start the system.
  3. Make a test. Run command line. Just type mysql -uUsername -pPassword. Should be able to enter in mysql.
  4. Go to php code and delete the path to mysql. The new shell_exec should be like this:

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

echo shell_exec("mysql -umYusername -pmYpassword -e \"CREATE DATABASE IF NOT EXISTS somedb\" ");

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

Note: AsConfused was rigth. We must to avoid empty space between -u and -p

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

Run the code. The database should be created!

这篇关于如何使用 shell_exec php 创建 MySql 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆