在Shell脚本中创建数据库-从PHP转换 [英] Create database in Shell Script - convert from PHP

查看:53
本文介绍了在Shell脚本中创建数据库-从PHP转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用于创建数据库,用户并向用户授予权限的PHP代码:

I have the following PHP code that i use to create a database, a user, and grant permissions to the user:

$con = mysql_connect("IP.ADDRESS","user","pass");
mysql_query("CREATE DATABASE ".$dbuser."",$con)or die(mysql_error());
mysql_query("grant all on ".$dbuser.".* to  ".$dbname." identified by '".$dbpass."'",$con) or die(mysql_error());

我想从shell脚本中执行这些相同的操作.就是这样吗?

I want to perform these same actions but from within a shell script. Is it just something like this:

MyUSER="user"
MyPASS="pass"
MYSQL -u $MyUSER -h -p$MyPASS -Bse "CREATE DATABASE $dbuser;"
MYSQL -u $MyUSER -h -p$MyPASS -Bse "GRANT ALL ON $DBUSER.* to  $DBNAME identified by $DBPASS;"

编辑,因为在postwwwacct(cPanel发布帐户创建挂钩脚本)中需要使用此代码,理想情况下,它将完全是自包含的

EDIT as this is needed within postwwwacct (a cPanel post account creation hook script) ideally it will be entirely self-contained

推荐答案

您需要使用小写的"MYSQL",并在-h之后添加主机名,并且将单引号和双引号混合使用.另外,您需要设置dbnamedbuserdbpass的值,并使用一致的大小写.

You need to lower-case "MYSQL" and add a hostname after the -h and you've mixed single and double quotes. Also, you need to set the values for dbname, dbuser and dbpass and use consistent capitalization.:

MyUSER="user"
MyPASS="pass"
HostName="host"
dbName="dbname"
dbUser="dbuser"
dbPass="dbpass"

mysql -u $MyUSER -h $HostName -p$MyPASS -Bse "CREATE DATABASE $dbUser;"
mysql -u $MyUSER -h $HostName -p$MyPASS -Bse "GRANT ALL ON ${dbUser}.* to $dbName identified by $dbPass;"

但是我对您的SQL语法不是100%自信.我认为它会更像这样:

But I'm not 100% confident in your SQL syntax. I would think it would look more like this:

mysql -u $MyUSER -h $HostName -p$MyPASS -Bse "CREATE DATABASE $dbName;"
mysql -u $MyUSER -h $HostName -p$MyPASS -Bse "GRANT ALL ON ${dbName}.* to $dbUser identified by $dbPass;"

这篇关于在Shell脚本中创建数据库-从PHP转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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