请帮助了解我在这里做错了什么。我想逃避是错误的,但我不能谷歌一个可行的例子。
my $host = "localhost";
my $port = "3306";
my $user = "root";
my $pass = "111";
my $db_name = "test";
my $db_user = "test";
my $db_pass = "test";
my $dsn = "dbi:mysql::$host:$port";
my $dbh = DBI->connect($dsn, $user, $pass) or die "Unable to connect: $DBI::errstr\n";
$dbh->do("CREATE DATABASE $db_name");
$dbh->do("CREATE USER $db_user\@$host");
$dbh->do("GRANT ALL ON $db_name.* TO $db_user\@$host IDENTIFIED BY $db_pass");
$dbh->disconnect();
错误:
DBD::mysql::db do failed: Operation CREATE USER failed for 'test'@'localhost' at /home/andrew/sandbox/script.pl line 42.
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 right syntax to use near 'test' at line 1 at /home/andrew/sandbox/script.pl line 43.
感谢您的回答。
答案 0 :(得分:1)
您需要引用密码。
$dbh->do("GRANT ALL ON $db_name.* TO $db_user\@$host IDENTIFIED BY '$db_pass'");
^ ^
虽然当然准备好的语句/参数会好得多(如果MySQL允许它们在这里,我不是100%肯定)。
您也应引用$db_user
和$host
(同样,假设参数不起作用)。
如果参数有效:
$dbh->do(q{GRANT ALL ON ?.* TO ?@? IDENTIFIED BY ?}, {}, $db_name, $db_user, $host, $db_pass);
我非常有信心参数可用于密码,也可能用于用户和主机。数据库名称,我不太确定。您可能只需使用quote_identifier
内嵌它。
编辑:您应该完全点击CREATE USER
行。授予权限将创建用户(并使用密码)。
答案 1 :(得分:0)
看起来你每次运行脚本时都试图创建用户 - 如果我尝试多次创建用户,我会得到确切的错误。