Guest User

Untitled

a guest
Sep 20th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.09 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use lib '/var/www/imscp/engine/PerlLib';
  6. use iMSCP::Bootstrapper;
  7. use iMSCP::Database;
  8. use iMSCP::Debug;
  9. use Servers::sqld;
  10.  
  11. iMSCP::Bootstrapper->getInstance()->boot();
  12.  
  13. my $password = shift;
  14. defined $password && $password ne '' or fatal('You must provide a password for the SQL root user');
  15.  
  16. my $sqlServer = Servers::sqld->factory();
  17. my $db = iMSCP::Database->factory();
  18.  
  19. for my $host('localhost', '127.0.0.1', '::1') {
  20.     my $qrs = $db->doQuery( 1, 'SELECT 1 FROM mysql.user WHERE user = ? AND host = ?', 'root', $host );
  21.     ref $qrs eq 'HASH' or fatal( $qrs );
  22.  
  23.     if(%{$qrs}) {
  24.         $qrs = $db->doQuery( 'd', 'DROP USER ?@?', 'root', $host );
  25.         ref $qrs eq 'HASH' or fatal( sprintf( 'Could not drop the %s@%s SQL user: %s', 'root', $host, $qrs ) );
  26.     }
  27.  
  28.     $sqlServer->createUser('root', $host, $password);
  29.  
  30.     $qrs = $db->doQuery( 'g', 'GRANT ALL ON *.* TO ?@? WITH GRANT OPTION', 'root', $host );
  31.     unless (ref $qrs eq 'HASH') {
  32.         fatal( sprintf( 'Could not GRANT privileges: %s', $qrs ) );
  33.     }
  34. }
  35.  
  36. 1;
  37. __END__
Add Comment
Please, Sign In to add comment