1

How can I start MySQL in a windows environment and return to the commandline when the startup finished and it is running?

If I do a

mysqld --defaults-file=... --console

the server starts but mysqld blocks while it is running.

Background for this requirement is that I want to start MySQL from a Java-Program (using Runtime.getRuntime().exec(...)) and run another program when MySQL is ready.

7
  • I would launch a "start" and try to connect in a temporized loop Commented Nov 8, 2015 at 10:09
  • I tried that by running a cmd /c start /B mysqld ... but it didn't work... Commented Nov 8, 2015 at 10:11
  • 3
    To start the MySql server enter net start <servername> on the command line. Commented Nov 8, 2015 at 10:14
  • I want to start MySQL from a Java-Program. Commented Nov 8, 2015 at 10:15
  • You shouldn't. It should already be running as a service, the way it is designed. Not under the control of an application. Commented Nov 8, 2015 at 10:43

1 Answer 1

1

I solved this by running the command in its own Thread and wait until I can connect to the server:

Thread runningThread = new Thread() {
  @Override
  public void run() {
    String[] cmdArray = new String[3];
    cmdArray[0] = "mysqld.exe";
    cmdArray[1] = "--defaults-file=./my.ini";
    cmdArray[2] = "--console";
    try {
      Process proc = Runtime.getRuntime().exec(cmdArray);

      // read results
      BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
      String line;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }

      br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      try {
        proc.waitFor();
      } catch (InterruptedException ex) {
        ex.printStackTrace();
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
};
runningThread.start();
while(!isMySQLRunning()) {
  System.out.println("waiting for MySQL to start...");
}

// ...

private static boolean isMySQLRunning() {
  String[] cmdArray = new String[7];
  cmdArray[0] = "mysqladmin.exe";
  cmdArray[1] = "-u";
  cmdArray[2] = "root";
  cmdArray[3] = "-pMyPassord";
  cmdArray[4] = "-P";
  cmdArray[5] = "3306";
  cmdArray[6] = "status";
  Vector<String> res = runCommand(cmdArray); // runCommand(): see above
  if (res.size() > 0 && res.get(0).contains("Can't connect to MySQL server")) {
    System.out.println("MySQLis not running.");
    return false;
  } else if (res.size() > 0 && res.get(0).contains("Uptime")) {
    System.out.println("MySQLis running.");
    return true;
  }
  System.out.println("MySQLis not running.");
  return false;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.