mysql 命令行安装并给用户sa所有权限
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace mysql安装
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// System.IO.Compression.ZipFile.ExtractToDirectory("mysql-5.7.43-winx64.zip", "d:\\mysql");
////安装
//{
// Process cmd = new Process();
// cmd.StartInfo.FileName = "cmd.exe";
// cmd.StartInfo.WorkingDirectory = @"D:\mysql\mysql-5.7.43-winx64\bin";
// cmd.StartInfo.RedirectStandardInput = true;
// cmd.StartInfo.RedirectStandardOutput = true;
// cmd.StartInfo.CreateNoWindow = true;
// cmd.StartInfo.UseShellExecute = false;
// cmd.Start();
// cmd.StandardInput.WriteLine(@"mysqld --install");
// cmd.StandardInput.Flush();
// cmd.StandardInput.Close();
// cmd.WaitForExit();
// Debug.WriteLine(cmd.StandardOutput.ReadToEnd());
//}
////初始化
//{
// Process cmd = new Process();
// cmd.StartInfo.FileName = "cmd.exe";
// cmd.StartInfo.WorkingDirectory = @"D:\mysql\mysql-5.7.43-winx64\bin";
// cmd.StartInfo.RedirectStandardInput = true;
// cmd.StartInfo.RedirectStandardOutput = true;
// cmd.StartInfo.CreateNoWindow = true;
// cmd.StartInfo.UseShellExecute = false;
// cmd.Start();
// cmd.StandardInput.WriteLine(@"mysqld --initialize-insecure");
// cmd.StandardInput.Flush();
// cmd.StandardInput.Close();
// cmd.WaitForExit();
// Debug.WriteLine(cmd.StandardOutput.ReadToEnd());
//}
//{
// Process cmd = new Process();
// cmd.StartInfo.FileName = "cmd.exe";
// cmd.StartInfo.WorkingDirectory = @"D:\mysql\mysql-5.7.43-winx64\bin";
// cmd.StartInfo.RedirectStandardInput = true;
// cmd.StartInfo.RedirectStandardOutput = true;
// cmd.StartInfo.CreateNoWindow = true;
// cmd.StartInfo.UseShellExecute = false;
// cmd.Start();
// cmd.StandardInput.WriteLine(@"mysqld --initialize");
// cmd.StandardInput.Flush();
// cmd.StandardInput.Close();
// cmd.WaitForExit();
// Debug.WriteLine(cmd.StandardOutput.ReadToEnd());
//}
//{
// using (MySqlConnection Connection = new MySqlConnection("SERVER=localhost;UID=root;"))
// using (MySqlCommand Command = new MySqlCommand("SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypassword');", Connection))
// {
// Connection.Open();
// Command.ExecuteNonQuery();
// }
//}
{
using (MySqlConnection Connection = new MySqlConnection("SERVER=localhost;UID=root;PWD=mypassword"))
{
var sql = "CREATE USER 'sa'@'%' IDENTIFIED BY '123456' ;GRANT ALL PRIVILEGES ON *.* TO 'sa'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;";
using (MySqlCommand Command = new MySqlCommand(sql, Connection))
{
Connection.Open();
Command.ExecuteNonQuery();
}
}
}
// var str= "D:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqld-nt\" --install MySQL --defaults-file=\"D:\\Program Files\\MySQL\\MySQL Server 5.0\\my.ini\""
}
}
}