通过Java启动和停止mysql [英] Start and Stop mysql through java

查看:326
本文介绍了通过Java启动和停止mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过java程序启动和停止mysql.我尝试了这个问题的解决方案,但未能启动的mysql.然后我尝试使用其他命令,如下所示:

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();
    }
}

该程序的输出如下:

MySQL server started successfully!
5.6.16
MySQL server stopped successfully!

但是,进程(Java代码执行)并没有在最后终止,它仍然是Running

But, the process (java code execution) is not getting terminated at the end, it's still Running!

这一次它可以启动该过程,但无法停止它.可以使用上述代码中的mysqlProc.destroy()取消该过程,但这不是一个不好的做法吗?

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?

那么,如何停止使用上述命令启动的mysql servere?

So, how can I stop the mysql servere that was started using above command?

或者,还有其他方法可以通过Java启动和停止mysql吗?

Or, is there any other way to start and stop mysql through java?

推荐答案

该过程仍然存在的原因是您永远不会终止它.如果您以上述方式使用Java Process API,则子进程(MySQL)会等待指示该做什么.这是有道理的,因为在大多数时候,Java代码都希望与孩子对话并通过stdio交换数据.

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.

在您的情况下,这没有意义:您已经通过JDBC进行通信,因此stdio管道只是在四处闲逛,使孩子活着.当Java停止运行时,大多数孩子死亡,但是MySQL显然更具弹性.

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.

在这种情况下,调用destroy是安全的,因为即使主进程意外崩溃,MySQL也不得破坏数据.不过,这不是一个好习惯.

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.

要解决您的问题,

  1. 使用将MySQL作为守护程序启动的Shell脚本包装器.该脚本应启动并立即返回,使MySQL服务器在后台运行.
  2. 确保正确处理stdio.如果您将那些管道闲逛,则孩子可能会挂死(例如,发生错误时).
  3. 您最终必须调用Process.waitFor()以确保正确清理了子进程.
  1. 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.
  2. Make sure you properly handle stdio. If you leave those pipes hanging around, the child might hang (for example, when there is an error).
  3. You must eventually call Process.waitFor() to make sure the child process is cleaned up properly.

这篇关于通过Java启动和停止mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆