How to Export & Import Existing User (with its Privileges!)

后端 未结 9 1980
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 17:01

I have an existing MySQL instance (test), containing 2 databases and a few users each having different access privileges to each database.

I now need to duplicate one of

9条回答
  •  别那么骄傲
    2021-02-03 17:38

    I tackled this with a small C# program. There is code here to generate a script or apply the grants directly from source to destination. If porting from a Windows -> *nix environment you may have to consider case sensitivity issues.

    using System;
    using MySql.Data.MySqlClient;
    using System.Configuration;
    using System.IO;
    using System.Collections.Generic;
    
    namespace GenerateUsersScript
    {
        class Program
        {
            static void Main(string[] args)
            {
                List grantsQueries = new List();
    
                // Get A Show Grants query for each user
                using (MySqlConnection sourceConn = OpenConnection("sourceDatabase"))
                {
                    using (MySqlDataReader usersReader = GetUsersReader(sourceConn))
                    {
                        while (usersReader.Read())
                        {
                            grantsQueries.Add(String.Format("SHOW GRANTS FOR '{0}'@'{1}'", usersReader[0], usersReader[1]));
                        }
                    }
    
                    Console.WriteLine("Exporting Grants For {0} Users", grantsQueries.Count);
    
                    using (StreamWriter writer = File.CreateText(@".\UserPermissions.Sql"))
                    {
                        // Then Execute each in turn 
                        foreach (string grantsSql in grantsQueries)
                        {
                            WritePermissionsScript(sourceConn, grantsSql, writer);
                        }
    
                        //using (MySqlConnection destConn = OpenConnection("targetDatabase"))
                        //{
                        //    MySqlCommand command = destConn.CreateCommand();
    
                        //    foreach (string grantsSql in grantsQueries)
                        //    {
                        //        WritePermissionsDirect(sourceConn, grantsSql, command);
                        //    }
                        //}
                    }
                }
    
                Console.WriteLine("Done - Press A Key to Continue");
    
                Console.ReadKey();
            }
    
            private static void WritePermissionsDirect(MySqlConnection sourceConn, string grantsSql, MySqlCommand writeCommand)
            {
                MySqlCommand cmd = new MySqlCommand(grantsSql, sourceConn);
    
                using (MySqlDataReader grantsReader = cmd.ExecuteReader())
                {
                    while (grantsReader.Read())
                    {
                        try
                        {
                            writeCommand.CommandText = grantsReader[0].ToString(); 
    
                            writeCommand.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(grantsReader[0].ToString());
    
                            Console.WriteLine(ex.Message);
                        }
                    }
                } 
            }
    
            private static void WritePermissionsScript(MySqlConnection conn, string grantsSql, StreamWriter writer)
            {
                MySqlCommand command = new MySqlCommand(grantsSql, conn);
    
                using (MySqlDataReader grantsReader = command.ExecuteReader())
                {
                    while (grantsReader.Read())
                    {
                        writer.WriteLine(grantsReader[0] + ";");
                    }
                }
    
                writer.WriteLine();
            }
    
            private static MySqlDataReader GetUsersReader(MySqlConnection conn)
            {
                string queryString = String.Format("SELECT User, Host FROM USER");
                MySqlCommand command = new MySqlCommand(queryString, conn);
                MySqlDataReader reader = command.ExecuteReader();
                return reader;
            }
    
            private static MySqlConnection OpenConnection(string connName)
            {
                string connectionString = ConfigurationManager.ConnectionStrings[connName].ConnectionString;
                MySqlConnection connection = new MySqlConnection(connectionString);
                connection.Open();
                return connection; 
    
            }
        }
    }
    

    with an app.config containing ...

        
            
            
         
    

提交回复
热议问题