Hello,
I am trying to create database, user and grant all privileges to the user over the database from command line via Shell script, my script code is below:
However the whole operation is failing with the following output:
Please note that, I have replaced the actual cPanel username with "xxxxxxx". No matter if I am executing the script as root user (in the home directory of the "xxxxxxx" user) or as the "xxxxxxx" the output is the same.
I have used the following documentations for guidance:
• UAPI Functions - Mysql::create_database - Software Development Kit - cPanel Documentation
• UAPI Functions - Mysql::create_user - Software Development Kit - cPanel Documentation
• UAPI Functions - Mysql::set_privileges_on_database - Software Development Kit - cPanel Documentation
As well as many, many, many threads on this forum, however it seems that every time, I am in a dead end. Any ideas and/or suggestions will be very helpful, thank you!
I am trying to create database, user and grant all privileges to the user over the database from command line via Shell script, my script code is below:
Code:
#!/bin/bash
for i in `stat -c "%U" .`; do echo "The cPanel username is" $i ;done
cpprefix=$(uapi --user="$i" Mysql get_restrictions --output=json | sed -e 's/^.*"prefix":"\([^"]*\)".*$/\1/');
rndm=`shuf -i 100-999 -n 1`;
password=`date +%s | sha256sum | base64 | head -c 12 ; echo`;
db="$cpprefix""$rndm";
uapi --user="$1" --output=json Mysql create_database name="$db"
uapi --user="$1" --output=json Mysql create_user name="$db" password="$password"
uapi --user="$1" Mysql set_privileges_on_database user="$db" database="$db" privileges=ALL%20PRIVILEGES
printf "\n\e[33m==============\e\n";
printf "\e[36mDatabase & User:\e[0m $db\n";
printf "\e[36mDatabase password:\e[0m $password\n";
printf "\e[33m==============\e[0m\n";
Code:
The cPanel username is xxxxxxx
[2017-12-29 09:32:36 -0500] die [uapi] setuids failed: Attempting to setuid as a normal user with RUID 503 at /usr/local/cpanel/Cpanel/AccessIds/SetUids.pm line 89.
Cpanel::AccessIds::SetUids::_log_and_die_if_not_root() called at /usr/local/cpanel/Cpanel/AccessIds/SetUids.pm line 66
Cpanel::AccessIds::SetUids::setuids("xxxxxxx") called at bin/apitool.pl line 83
bin::apitool::run("--user=xxxxxxx", "Mysql", "get_restrictions", "--output=json") called at bin/apitool.pl line 29
die [uapi] setuids failed: Attempting to setuid as a normal user with RUID 503
exit level [die] [pid=27602] (setuids failed: Attempting to setuid as a normal user with RUID 503)
Option user requires an argument
{"func":"create_database","module":"Mysql","apiversion":3,"result":{"messages":null,"errors":["The name “746” does not begin with the required prefix “xxxxxxx_”."],"metadata":{},"status":0,"data":null}}
Option user requires an argument
{"func":"create_user","module":"Mysql","apiversion":3,"result":{"data":null,"metadata":{},"messages":null,"errors":["The name “746” does not begin with the required prefix “xxxxxxx_”."],"status":0}}
Option user requires an argument
---
apiversion: 3
func: set_privileges_on_database
module: Mysql
result:
data: ~
errors:
- The system user “xxxxxxx” does not control a MySQL database named “746”.
messages: ~
metadata: {}
status: 0
==============
Database & User: 746
Database password: ZWE3NDg4Y2U2
==============
I have used the following documentations for guidance:
• UAPI Functions - Mysql::create_database - Software Development Kit - cPanel Documentation
• UAPI Functions - Mysql::create_user - Software Development Kit - cPanel Documentation
• UAPI Functions - Mysql::set_privileges_on_database - Software Development Kit - cPanel Documentation
As well as many, many, many threads on this forum, however it seems that every time, I am in a dead end. Any ideas and/or suggestions will be very helpful, thank you!