<?php /** Feel free to improve it. * Original by Janich: https://gist.github.com/janich/6121771 * * @requires PHP 5.3+ * @package ExportMySQLUsers * @author Zaid Daba'een * @license http://www.dbad-license.org/ DBAD license */ // Set up database root credentials $host = 'localhost'; $user = 'root'; $pass = 'your_passwd'; // ---- Do not edit below this ---- // Misc settings header('Content-type: text/plain; Charset=UTF-8'); // Final import queries goes here $export = array(); // Connect to database try { $link = new PDO("mysql:host=$host;dbname=mysql", $user, $pass); } catch (PDOException $e) { printf('Connect failed: %s', $e->getMessage()); die(); } // Get users from database //$statement = $link->prepare("select `user`, `host`, `authentication_string` FROM `user`"); $statement = $link->prepare("select `user`, `host`, `password` FROM `user`"); $statement->execute(); while ($row = $statement->fetch()) { $user = $row[0]; $host = $row[1]; $pass = $row[2]; $export[] = 'CREATE USER \''. $user .'\'@\''. $host .'\' IDENTIFIED BY \''. $pass .'\''; // Fetch any permissions found in database $statement2 = $link->prepare('SHOW GRANTS FOR \''. $user .'\'@\''. $host .'\''); $statement2->execute(); while($row2 = $statement2->fetch()) { $export[] = $row2[0]; } } $link = null; echo implode(";\n", $export);
mysql --version
//$statement = $link->prepare("select `user`, `host`, `authentication_string` FROM `user`");
$statement = $link->prepare("select `user`, `host`, `password` FROM `user`");
CREATE USER 'root'@'localhost' IDENTIFIED BY '*MD5-HASH'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION; CREATE USER 'mysql.sys'@'localhost' IDENTIFIED BY '*MD5-HASH'; GRANT USAGE ON *.* TO 'mysql.sys'@'localhost'; GRANT TRIGGER ON `sys`.* TO 'mysql.sys'@'localhost'; GRANT SELECT ON `sys`.`sys_config` TO 'mysql.sys'@'localhost'; CREATE USER 'debian-sys-maint'@'localhost' IDENTIFIED BY '*MD5-HASH'; GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' WITH GRANT OPTION; CREATE USER 'your_user'@'localhost' IDENTIFIED BY '*MD5-HASH'; GRANT ALL PRIVILEGES ON `your_DB`.* TO 'your_user'@'localhost' WITH GRANT OPTION;
Source: https://habr.com/ru/post/328604/