---
title: "linux下perl连接mysql数据库环境部署详解"
author: "ShellyLi"
date: "2011-12-05T10:58:41Z"
tags: []
categories: ["数据库"]
slug: "linux-perl-mysql"
draft: false
lastmod: "2011-12-05T10:58:41Z"
id: 1291930
url: "https://shellyli.iteye.com/blog/1291930"
---

环境说明:  
           
           系统：red-hat Enterprise 5
          
            数据库：mysql-5.1.35-linux-i686-glibc23二进制版
 
            perl mysql驱动程序:DBD-mysql-4.011.tar.gz
 
             DBI：  DBI-1.609.tar.gz           
             
             
            perl版本 ：5.8.8
 
            要求：在red-hat上部署环境，使得perl可以对mysql数据库进行操作.
 
 
一、首先安装mysql数据库,这个是前提,因为perl的mysql驱动程序需要引用mysql/bin下的mysql_config文件. 
         解压mysql-5.1.35-linux-i686-glibc23.tar.gz   可以查看下INSTALL-BINARY文件.里面详述了安装的方法，
 
创建一个分组、便于数据库的用户管理
shell> groupadd mysql      
添加用户
shell> useradd -g mysql mysql

shell> cd /usr/local
解压
shell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -
创建软连接
shell> ln -s full-path-to-mysql-VERSION-OS mysql
修改权限
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
导入数据库初始化脚本
shell> scripts/mysql_install_db --user=mysql
设置数据文件权限
shell> chown -R root .
shell> chown -R mysql data
启动mysql 
shell> bin/mysqld_safe --user=mysql &
 
你需要将你的mysql目录添加到环境变量PATH里
 
vi /etc/profile
 
在最后一行添加:
PATH=$PATH:/usr/local/mysql
export PATH
 
 
退出vi
 
source /etc/profile
 
mysql 配置完毕.
 
 
二、安装驱动程序
 
这个相对比较复杂,因为我安装的过程中一直报错,说"mysql.so"找不到,我搜了大量的论坛，也没找到解决方法，最后在mysql的官方网站这里找到了   http://dev.mysql.com/doc/refman/5.0/en/perl-support-problems.html
 
2.21.3. Problems Using the Perl DBI/DBD InterfaceIf Perl reports that it cannot find the ../mysql/mysql.so module, the problem is probably that Perl cannot locate the libmysqlclient.so shared library. You should be able to fix this problem by one of the following methods: 
•Compile the DBD::mysql distribution with perl Makefile.PL -static -config rather than perl Makefile.PL. 
•Copy libmysqlclient.so to the directory where your other shared libraries are located (probably /usr/lib or /lib). 
•Modify the -L options used to compile DBD::mysql to reflect the actual location of libmysqlclient.so. 
•On Linux, you can add the path name of the directory where libmysqlclient.so is located to the /etc/ld.so.conf file. 
•Add the path name of the directory where libmysqlclient.so is located to the LD_RUN_PATH environment variable. Some systems use LD_LIBRARY_PATH instead. 
第一步：我没执行成功，可能是因为版本问题
 
第二步：我使用  find / -name "libmysqlclient.so "   命令查找到了这个文件的路径
 
                  然后将其copy到 /usr/lib下
 
第三步：不需要做。
 
第四步:将 /usr/lib   添加到  /etc/ld.so.conf 文件的末尾 （事实上在这个系统里这句没有起作用，起作用的是下面这步）
 
第五步：LD_LIBRARY_PATH =$LD_LIBRARY_PATH :/usr/lib
 
                  export      LD_LIBRARY_PATH
 
现在你可以正确得安装你的驱动程序了:
 
gzip -d  DBD-mysql-4.011.tar.gz
 
tar -xvf  DBD-mysql-4.011.tar
 
cd  DBD-mysql-4.011
 
perl    Makefile.PL
 
make
 
make test
 
make install
 
搞定.
 
 
 

三：安装DBI
 
         gunzip < DBI-1.609.tar.gz   | tar xvf -
 
         perl   Makefile.PL
        
         make
 
         make test
         
          make install
 
完成
 
 
 
下面就来试试吧.我们来写一段perl程序链接mysql数据库:(假定你有一个数据库名称叫做test)
 
vi  test
 
#! /usr/bin/perl -w
use DBI;
my $dbh = DBI->connect("DBI:mysql:database=test:host=localhost","mysql","", {"RaiseError" => 1});
$dbh->disconnect();
 
如果没有报什么错误,说明配置成功了.呵呵