Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

SOLVED Creating db and import sql in script hook

Discussion in 'cPanel Developers' started by binooetomo, Oct 8, 2018.

  1. binooetomo

    binooetomo Registered

    Joined:
    Sep 24, 2018
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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-
     
  2. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,072
    Likes Received:
    215
    Trophy Points:
    173
    Location:
    Houston
    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!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    binooetomo likes this.
  3. binooetomo

    binooetomo Registered

    Joined:
    Sep 24, 2018
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Indonesia
    cPanel Access Level:
    DataCenter Provider
    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))
    
     
  4. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,072
    Likes Received:
    215
    Trophy Points:
    173
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    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!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. binooetomo

    binooetomo Registered

    Joined:
    Sep 24, 2018
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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-
     
  6. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,072
    Likes Received:
    215
    Trophy Points:
    173
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
    Hi @binooetomo

    I'm glad you were able to get the issue resolved and thank you for updating the thread!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    binooetomo likes this.
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice