http://qs321.pair.com?node_id=1223430

Perl300 has asked for the wisdom of the Perl Monks concerning the following question:

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.