I'm finding the MySQL privilege system to be rather confusing when it comes to how my Perl program relates to it.

I have a Perl program accessing MySQL on the same (Unix) machine, and I've already determined that MySQL is running, and that a particular database does NOT exist, so now I am attempting to create it (assume variables are defined accordingly):
my $drh=DBI->install_driver('mysql'); my $rc = $drh->func('createdb',$DATABASE,$HOST,$USER,$PASSWORD,'admin' +) or die "Can't create\n";

Now I understand that when MySQL checks authentication against the mysql:user table, the Hostname takes a priority match over User and Password (which drives me nuts). My question is this: does this mean that I can specify whatever $HOST I want, regardless of whatever host I am really using, in order to match an entry in the mysql:user table?

And is connecting to MySQL like this independent of whether or not the Perl program is run as root or with root ownership? I am asking because if I get the program to work on the command line, then I get privilege errors when running the program as a CGI using Apache. I think the confusion may arise when using
use Sys::Hostname; $HOST=hostname();
sometimes $HOST defaults to 'localhost', other times it defaults to the actual hostname of the machine. Should I just hard code 'localhost' as my host into the DBI commands?

I've read man DBI, man DBD::mysql, the Perl chapters of O'Reilly's MySQL book, and the MySQL site. I was hoping your experience can explain it to me or give me some insight into the 'proper' way of setting up privileges in running a perl program to do some admin work (create a non-existing db, create and alter tables) in MySQL.

By the way, my Perl program was using MySQL just fine. It wasn't until I installed the program on another machine did I realize these privileges are more complicated that I had expected.... I'm confused.....

In reply to Perl and MySQL privileges by fpi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.