gpt4 book ai didi

mysql - 从文件中读取密码时使用 perl dbi 获取 "Access denied"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:23 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个简单的 perl 脚本,使用 DBIMySQL 数据库中提取表格的全部内容。

我不想将数据库密码放在脚本中,所以我想设置一个密码变量,该变量从包含密码的文件中读取。

我已经设置了脚本,以便三个 DBI 参数(DSNuserpass) 是变量。当我在脚本中明确指定密码时,它就起作用了。当我让它读取文件时,它失败了。

我什至在调用 DBI 之前构建了一个小测试来回显 $pass 变量,而且效果很好。它显示了正确的密码。

我确定我在做一些愚蠢的事情。帮忙?

环境:


~/mysql$ uname -a

Linux kbwm-radio-web 3.2.0-24-virtual #37-Ubuntu SMP Wed Apr 25 12:51:49 UTC 2012 i686 i686 i686 GNU/Linux

~/mysql$ perl --version

This is perl 5, version 18, subversion 2 (v5.18.2) built for i686-linux-gnu-thread-multi-64int
(with 41 registered patches, see perl -V for more detail)

~/mysql$ mysql -u phpuser -pcat ./phpuser.auth barret_test -e "status;"

mysql  Ver 14.14 Distrib 5.5.41, for debian-linux-gnu (i686) using readline 6.3

如您所见,在密码的mysql语句中使用命令扩展是有效的。

脚本(是的,部分内容不雅。稍后会修复):

#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $dsn = "dbi:mysql:barret_test:localhost:3306";
my $user = "phpuser";
my $pass = qx{cat /home/barret/mysql/phpuser.auth};

print "$pass\n";


my $dbh = DBI->connect(
$dsn,
$user,
$pass,
{ RaiseError => 1 },
) or die $DBI::errstr;

my $all = $dbh->selectall_arrayref("select name from users;");

foreach my $row (@$all) {
my ($name) = @$row;
print "$name\n";
}

$dbh->disconnect();

运行它返回:

~/mysql$ perl user_list.pl

*redacted_password*


DBI connect('barret_test:localhost:3306','phpuser',...) failed: Access denied for user 'phpuser'@'localhost' (using password: YES) at user_list.pl line 14.

如果我从

更改第 9 行
my $pass = qx{cat /home/barret/mysql/phpuser.auth};

my $pass = "redacted_password";

发生这种情况:

~/mysql$ perl user_list.pl

*redacted_password*

barret

richard

dave

我用 Google 搜索并在此处进行了搜索,没有发现任何似乎涵盖我的用例的内容。我尝试用单引号和双引号将 $pass 包裹在 DBI 调用中,没有任何变化。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com