如何使perl DBI中的mysql_read_default_group工作

问题描述 投票:0回答:2

我正在尝试使用 my.ini 文件作为配置来连接 mysql。

这是我的 Perl 代码:

use DBI;

my $dsn = "DBI:mysql:umls;mysql_read_default_group=client;";

my $dbh = DBI->connect($dsn);

这是 my.ini 文件:

...

[client]

host=localhost

user=root

password=mypassword

database=umls

port=3306

....

根据官方文档(http://search.cpan.org/~capttofu/DBD-mysql-4.033/lib/DBD/mysql.pm), DBI将使用my.ini中的信息连接mysql,但是,它给我错误,如:errors

我认为问题是 DBI 从未成功读取 my.ini,因此它使用 ODBC@localhost 作为默认值。

如果我只在 connect() 中指定用户名和密码,连接就会工作。 谁能帮我弄清楚为什么

'mysql_read_default_group=client'
不起作用? 我将非常感激! 谢谢

mysql perl ini dbi dbd
2个回答
0
投票

我阅读了一些文档来回答您的问题,我发现您需要在不同部分提供用户名和密码,并且还需要提及文件名。喜欢

[myapp]
host=localhost

user=root

password=mypassword

database=umls

port=3306

然后

my $dbh = DBI->connect("DBI:mysql:test"
    . ";mysql_read_default_file=$ENV{HOME}/.my.cnf"
    .';mysql_read_default_group=myapp',
    undef, 
    undef
   ) or die "something went wrong ($DBI::errstr)";

希望这对您有帮助。

参考: http://www.perlmonks.org/?node_id=519356


0
投票

我知道这是一个老问题,但我想我会回答,因为没有特定于模块的答案。我在使用 UMLS::Interface 模块时也遇到了此错误,并且能够修复它。

您遇到此错误的原因是 DBI 模块无法识别您在 my.cnf 文件中设置了默认 UMLS 数据库名称。如果发生这种情况,UMLS::Interface 模块的作者希望有一个名为“umls”(区分大小写)的数据库。就我而言,数据库称为“UMLS”。

此问题的两种解决方案:

解决方案1)编辑UMLS::Interface CuiFinder.pm脚本

转到您的 UMLS 接口安装目录

#Location on Ubuntu 22.04
cd /usr/local/share/perl/5.34.0/UMLS/Interface
sudo nano CuiFinder.pm
#Ctrl + W
#Ctrl + T 
#Enter 2464, which will send you to line 2464
#Enter user password 

在这里你会看到一行代码:

my $dsn = "DBI:mysql:umls;mysql_read_default_group=client;"

这是当 mysql 设置中的 my.cnf 无法识别或没有客户端设置时执行的代码(请参阅解决方案#2)。在这里,您需要将“umls”更改为您的数据库名称。就我而言,我创建了名为“UMLS”的数据库,因此在我的情况下,该行现在显示为:

my $dsn = "DBI:mysql:UMLS;mysql_read_default_group=client;"

您现在应该能够完成脚本而不会出现错误。

解决方案 2) 编辑 mysql my.cnf 客户端设置 进一步阅读您的帖子后,我注意到您已经编辑了您的 .ini 文件,因此看来解决方案 1 可能是任何尝试使其工作的人的最佳选择。

您还可以尝试让 DBI 模块识别您的默认数据库是 UMLS 数据库,而不是更改 UMLS:Interface 模块中的 perl 代码。我无法让它在运行 mysql Ver 8.0.36-0ubuntu0.22.04 的 Ubuntu 22.04 上工作。 DBI 坚持说我没有默认数据库,这很奇怪,因为它识别默认用户和密码才能访问数据库。

转到您的 my.cnf 文件位置。

cd /etc/mysql

编辑我的 my.cnf 文件(注意默认情况下这是只读的)

sudo nano my.cnf

将以下内容复制并粘贴到文件中:

[client]
user=<your username here>
password=<your user's password>
port=3306 #this is the default but can also check this in mysql with SHOW VARIABLES LIKE '%port%;'
socket=/var/run/mysqld/mysqld.sock #this is the default but can also check this in mysql with 'SHOW VARIABLES LIKE '%socket%;''
database=<your database name>

理论上,这应该允许 CuiFinder.pm 脚本执行 if else 语句的第一个子句并拉入默认数据库,而无需编辑 perl 代码。就像我说的,DBI 没有意识到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.