SOLVED Creating db and import sql in script hook

binooetomo

Member
Sep 24, 2018
13
1
3
Indonesia
cPanel Access Level
DataCenter Provider
Dear All.

I'm writing a python script and register it as a hook, with context 'Whostmgr::Accounts::Create' and stage 'post'

Part of the script is :
1. cd to /home/['username']/public_html (in this case the username is 'gijilint')
2. Call : uapi --output=json --user=gijilint Mysql create_database name=gijilint_wp_20181008203558
3. Call : mysql -D gijilint_wp_20181008203558 <./master_sql.sql

The cpanel error log always got :

'''
[2018-10-08 20:35:59 -0400] info [uapi] ---debug_hooks---
[2018-10-08 20:35:59 -0400] info [uapi] msg: No hooks found for traversed context
[2018-10-08 20:35:59 -0400] info [uapi] context: Cpanel::UAPI::Mysql::create_database
[2018-10-08 20:35:59 -0400] info [uapi] stage: post
ERROR 1049 (42000): Unknown database '<./master_sql.sql'

'''

I tried to go to mysql shell and call : show databases , and 'gijilint_wp_20181008203558' is there
exit from mysql shell back to bash shell, I manualy call 'mysql -D gijilint_wp_20181008203558 <./master_sql.sql' and got no error messages.

Login back to mysql shell , (using 'mysql -D gijilint_wp_20181008203558') and call 'show tables' ... and all the tables is created as expected.

My question is : why step #3 got 'ERROR 1049 (42000): Unknown database '<./master_sql.sql' ?

Sincerely
-bino-
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,298
1,279
313
Houston
hi @binooetomo

Why are you calling <./master_sql.sql ? The database name is gijilint_wp_20181008203558 unless there's a table in the db with that name it's not going to come up and from your creation of the database I don't see it being created.

Thanks!
 
  • Like
Reactions: binooetomo

binooetomo

Member
Sep 24, 2018
13
1
3
Indonesia
cPanel Access Level
DataCenter Provider
hi @binooetomo

Why are you calling <./master_sql.sql ? The database name is gijilint_wp_20181008203558 unless there's a table in the db with that name it's not going to come up and from your creation of the database I don't see it being created.

Thanks!
First, thankyou @cPanelLauren for your response.
From my script log (I re run the script , it already call '
mysql -D kupretin_wp_20181009205940 <./master_sql.sql

and still got the same error code :

ERROR 1049 (42000): Unknown database '<./master_sql.sql'

Actualy, before the import part ... my script do a double check to make sure if target database is ready.
'
Here is a snip of it


Code:
        dbcheck = 'mysqlshow %s' %(dbname)
        dbcheck = dbcheck.split()
        while True :
            try :
                p = subprocess.Popen(dbcheck, stdout=subprocess.PIPE)
                presult=p.communicate()
                assert presult[0].split('|')[0].split(':')[0]=='Database'
                break
            except Exception as e :
                time.sleep(1)
        sqljob='mysql -D %s <./master_sql.sql' %(dbname)
        writelog(sqljob)
        sqljob=sqljob.split()
        p = subprocess.Popen(sqljob, stdout=subprocess.PIPE)
        results= p.communicate()
        writelog(str(presult))
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,298
1,279
313
Houston
Hi @binooetomo


The part I am not understanding though is why you're referencing a table in the database that doesn't exist. This will never work unless master_sql.sql exists.

I'm not understanding the end goal you're looking to accomplish either and maybe understanding that will help.


Thanks!
 

binooetomo

Member
Sep 24, 2018
13
1
3
Indonesia
cPanel Access Level
DataCenter Provider
Dear @cPanelLauren

Thankyou for your response.

First I Apologize for this.

This is My vault.
I learn more about playing with python's subprocess.Popen and it fixed the problem.


That is ... the working code should :

myinput=open('master_sql.sql')
sqljob='mysql -D %s ' %(dbname)
writelog(sqljob)
sqljob=sqljob.split()
p = subprocess.Popen(sqljob, stdin=myinput, stdout=subprocess.PIPE)

Sincerely
-bino-