Create a database in the Shell script - convert from PHP

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

$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());

I want to follow the same steps, but from within the shell script. This is 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;"

EDIT as this is necessary in postwwwacct (cPanel post script account creation hook), ideally it will be completely self-contained

+3
source share
2 answers

"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;"

100% 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;"
+6

( ), .

, script sql script, php, shell (s!) .. .

+1

Source: https://habr.com/ru/post/1744123/


All Articles