Просмотр исходного кода

update utf8 to utf8mb4 for support last mysql

rajven 4 лет назад
Родитель
Сommit
57e180ed8f
3 измененных файлов с 29 добавлено и 31 удалено
  1. 20 26
      scripts/Rstat/mysql.pm
  2. 2 3
      scripts/parse_flow.pl
  3. 7 2
      updates/db-patch-mysql-utf8.sh

+ 20 - 26
scripts/Rstat/mysql.pm

@@ -122,20 +122,20 @@ my $sth;
 if (ref($batch_sql) eq 'ARRAY') {
     foreach my $sSQL (@$batch_sql) {
         next if (!$sSQL);
-        $sth = $db->prepare($sSQL);
-        $sth->execute;
+        $sth = $db->prepare($sSQL) or die "Unable to prepare $sSQL" . $db->errstr;
+        $sth->execute() or die "Unable to prepare $sSQL" . $db->errstr;
         $apply = 1;
         }
     } else {
     my @msg = split("\n",$batch_sql);
     foreach my $sSQL (@msg) {
         next if (!$sSQL);
-        $sth = $db->prepare($sSQL);
-        $sth->execute;
+        $sth = $db->prepare($sSQL) or die "Unable to prepare $sSQL" . $db->errstr;
+        $sth->execute() or die "Unable to prepare $sSQL" . $db->errstr;
         $apply = 1;
         }
     }
-if ($apply) { $sth->finish; }
+if ($apply) { $sth->finish(); }
 $db->{AutoCommit} = 1;
 }
 
@@ -143,9 +143,8 @@ $db->{AutoCommit} = 1;
 
 sub batch_db_sql_cached {
 
-my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 1, AutoCommit => 0 });
+my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 0, AutoCommit => 0 });
 if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
-$db->do('SET NAMES utf8');
 $db->{mysql_auto_reconnect} = 1;
 
 my $table= shift;
@@ -153,11 +152,11 @@ my $batch_sql=shift;
 
 return if (!$db);
 if (ref($batch_sql) eq 'ARRAY') {
-    my $sth = $dbh->prepare_cached($table);
+    my $sth = $db->prepare_cached($table) or die "Unable to prepare:" . $db->errstr;
     db_log_debug($db,"Start prepare data");
     foreach my $sSQL (@$batch_sql) {
         next if (!$sSQL);
-        $sth->execute(@$sSQL);
+        $sth->execute(@$sSQL) or die "Unable to execute:" . $db->errstr;
         }
     db_log_debug($db,"End prepare data");
     }
@@ -175,12 +174,11 @@ my $sql=shift;
 return if (!$db);
 return if (!$sql);
 if ($sql!~/^select /i) { db_log_debug($db,$sql); }
-my $sql_prep = $db->prepare($sql);
+my $sql_prep = $db->prepare($sql) or die "Unable to prepare $sql: " . $db->errstr;
 my $sql_ref;
-if ( !defined $sql_prep ) { die "Cannot prepare statement: $DBI::errstr\n"; }
-$sql_prep->execute;
+$sql_prep->execute() or die "Unable to execute $sql: " . $db->errstr;
 if ($sql=~/^insert/i) { $sql_ref = $sql_prep->{mysql_insertid}; }
-if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref(); };
+if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref() or die "Unable to select $sql: " . $db->errstr; };
 $sql_prep->finish();
 return $sql_ref;
 }
@@ -202,8 +200,8 @@ if ($level eq $L_WARNING and $log_level >= $L_WARNING) { log_warning($msg); $db_
 if ($level eq $L_INFO and $log_level >= $L_INFO) { log_info($msg); $db_log = 1; }
 if ($level eq $L_DEBUG and $log_level >= $L_DEBUG) { log_debug($msg); $db_log = 1; }
 if ($db_log) {
-    my $history_rf=$db->prepare($history_sql);
-    $history_rf->execute;
+    my $history_rf=$db->prepare($history_sql) or die "Unable to prepare $history_sql:" . $db->errstr;
+    $history_rf->execute() or die "Unable to execute $history_sql: " . $db->errstr;
     }
 }
 
@@ -258,7 +256,7 @@ if ($log_level >= $L_WARNING) {
 
 sub init_db {
 # Create new database handle. If we can't connect, die()
-my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
+my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 0, AutoCommit => 1 });
 if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
 $db->do('SET NAMES utf8mb4');
 $db->{mysql_auto_reconnect} = 1;
@@ -303,12 +301,9 @@ my $table = shift;
 my @result;
 return @result if (!$db);
 return @result if (!$table);
-my $list = $db->prepare( $table );
-if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
-$list->execute;
-while(my $row_ref = $list ->fetchrow_hashref) {
-push(@result,$row_ref);
-}
+my $list = $db->prepare( $table ) or die "Unable to prepare $table:" . $db->errstr;
+$list->execute() or die "Unable to execute $table: " . $db->errstr;
+while(my $row_ref = $list->fetchrow_hashref()) { push(@result,$row_ref); }
 $list->finish();
 return @result;
 }
@@ -321,10 +316,9 @@ my $tsql = shift;
 my @result;
 return @result if (!$db);
 return @result if (!$tsql);
-my $list = $db->prepare( $tsql . ' LIMIT 1' );
-if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
-$list->execute;
-my $row_ref = $list ->fetchrow_hashref;
+my $list = $db->prepare( $tsql . ' LIMIT 1' ) or die "Unable to prepare $tsql: " . $db->errstr;
+$list->execute() or die "Unable to execute $tsql: " . $db->errstr;
+my $row_ref = $list ->fetchrow_hashref();
 $list->finish();
 return $row_ref;
 }

+ 2 - 3
scripts/parse_flow.pl

@@ -21,10 +21,9 @@ use Rstat::net_utils;
 use Rstat::mysql;
 use Parallel::ForkManager;
 
-my $router_id;
-if (scalar @ARGV>1) { $router_id=shift(@ARGV); } else { $router_id=$ARGV[0]; }
+if (!$ARGV[0]) { exit 110; }
 
-if (!$router_id) { exit 110; }
+my $router_id=$ARGV[0];
 
 my $fork_count = $cpu_count*10;
 

+ 7 - 2
updates/db-patch-mysql-utf8.sh

@@ -4,5 +4,10 @@ echo "Run in mysql console:"
 
 dbname=$1
 
-mysql -u root -p ${dbname} -B -N -e "SHOW TABLES" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}'
-echo "ALTER DATABASE ${dbname} CHARACTER SET utf8 COLLATE utf8_general_ci;"
+mysql -u root -p ${dbname} -B -N -e "SHOW TABLES" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; SET foreign_key_checks = 1; "}' >migration_utf8
+echo "ALTER DATABASE ${dbname} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;">>migration_utf8
+
+mysql -u root -p ${dbname} < migration_utf8
+rm -f migration_utf8
+
+exit