The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Developing cPAddon Script : How to get MySQL database name?

Discussion in 'cPanel Developers' started by seyfin, Mar 30, 2013.

  1. seyfin

    seyfin Registered

    Joined:
    Mar 8, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    I am developing a cPAddon Script. However, I am having difficulties trying to find out how to get the MySQL database name in my script code.

    The documentation on the cPanel's web-site (Configuration File Variables) states:

    "
    The following variables are used in the default config file:
    ...
    $VAR1 = {
    'mysql.$meta_info->{mysql}[0].sqldb' => 'database_name', #MySQL database name
    ...
    'mysql_pass' => 'abcdefghijklm', #Password for MySQL database user
    ...
    'mysql_user' => 'domainuser_dbuser', #MySQL Database user
    "

    So, in the code of my cPAddon Script (XCart.pm), I can get and print out the MySQL user and password as follows:

    sub install {
    my $cpo = shift;
    print "MySQL user: $cpo->{'mysql_user'} <br />";
    print "MySQL password: $cpo->{'mysql_pass'} <br />";
    }

    However, I am not sure what is the correct syntax to get the MySQL database name in my script?

    my $my_var = $cpo->{'mysql'}????

    Thank you in advance for your help,
    Sergey
     
  2. seyfin

    seyfin Registered

    Joined:
    Mar 8, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Well, it took some time to find it out, by experimenting with the code...

    Here is the correct code

    sub install {
    my $cpo = shift;
    print "MySQL database: $cpo->{'mysql_user_post'}<br />";
    print "MySQL user: $cpo->{'mysql_user'} <br />";
    print "MySQL password: $cpo->{'mysql_pass'} <br />";
    }

    it prints out everything I need

    MySQL database: xcdb1
    MySQL user: xc4cpane_xcdb1
    MySQL password: NmUTjXf_9L

    assuming the $meta_info hashref is defined earlier

    our $meta_info = {
    'mysql' => ['xcdb'],
    }

    So, the value of $cpo->{'mysql_user_post'} is combined with the value of $meta_info->{'mysql'} and unique id of the database being created by the script.
     
  3. seyfin

    seyfin Registered

    Joined:
    Mar 8, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    So, I can use the following code to get full database name

    $cpo->{'mysql_database'} = "$cpo->{'user'}_$cpo->{'mysql_user_post'}";
    print "MySQL database: $cpo->{'mysql_database'}<br />";

    It will print out:

    MySQL database: xc4cpane_xcdb1
     
Loading...

Share This Page