备份/还原用户/密码/特权


16

我正在从一台服务器移动到另一台服务器,并且想从我的MySQL Server备份所有数据库+用户/特权/密码。我发现使用来备份数据库mysqldump,但是我不知道如何备份所有用户和给定的特权。有没有办法实现这一目标,还是必须在新服务器上重新进行设置?


您是否正在将数据移至另一台运行相同版本MySQL的服务器?
RolandoMySQLDBA 2015年

Answers:


16

“ mysql”数据库包含用户/特权/密码。因此,将mysql数据库与其他数据库一起转储

mysqldump [options] --all-databases > all_databases_dump.sql

mysqldump -u root -p mysql user > user_table_dump.sql

这些mysql数据库表包含授权信息

用户:用户帐户,全局特权和其他非特权列。

db:数据库级特权。

table_priv:表级特权。

columns_priv:列级特权。

procs_priv:存储过程和函数特权。

恢复交叉检查后

select Host, user, password from user ;

SHOW GRANTS FOR 'user'@'localhost';

7
警告。如果要将其加载到更新版本的MySQL中,则mysql.user由于架构更改,的转储可能会失败。
里克·詹姆斯

1
@RickJames:如果要迁移到新版本并还原用户,该怎么办?
brunoqc

1
mysql_upgrade是处理模式更改的脚本。但它希望您一次只进行一次重大更改,就地而不是重新加载。研究它。(对不起,我在升级方面没有经验。)
Rick James

1
恢复后,您可能/也将需要flush privileges;新的mysql。像mysql -u root -p -e'flush privileges;' 这样,可能/也将您在新服务器上的mysql根密码设置为旧服务器的root密码,因此请确保您知道这是什么。
meesern

0

该PHP脚本的灵感来自于需要执行与原始问题相同的操作,其中所讨论的服务器运行的是不同版本的MariaDB。由于它是PHP,因此可以在任何支持PHP(版本7.3或更高版本)的平台上运行。

<?php
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting(E_ALL);

//
// You will want to modify the 4 variables below for your environment
//

$dbuser       = 'root';                   // DB user with authority to SHOW GRANTS from mysql.user
$dbpassword   = 'blahblah';               // password for the DB user
$useroutfile  = '/temp/Users.sql';        // where to write the user file that may be imported on new server
$grantoutfile = '/temp/Grants.sql';       // where to write the grant file that may be imported on new server
$ignore_users = ['root','replication_user'];  // array of users that should NOT be exported

//
// There really should not be any reason to modify anything below this comment 
// but please do browse through it and understand what is being done
//

$dsn = 'mysql:host=localhost;charset=utf8mb4';
$opt = [PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION ,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC       ,
        PDO::ATTR_EMULATE_PREPARES   => true                   ,
       ];
try {

    $ourdb = new PDO ($dsn,$dbuser,$dbpassword,$opt);

} catch (PDOException $e) {

    error_log($e);  // log the error so it may be looked at later if necessary
    echo 'Could not connect to the SQL server';
    exit;
}  // end of the try/catch block

$notuser = implode(',',array_map('add_quotes',$ignore_users));

//
// We got connected to the database so now let's make sure we can open the
// output files for writing - note that using mode w will overwrite any
// existing files so we'll always start off cleanly
//

$userout = fopen($useroutfile,'w');

if ($userout === false) {  // could not open the output file for writing for some reason

    error_log('Could not open the output file for writing (' . $useroutfile . ')');
    exit;

}  // end of if we could not open the output file for writing

$grantout = fopen($grantoutfile,'w');

if ($grantout === false) {  // could not open the output file for writing for some reason

    error_log('Could not open the output file for writing (' . $grantout . ')');
    exit;

}  // end of if we could not open the output file for writing

$Query = $ourdb->query("
    SELECT CONCAT('SHOW GRANTS FOR ''', user, '''@''', host, ''';') AS query 
           FROM mysql.user 
           WHERE user NOT IN(" . implode(',',array_map('add_quotes',$ignore_users)) . ")
");
$users = $Query->fetchAll(PDO::FETCH_COLUMN);

foreach ($users as $GrantQ) {  // go through each of the users found

    $UserQ  = $ourdb->query("$GrantQ");  // retrieve the grants for a user
    $grants = $UserQ->fetchAll(PDO::FETCH_COLUMN);

    foreach ($grants as $grant) {  // go through each of the grants found for this user

        if (stripos($grant,'IDENTIFIED BY PASSWORD') === false) {

            fwrite($grantout,$grant . ';' . PHP_EOL);  // write the command to actually do the grant

        } else {

            fwrite($userout,$grant . ';' . PHP_EOL);  // write the command to actually do the grant
}
        }  // end of foreach through the grants found

}  // end of foreach through the queries to show the grants for each user

fwrite($userout ,'FLUSH PRIVILEGES;' . PHP_EOL);  // make sure SQL knows about the new users and privileges
fwrite($grantout,'FLUSH PRIVILEGES;' . PHP_EOL);  // make sure SQL knows about the new users and privileges
fclose($userout);   // close our output file
fclose($grantout);  // close our output file
echo 'The grants for ' . count($users) . ' users were written to ' . $useroutfile . PHP_EOL;

function add_quotes($str) {return sprintf("'%s'", $str);}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.