Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Create new user in MySQL

by gmishra (Sexton)
on Mar 26, 2004 at 11:35 UTC ( [id://339982]=perlquestion: print w/replies, xml ) Need Help??

gmishra has asked for the wisdom of the Perl Monks concerning the following question:

dear monks, i have to write a program, that takes database name, new user name and password as input from the user and create the database. I have written the following program.
use DBI; $drh = DBI->install_driver("mysql"); $database ='testg1'; $host ="localhost"; $user = "root"; $password = "passowrd"; $rc = $drh->func('createdb', $database, $host, $user, $password, ' +admin'); if ($rc == 1){print"new database created.."; } else{print "could not create database: $DBI::errstr\n";} ##################################### ######database created successfully ##################################### #################################### ### the following segment is giving problem ##################################### # the datasource $dbname = 'testg1'; my $db="$dbname:164.xxx.x.xxx:yyyy"; my $dbh = DBI->connect("DBI:mysql:$db",$user,$password) or die("ca +nt connect\n"); print "connected"; ########################################### ###### because of the error, i dont reach the following ##segment ############################################ $auser = 'newuser@localhost'; $pw = 'abcd'; my $query = qq|GRANT ALL on $db.* TO ? IDENTIFIED BY ?|; $dbh->do($query,undef,$auser,$pw) || die ("Could not do $query - E +rror: $DBI::errstr"); ###########################################
Though i am able to make the new database, i am not able to use it to create new user and grant rights. I am getting the following error.
DBI->connect(testg1:164.xxx.x.xxx:yyyy) failed: Access denied for user +: 'root@myhost' (Using password: YES) at create.pl line 19 cant connect
I can connect through the MySql console using same username and password. I can also create dtabase and grant rights etc from there. But through thr program, i am getting the "cant connect" error. Please help.

Replies are listed 'Best First'.
Re: Create new user in MySQL
by jaa (Friar) on Mar 26, 2004 at 12:02 UTC

    You should issue the GRANT first - before you attempt to connect to the database.

    The MySQL user authentication is actually managed in the mysql database - e.g. your list of users is in mysql.user.

    Note that if you manipulate the mysql.* tables the changes do not take effect until you issue a reload.

    Regards,

    Jeff

      hey thanks.
      But i need a database handle($dbh) to run the "grant" right? how dow do i make the handle.. unless i connect. Also, what is "reload"?
      Please help. I am very new in this area.
      waiting,
      gauravi

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://339982]
Approved by castaway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found