Turn MySQL server on and off


I want to turn on and off the mysql server through java, this is just an exercise, I used the org.apache.common.exec package to run the command, the only problem is that I can only do it through the daemon with

sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

(this to turn off the server) and I would like to see if they have any command to know the status through the daemon because with the other commands I searched for I get a Pid error.

I had been told in another question that the status of the server could be known through the jdbc but I can not find that form.

asked by Paul Avalos 01.08.2016 в 04:35

1 answer


Use the following code to execute a command (in this case, turn off the database) of the OS from java:


Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("echo 'Password' | sudo -S service mysql stop");
//imprimes el password de root y lo lees de la salida con la opción -S

In windows it would be something similar to the following:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec('"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqladmin" -u root shutdown');
answered by 01.08.2016 в 07:44