我有以下用于创建数据库,用户和向用户授予权限的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帖子帐户创建钩子脚本)中理想情况下它将是完全自包含的
答案 0 :(得分:6)
您需要使用小写“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;"
答案 1 :(得分:1)
您的报价和大小写有点偏离(或平台偏见),但实质上是肯定的。
您可能想要考虑实际让您的脚本创建一个sql脚本,然后让它通过php,shell(s!)等运行会更容易。