gpt4 book ai didi

mysql - Perl DBD 无法连接到 64 位机器上的 MySQL

转载 作者:可可西里 更新时间:2023-11-01 08:36:46 24 4
gpt4 key购买 nike

我在 RHEL 5.5 64 位 机器上。

我安装了 ActivePerl机器上5.10 64位,升级之前内置的Perl 5.8 64位。我已启动并运行 MySQL,并且我的 PHP 项目能够访问它。我的 Perl 文件需要使用 DBD 访问同一个数据库,但它无法做到这一点。我已经验证:

  1. 我的 MySQL 服务已启动并正在运行。
  2. 我的用户存在并且数据库和数据确实存在。
  3. 我能够通过 MySQL 客户端 shell 访问数据库中的数据。

以下是我的 Perl 脚本。

#!/usr/bin/perl

use DBI;


$dbh = DBI->connect( "DBI:mysql:go:super218:3306","root","NEWPASSWORD" ) or die "Couldn't connect to database: " . DBI->errstr;

my $sth = $dbh->prepare( "SELECT * FROM phones" )
or die "Can't prepare SQL statement: $DBI::errstr\n";

$sth->execute or die "executing: $stmtA ", $dbh->errstr;

my @row;
while ( @row = $sth->fetchrow_array( ) ) {
print "Row: @row\n";
}

使用正确的用户名和密码时出现以下错误:

DBI connect('go:super218:3306','root',...) failed: (no error string) at testdb.pl line 6
Couldn't connect to database: at testdb.pl line 6.

我收到以下错误,用户或密码不正确:

DBI connect('go:super218:3306','root1',...) failed: Access denied for user 'root1'@'localhost' (using password: YES) at testdb.pl line 6
Couldn't connect to database: Access denied for user 'root1'@'localhost' (using password: YES) at testdb.pl line 6.

我该如何解决这个问题?我想问题出在 MySQL 的末端。

最佳答案

一般在数据库连接字符串、密码字符串和sql查询中使用单引号,因为使用双引号可能会出错。因为双引号用于interpolation。 .

我认为您应该这样写。

#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use DBD::mysql;

my $dbh = DBI->connect( 'DBI:mysql:go;host=super218','root','NEWPASSWORD' ,{ RaiseError => 1 } )or die "Couldn't connect to database";
my $sth = $dbh->prepare( 'SELECT * FROM phones');

$sth->execute;

while ( my @row = $sth->fetchrow_array() ) {
print "Row: @row\n";
}

关于mysql - Perl DBD 无法连接到 64 位机器上的 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782053/

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