perl 链接lampp的mysql数据库 mac 系统

perl -v
mac 系统自带
安装mysql
DBI

use strict;
use DBI;
 
my $host = "localhost";         # 主机地址
my $driver = "mysql";           # 接口类型 默认为 localhost
my $database = "crm";        # 数据库
# 驱动程序对象的句柄
my $dsn = "DBI:$driver:database=$database:$host";  
my $userid = "urldb";            # 数据库用户名
my $password = "123456";        # 数据库密码
 
# 连接数据库
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT * FROM fish_cash");   # 预处理 SQL  语句
$sth->execute();    # 执行 SQL 操作
 
# 注释这部分使用的是绑定值操作
# $alexa = 20;
# my $sth = $dbh->prepare("SELECT name, url
#                        FROM Websites
#                        WHERE alexa > ?");
# $sth->execute( $alexa ) or die $DBI::errstr;
 
# 循环输出所有数据
while ( my @row = $sth->fetchrow_array() )
{
       print join('\t', @row)."\n";
}
 
$sth->finish();
$dbh->disconnect();

安装完毕,执行依然不行
Mac下配置perl的DBD::MySQL模块
perl -MCPAN -e shell
install模块
终端依次输入 自动命令行
perl -MCPAN -e shell
cpan[1]> install DBD::mysql
cpan[2]> exit

export DYLD_LIBRARY_PATH=”/usr/local/mysql/lib:$DYLD_LIBRARY_PATH”

/Applications/XAMPP/xamppfiles/lib/mysql/plugin/

find ./ -iname ‘mysqlclient’

export DYLD_LIBRARY_PATH=”/Applications/XAMPP/xamppfiles/bin:$DYLD_LIBRARY_PATH”

我的机器一直用XAMPP的mysql 发现是个负担,很多教程都直接用usr/local/bin的mysql
比较容易

/Applications/XAMPP/xamppfiles/bin

/Applications/XAMPP/xamppfiles/bin/mysql_config

perl Makefile.PL –mysql_config=/Applications/XAMPP/xamppfiles/bin/mysql_config

perl Makefile.PL -mysql_config=/Applications/XAMPP/xamppfiles/bin/mysql_config -testuser=root -testpassword=dbadmin

perl -MCPAN -e “install DBD::mysql”

You can also optionally set the user to run ‘make test’ with:

perl Makefile.PL –testuser=username

Can’t exec “mysql_config”: No such file or directory at Makefile.PL line 561.
Can’t find mysql_config. Use –mysql_config option to specify where mysql_config is located
Failed to determine directory of mysql.h. Use

因为我的mysql在xampp下
make test perl Makefile.PL –testuser=username

无功劳

这个方案是最后成功了的方案

下载这个玩意

http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.038.tar.gz

perl Makefile.PL –mysql_config=/Applications/XAMPP/xamppfiles/bin/mysql_config –testuser=urldb

perl Makefile.PL –mysql_config=/Applications/XAMPP/xamppfiles/bin/mysql_config –testuser=urldb –testpassword=123456

install_driver(mysql) failed: Can’t load ‘/Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle’ for module DBD::mysql: dlopen(/Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle, 1): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle
Reason: unsafe use of relative rpath libmysqlclient.18.dylib in /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle with restricted binary at /System/Library/Perl/5.18/darwin-thread-multi-2level/DynaLoader.pm line 194.

找到个这个文件
/Applications/XAMPP/xamppfiles/lib/libmysqlclient.18.dylib

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle

改成我自己的

otool -L /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle

sudo install_name_tool -change libmysqlclient.18.dylib /Applications/XAMPP/xamppfiles/lib/libmysqlclient.18.dylib /Library/Perl/5.18/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle

好了。


This entry was posted in Linux. Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/perl-mysql-mac/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

*

  

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

About Me

静水流深,水滴石穿