Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

MySQL syntax error GRANT

by Anonymous Monk
on Oct 02, 2018 at 18:29 UTC ( [id://1223419]=perlquestion: print w/replies, xml ) Need Help??

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

What can be wrong in this syntax? I want to create a user and grant some rights in a MySQL database (8.0) using the latest DBD driver

my $user = 'somename'; my $password="0123"; my $db ="test"; my $host="localhost"; print "Connecting to terminology database $db as $user... "; my $dsn = "DBI:mysql:$db:$host:$port"; my %attr = (PrintError=>0, RaiseError=>1); my $dbh = DBI->connect($dsn,$Superuser,$pass, \%attr); print "done!\n"; my $sql; print "Creating user $user\n"; $sql = 'CREATE USER ?@? IDENTIFIED BY ?'; $dbh->do($sql, undef, $user, "localhost", $password); print "done!\n"; print "Granting user $user some rights\n"; $sql = 'GRANT ALL ON $db.* TO ?@? IDENTIFIED BY ?'; $dbh->do(q{GRANT ALL ON ?.* TO ?@? IDENTIFIED BY ?}, {}, $db, $use +r, $host, $password); print "done!\n";

I connect to the database as "root", so it should have all the privileges. I get the following

Connecting to terminology database test as root... done! Creating user somename done! Granting user somename some rights DBD::mysql::db do failed: You have an error in your SQL syntax; check +the manual that corresponds to your MySQL server version for the righ +t syntax to use near ''test'.* TO 'somename'@'localhost' IDENTIFIED B +Y '0123'' at line 1

Replies are listed 'Best First'.
Re: MySQL syntax error GRANT
by poj (Abbot) on Oct 02, 2018 at 19:38 UTC

    See DBI placeholders

    With most drivers, placeholders can't be used for any element of a statement that would prevent the database server from validating the statement and creating a query execution plan for it.

    i.e database names, table names, column names etc. Try

    $sql = "GRANT ALL ON $db.* TO ?@? IDENTIFIED BY ?"; $dbh->do($sql, {}, $user, $host, $password);
    poj

      Very interested indeed. It works, provided IDENTIFIED BY ? is not passed, as it really seems it is no (more?) accepted in 8.0

Re: MySQL syntax error GRANT
by Paladin (Vicar) on Oct 02, 2018 at 18:54 UTC
    According to the docs GRANT doesn't have an IDENTIFIED BY clause.

      I thought about this, but even:

      my $user = 'somename'; my $db ="test"; my $host="localhost"; $dbh->do(q{GRANT ALL ON ?.* TO ?@?}, {}, $db, $user, $host);

      does not produce any improvement, same syntax error. I am quite perplex.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2024-04-23 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found