Configure mysql mainly by calling CMD
There are three core steps:
1. Decompress mysql file in specified path by code (nuget gets sharpzipXXX, cross-volume copy writes recursive file copy by itself), and add the bin path of mysql to the path of system variable (Baidu search C# how to add environment variable)
2. Code creation database configuration file my.ini calls kernel.dll (searchable how to create INI file through C#)
3. Emphasis is put on calling cmd configuration database, which takes more time.
Code directly:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TestMysql { public class CmdHelper { public void DealTheWholeCommand(string _CommandLine) { bool StandOutMark = false; bool StandErrorMark = false; //FunctionDetail.ShowInfo showinfo = new FunctionDetail.ShowInfo(); Task task = new Task(() => { Process CmdProcess = new Process(); //Set up the application to start CmdProcess.StartInfo.FileName = "cmd.exe"; //Whether to start using operating system shell CmdProcess.StartInfo.UseShellExecute = false; // Accept input from the caller CmdProcess.StartInfo.RedirectStandardInput = true; //Output information CmdProcess.StartInfo.RedirectStandardOutput = true; // Output error CmdProcess.StartInfo.RedirectStandardError = true; //Do not display program window CmdProcess.StartInfo.CreateNoWindow = true; //Start the program CmdProcess.Start(); //Send input information to the cmd window //p.StandardInput.WriteLine(CommandLine + "&exit"); //Obtain output information Task taskForStandOut = new Task(()=> { string ResultLine = string.Empty; while (true) { Thread.Sleep(200); ResultLine = CmdProcess.StandardOutput.ReadLine(); if (!string.IsNullOrEmpty(ResultLine)) { Console.WriteLine(ResultLine); } if (CmdProcess.StandardOutput.EndOfStream) { //Console.WriteLine("Operation Execution Completed"); StandOutMark = true; break; } } }); taskForStandOut.Start(); Task taskForStandError = new Task(() => { string ResultLine2 = string.Empty; while (true) { Thread.Sleep(200); ResultLine2 = CmdProcess.StandardError.ReadLine(); if (!string.IsNullOrEmpty(ResultLine2)) { Console.WriteLine(ResultLine2); } if (CmdProcess.StandardError.EndOfStream) { //Console.WriteLine("Processing Completed"); StandErrorMark = true; break; } } }); taskForStandError.Start(); CmdProcess.StandardInput.WriteLine(_CommandLine + "&exit"); CmdProcess.StandardInput.AutoFlush = true; while (true) { if (StandErrorMark&&StandOutMark) { CmdProcess.WaitForExit(); CmdProcess.Close(); //Console.WriteLine("CMD closed"); CmdProcess = null; break; } Thread.Sleep(500); } }); task.Start(); task.Wait(); } } }
Above is the core code to execute the cmd configuration database command.
Called and specific commands:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestMysql { class Program { static void Main(string[] args) { CmdHelper cmdHelper = new CmdHelper(); cmdHelper.DealTheWholeCommand("mysqld --install"); cmdHelper.DealTheWholeCommand("mysqld --initialize-insecure --user=root --console"); cmdHelper.DealTheWholeCommand("net start mysql"); Console.ReadKey(); } } }
!!! Right-click administrator privilege operation can also be set up to automatically request administrator privileges or execute mysqld --install and net start mysql will prompt privilege issues
my.ini generated by the code is as follows:
[mysqld] basedir="C:\WorkSpace\Testsetup\UfaceOffline\Mysql" datadir="C:\WorkSpace\Testsetup\UfaceOffline\Mysql\data" port=3306 server_id=10 character-set-server=gbk character_set_filesystem=gbk [client] port=3306 default-character-set=gbk [mysqld_safe] timezone="CST" [mysql] default-character-set=utf8
The test system is win10. Before testing, Baidu can uninstall mysql (because it is installation-free version, it does not involve registry uninstallation, just uninstall mysql service and delete data folder in mysql root directory).
!!! insecure in the initialization statement will make the mysql user created do not contain a password, so login can be executed after connecting to the database without entering a password.
Alter user () identified by "123456" to modify password