Script para Exportar usuarios e permissoes MySQL

<?php
// Exporta usuarios e permissoes MySQL

$host = 'localhost';
$user = 'root';
$pass = 'toor';

header('Content-type: text/plain; Charset=UTF-8');
$export = array();
$link = new mysqli($host, $user, $pass, 'mysql', 3306);

if ($link->connect_error) {
printf('Connect failed (%s): %s', $link->connect_errno, $link->connect_error);
die();
}

$link->query('SET NAMES \'utf8\'');
$result = $link->query('SELECT `User`, `Host`, `Password` FROM `user`');

if ($result) {
while ($row = $result->fetch_row()) {
$user = $row[0];
$host = $row[1];
$pass = $row[2];

$export[] = 'CREATE USER `'. $user .'`@`'. $host .'` IDENTIFIED BY \''. $pass .'\'';

// Fetch any permissions found in database
$result2 = $link->query('SHOW GRANTS FOR `'. $user .'`@`'. $host .'`');

if ($result2) {
while ($row2 = $result2->fetch_row()) {
$export[] = $row2[0];
} // while ($row2...
} // if ($result2)...
} // while ($row...
}

$link->close();
echo implode(";\n", $export);
?>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s