MYSQL是老牌关系型数据库,在受够了sqlite,mslocaldb,sqlce等本地数据库之后,发现了mysql5.6的一些版本也可以绿色安装,编程实现从资源文件里面解压到目标机器上,并配置好成为本机系统服务。并且EF的mysql驱动对code first支持非常好。于是探索出了用mysql来做本地数据库的方法。
my.ini配置
[client]
port=3308
[mysql]
default-character-set=gbk
[mysqld]
port=3308
basedir="%BaseDir%"
datadir="%BaseDir%data/"
character-set-server=gbk
default-storage-engine=MyISAM
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
max_connections=512
query_cache_size=0
table_cache=256
tmp_table_size=18M
thread_cache_size=8
myisam_max_sort_file_size=64G
myisam_sort_buffer_size=35M
key_buffer_size=25M
read_buffer_size=256K
read_rnd_buffer_size=64M
sort_buffer_size=256M
innodb_additional_mem_pool_size=24M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=12M
innodb_buffer_pool_size=47M
innodb_log_file_size=24M
innodb_thread_concurrency=8
从资源文件里面解压mysql
/// <summary> /// 检查MySQL服务是否运行,如果没有安装MySQL自动解压并初始化 /// </summary> private static void InitMysqlService() { WaitUI.WorkMessage = "第一次使用系统,正在初始化本地数据库..."; byte[] zipfile = (byte[])Properties.Resources.ResourceManager.GetObject("MySQLx86"); System.IO.File.WriteAllBytes("MySQLx86.zip", zipfile); UnZip("MySQLx86.zip", "", "", true); #region 初始化MySQL try { string physicalRoot = AppDomain.CurrentDomain.BaseDirectory + "MySQLx86\"; //1.修改my.ini配置 为防止本机已装mysql,特修改my.ini中端口号为3308 string iniFile = System.IO.File.ReadAllText(physicalRoot + "my.ini"); iniFile = iniFile.Replace("%BaseDir%", physicalRoot.Replace("\", "/")); //%BaseDir%为my.ini中自定义的目录参数 System.IO.File.WriteAllText(physicalRoot + "my.ini", iniFile); //2.创建win服务 string info1 = Execute(physicalRoot + "bin\mysqld.exe" + " install MySQLd --defaults-file="" + physicalRoot + "my.ini"", 0); Debug.WriteLine(info1); //3.启动服务 string info2 = Execute("net start MySQLd", 0); Debug.WriteLine(info2); } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { System.IO.File.Delete("MySQLx86.zip"); ServiceController serviceController = ServiceController.GetServices().Where(sc => sc.ServiceName.Equals("MySQLd")).FirstOrDefault(); if (serviceController != null && serviceController.Status != ServiceControllerStatus.Running) { serviceController.Start(); } } #endregion }
/// <summary> /// 解压文件 /// </summary> /// <param name="zipedFile"></param> /// <param name="strDirectory"></param> /// <param name="password"></param> /// <param name="overWrite"></param> private static void UnZip(string zipedFile, string strDirectory, string password, bool overWrite) { if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\")) strDirectory = strDirectory + "\"; using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(zipedFile))) { s.Password = password; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = ""; string pathToZip = ""; pathToZip = theEntry.Name; if (pathToZip != "") directoryName = Path.GetDirectoryName(pathToZip) + "\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "") { if ((System.IO.File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!System.IO.File.Exists(strDirectory + directoryName + fileName))) { using (FileStream streamWriter = System.IO.File.Create(strDirectory + directoryName + fileName)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, size); else break; } streamWriter.Close(); } } } } s.Close(); } }