Mysql error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jain236
    New Member
    • Jul 2007
    • 36

    Mysql error

    Hi all i am new to the DBI .
    when i am trying to connect to mysql using perl script i am getting the following error

    Can't connect to data source '$dsn' because I can't work out what driver to use (it doesn't seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is not set)
    here is the way i am tryng to connect
    Code:
    use strict;
    
    use DBI;
    use DBD::mysql;
    
    my $drh = DBI->install_driver("mysql");
    
     my $dsn = 'DBI:mysql:my_database:localhost';
     my $dbh = DBI->connect('$dsn', 'root', '12345678',);
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Here is what I use for a default connection.

    Code:
    my %config_vars = (DBServerName  => 'localhost',
    					DBName 			   => 'xxxx',
    					DBUserName 		=> 'xxxx',
    					DBPassword 		=> 'xxxx',
    					);
    
    					
    my ($dbh, $data_source);
    my $dataSource = 'DBI:mysql:' . $config_vars{DBName} . ':' . $config_vars{DBServerName};
       $dbh = DBI->connect( $dataSource, $config_vars{DBUserName}, $config_vars{DBPassword}, {RaiseError=>1});
    --Kevin

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      I use connect() method as below:
      Code:
      $dsn = 'my_database';
      $host= 'localhost';
      my $dbh = DBI->connect( "dbi:mysql:database=$dsn;host=$host",'root','password') or die "Can't connect to mysql database: $DBI::errstr\n";

      Comment

      Working...