Creating db and import sql in script hook
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-
-
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! 0 -
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 itdbcheck = '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))0 -
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! 0 -
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- 0 -
Hi @binooetomo I'm glad you were able to get the issue resolved and thank you for updating the thread! 0
Please sign in to leave a comment.
Comments
5 comments