为什么我每次运行程序都说,cant connect to mysql...
然后都要运行mysql的server instance config才可以启动服务??!!!!谢谢!

解决方案 »

  1.   

    net start WindowsXXX服务名称
    如果是linux下的MySQL,那就不晓得了,
      

  2.   

    unit MySQLAction;{
      desc:操作mysql服务的方法
      author:wangcheng
      date:2008-06-20
      modify:
         2008-06-20  增加mysql在windows服务中的方法
    }interfaceuses
      Windows, Messages, SysUtils, IniFiles;function IsInstallMySQL(ServeiceName,MySQLPath:PChar;Port:integer):Boolean;stdcall;
    function ConfigMySQLIni(MySQLPath:PChar;Port:integer):Boolean;stdcall;
    function StopMySQL(ServeiceName:PChar):Boolean;stdcall;
    function StartMySQL(ServeiceName:PChar):Boolean;stdcall;
    procedure RemoveMySQL(MySQLPath,ServeiceName:PChar);stdcall;exports
      IsInstallMySQL,
      ConfigMySQLIni,
      StopMySQL,
      StartMySQL,
      RemoveMySQL;implementationuses ServiceAction;function IsInstallMySQL(ServeiceName,MySQLPath:PChar;Port:integer):Boolean;
    var
      nTimes:integer;   //安装服务计数变量
    begin
      result:=false;  if CommFuns_ServiceGetKeyName('',ServeiceName) then begin
        result:=true;
      end else begin
        //先改写mysql的配置文件
        if Not ConfigMySQLIni(MySQLPath,Port) then Exit;
        nTimes := 0;
        //先检查所需要的mysql是否在服务中
    //    MainPref.AddLogs('start to check mysql named [' + ServeiceName + '] is in windows service');
        while (Not CommFuns_ServiceGetKeyName('',ServeiceName))  do begin
          if nTimes>3 then begin
            break;
            exit;
          end else begin
    //        MainPref.AddLogs('install mysql command =' + MySQLPath + 'bin\mysqld-nt --install ' + ServeiceName + ' --defaults-file=' + MySQLPath + 'my.ini');
            winexec(PChar(MySQLPath + 'bin\mysqld-nt --install ' + ServeiceName + ' --defaults-file="' + MySQLPath + 'my.ini"'),SW_Hide);
            Sleep(1000);
            inc(nTimes);
          end;
        end;    if CommFuns_ServiceGetKeyName('',ServeiceName) then result := True;    
      end;
    end;function StartMySQL(ServeiceName:PChar):Boolean;
    begin
      result:=CommFuns_ServiceStart('',ServeiceName);
    end;{
      修改mysql的配置信息,重要是目录和端口
    }
    function ConfigMySQLIni(MySQLPath:PChar;Port:integer):Boolean;
    var
      tmpIni:TIniFile;
      fs:String;  
    begin
      result:=false;
    //  MainPref.AddLogs('my.ini path =' + MySQLPath + 'my.ini');
      if Not FileExists(MySQLPath + 'my.ini') then Exit;  tmpIni:=TIniFile.Create(MySQLPath+'my.ini');
      try
        tmpIni.WriteInteger('client','port',Port);
        fs:=StringReplace(MySQLPath,'\','/',[rfReplaceAll]);
        tmpIni.WriteString('mysqld','basedir','"'+fs+'"');
        tmpIni.WriteString('mysqld','datadir','"'+fs+'data/"');
        tmpIni.WriteInteger('mysqld','port',Port);
        result:=true;
    //    MainPref.AddLogs('modify my.ini ok.');
      finally
        tmpIni.Free;
      end;
      end;function StopMySQL(ServeiceName:PChar):Boolean;
    begin
      result:=CommFuns_ServiceStop('',ServeiceName);
    end;
    procedure RemoveMySQL(MySQLPath,ServeiceName:PChar);
    begin
      WinExec(PChar(MySQLPath+'bin\mysqld-nt --remove '+ ServeiceName),SW_HIDE);
      Sleep(500);
    end;end.