/// <summary> ///install and turn on services/// </summary> Public Static BOOLInitandstartservice (stringServiceName,stringMysqlinipath,stringMysqlpath,stringMysqlbinpath,stringMysqldatapath) { Try{a_initdatabase (Mysqlbinpath); B_resetmysqlconfig (Mysqlinipath, Mysqlpath, Mysqldatapath); C_installservice (Mysqlbinpath, ServiceName, Mysqlinipath); D_startservice (ServiceName); E_setdefaultpassword (Mysqlinipath, API. Mysqlpassword); } Catch(Exception ex) {Api.logger.addToLog (ex,"Initandstartservice Method Error! ");return false; } return true; } /// <summary> ///initializes the database, and if initialization has already occurred, it is no longer initialized, notice that the initialization is complete with a blank password. /// </summary> /// <param name= "Mysqlbinpath" ></param> /// <returns></returns> Public Static voidA_initdatabase (stringMysqlbinpath) { if(!directory.exists (API. Mysqldatapath)) {Try{Directory.CreateDirectory (API. Mysqldatapath); }Catch { } } //Create a new initial database varLog = exec. Runcmd (Mysqlbinpath,"mysqld--initialize-insecure--console"). Trim ();//--user=mysqlApi.logger.addToLog ("a_initdatabase method log: \ r \ n"+log); } /// <summary> ///configures the database INI file, which is called before the service is started after initialization. /// </summary> Public Static voidB_resetmysqlconfig (stringMysqlinipath,stringMysqlpath,stringMysqldatapath) {Inihelper ini=NewInihelper (Mysqlinipath); Ini. Write ("mysqld","Basedir", (Mysqlpath +""). TrimEnd ('\\')); Ini. Write ("mysqld","DataDir", (Mysqldatapath +""). TrimEnd ('\\')); } /// <summary> ///the installation service will perform an uninstall reload. /// </summary> Public Static voidC_installservice (stringMysqlbinpath,stringServiceName,stringMysqlinipath) {Exec. Runcmd (Mysqlbinpath,string. Format ("mysqld.exe-remove {0}", ServiceName)); varLog = exec. Runcmd (Mysqlbinpath,string. Format ("mysqld.exe-install {0}--defaults-file=\ "{1}\"", ServiceName, Mysqlinipath)); Api.logger.addToLog ("C_installservice method log: \ r \ n"+log); } /// <summary> ///Start the service/// </summary> Public Static BOOLD_startservice (stringserviceName) { //return Cocon90.Lib.Util.Window.Service.ServiceHelper.StartService (ServiceName, Timespan.fromseconds (Ten));//Open Service Try{servicecontroller[] services=servicecontroller.getservices (); ServiceController Service= Services. Where (p = = P.servicename = =serviceName). FirstOrDefault (); if(Service = =NULL)return false; Service. Start (); Service. WaitForStatus (servicecontrollerstatus.running, Timespan.fromseconds (Ten)); returnService. Status = =servicecontrollerstatus.running; } Catch(Exception ex) {Api.logger.addToLog (ex,"D_startservice method Error:");return false; } } /// <summary> ///set the initial password. Modify the blank password to the specified password. /// </summary> Public Static BOOLE_setdefaultpassword (stringMysqlinipath,stringnewpassword) { varPort =Getdbport (Mysqlinipath); returnSql. SetDefaultPassword (Port,"", NewPassword); } /// <summary> ///detects if a database exists (or has been initialized)/// </summary> Public Static BOOLGetisdbfileexisted (stringMysqldatapath) { return! (! Directory.Exists (Mysqldatapath) | | Directory.GetFiles (Mysqldatapath). Length = =0|| ! Directory.Exists (Path.Combine (Mysqldatapath,"MySQL")));//Check if there are no files in the directory, or if there are no MySQL folders under the data directory//if (ifnotexistisinit)//{ // //Create a new initial database//string log = Exec. Runcmd (Mysqlbinpath, "mysqld--initialize-insecure--user=mysql--console"). Trim (); //if (log. Length >)// { // //get the initial password//Initpassword = log. Substring (log. LENGTH-12, 12); // } //} } /// <summary> ///Get current Port/// </summary> Public Static stringGetdbport (stringMysqlinipath) {Inihelper ini=NewInihelper (Mysqlinipath); returnIni. Read ("mysqld","Port"); }
Public Static BOOLSetDefaultPassword (ObjectPortObjectInitpassword,stringnewpassword) { stringConnStr =string. Format ("server=localhost;user=root;port={0};p assword= ' {1} '; logging=true;", Port, Initpassword); Mysqlconnection Conn=Newmysqlconnection (CONNSTR); Try{Conn. Open (); varCommand =NewMysqlcommand (string. Format ("alter user ' root ' @ ' localhost ' identified by ' {0} ';", NewPassword), conn); varEFF =command. ExecuteNonQuery (); Conn. Close (); return true; } Catch(Exception ex) {conn. Close (); return false; } }
MySQL service initialization, installation, startup