在Shell脚本中创建数据库 – 从PHP转换

前端之家收集整理的这篇文章主要介绍了在Shell脚本中创建数据库 – 从PHP转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下PHP代码,用于创建数据库,用户和授予用户权限:
$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脚本中执行这些相同的操作.它只是这样的:

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帖子帐户创建钩子脚本)中需要编辑,理想情况下它将是完全独立的

您需要使用小写“MysqL”并在-h之后添加主机名,并且您混合使用单引号和双引号.此外,您需要设置dbname,dbuser和dbpass的值并使用一致的大小写:
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语法没有百分之百的信心.我认为它看起来更像是这样的:

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;"
原文链接:https://www.f2er.com/bash/387122.html

猜你在找的Bash相关文章