I want to start and stop mysql through java program. I tried solution from this question, but it failed to start the mysql. Then I tried using other commands as below:
private static String commandStart = SQL_INSTALL_DIR + "/bin/mysqld";
private static String commandStop = SQL_INSTALL_DIR + "/bin/mysqld -u root shutdown";
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
startMysql();
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "testuser";
String password = "test623";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Main.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
stopMysql();
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Main.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
private static void startMysql() {
try {
mysqlProc = Runtime.getRuntime().exec(commandStart);
System.out.println("MySQL server started successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void stopMysql() {
try {
Runtime.getRuntime().exec(commandStop);
System.out.println("MySQL server stopped successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
The output of this program is as below:
MySQL server started successfully!
5.6.16
MySQL server stopped successfully!
But, the process (java code execution) is not getting terminated at the end, it's still Running!
This time it's able to start the process but unable to stop it. The process can be destryed using mysqlProc.destroy() in above code, but isn't that a bad practice?
So, how can I stop the mysql servere that was started using above command?
Or, is there any other way to start and stop mysql through java?
解决方案
The reason why the process is still alive is that you never terminate it. If you use the Java Process API in the way outlined above, then the child process (MySQL) waits for instructions what to do. This makes sense since most of the time, the Java code will want to talk to the child and exchange data via stdio.
In your case, that doesn't make sense: You already communicate via JDBC, so the stdio pipes are just hanging around, keeping the child alive. When Java stops, most children die but apparently MySQL is more resilient.
Calling destroy is safe in this case because MySQL must not corrupt data even when the main process crashes unexpectedly. It's not good practice, though.
To solve your problems:
Use a shell script wrapper that starts MySQL as a daemon. The script should start and return immediately, leaving the MySQL server running in the background.
Make sure you properly handle stdio. If you leave those pipes hanging around, the child might hang (for example, when there is an error).
You must eventually call Process.waitFor() to make sure the child process is cleaned up properly.