Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Greetings monks!

I am trying to insert around 30-50K rows using DBI into a MySQL DB (5.5.56-MariaDB). I am trying to use following code (say insert1.pl) which is working for almost 6-7K rows.

#!/usr/bin/perl -w use strict; use DBI; use Data::Dumper; use POSIX qw( strftime ); my $database = 'db_name'; my $db_user = 'user'; my $db_password = 'pwd'; my $db_hostname = 'db_hostname'; my $dbh2 = DBI->connect("DBI:mysql:database=$database:host=$db_hostnam +e",$db_user,$db_password, { RaiseError => 1, AutoCommit => 1, mysql_auto_reconnect => 1 } # +Added AutoCommit => 1, mysql_auto_reconnect => 1 while trying to make + it work ); die "unable to connect to server $DBI::errstr" unless $dbh2; my $sql2 = q^ INSERT INTO table_name (col_1, col_2, col_3, col_4, col_5) VALUES ^; while(my @row = $sth1->fetchrow_array){ if ($row[3]) { $sql2 .= " ('" . $row[0] . "', '" . $row[1] . "', '" +. $row[2] . "', '" . $row[3] . "', '" . $row[4] . "'),\n"; } else { $sql2 .= " ('" . $row[0] . "', '" . $row[1] . "', '" +. $row[2] . "', null, '" . $row[4] . "'),\n"; } } $sth1->finish(); $sql2 .= " ('last_dummy', 'last_dummy', 'last_dummy', 'last_d +ummy', 'last_dummy')\n"; print localtime() . "\tThe sql2:\n$sql2\n"; my $sth2 = $dbh2->prepare($sql2); sleep 10; # Added this later while trying to make it work $sth2->execute(); # I get for execute. sleep 10; # Added this later while trying to make it work $sth2->finish(); my $sql3 = "DELETE FROM table_name WHERE col_1 = 'last_dummy'"; print localtime() . "\tThe sql3:\n$sql3\n"; my $sth3 = $dbh2->prepare($sql3); $sth3->execute(); $sth3->finish(); $dbh2->disconnect();

But when I try to insert all the rows, which at present are 27K, I see error in log like:

('host1', '123.456.789', 'string1', 'string4', 'Root (root@l +ocalhost) (configure /etc/snmp/snmp.local.conf)'), ('host2', '123.456.743', 'string2', 'string5', 'Root (root@l +ocalhost) (configure /etc/snmpDBD::mysql::st execute failed: MySQL se +rver has gone away at insert1.pl line 38. DBD::mysql::st execute failed: MySQL server has gone away at insert1.p +l line 38. /snmp.local.conf)'), ('host3', '499.456.789', 'string3', 'string6', 'Root (root@l +ocalhost) (configure /etc/snmp/snmp.local.conf)'),

I have tried to add option for AutoCommit and auto_reconnect as I got those as suggetions while searching for reason behind the error MySQL server has gone away at

I did try to super search here for 'bulk insert', 'mysql bulk' & 'DBI insert' but didn't get what I am looking for. I also tried use diagnostics but it adds only one line in the error: Uncaught exception from user code: Issuing rollback() due to DESTROY without explicit disconnect() of DBD::mysql::db handle database=telemetry:host=<IP> at insert1.pl line 38.

I am pretty sure that there is better way to do this than how I am doing it here.

UPDATE_1:Added note after trying use diagnostics.

UPDATE_2:The 'Root (root@localhost) (configure /etc/snmp/snmp.local.conf)' is coming from data. So that is not the problem here.

UPDATE_3: Updated title to mark it as SOLVED.


In reply to [SOLVED]: How to insert 30-50K rows into MySQL DB using DBI? by Perl300

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found