11package db ;
22
3+ import commons .CommonRails ;
34import javax .xml .parsers .DocumentBuilder ;
45import javax .xml .parsers .DocumentBuilderFactory ;
56import org .w3c .dom .Document ;
910import java .io .InputStreamReader ;
1011import java .sql .Connection ;
1112import java .sql .DriverManager ;
12- import java .sql .SQLException ;
1313import java .util .stream .Collectors ;
1414
1515/**
1616 * Loads MySQL credentials from authentication/mysql.auth.xml.
17- * Provides ensureMysqlRunning() which uses systemctl status mysql to check
18- * whether MySQL is installed and running, and starts it via sudo if needed.
17+ * ensureMysqlRunning() checks systemctl status, starts if needed, then tests JDBC login.
1918 */
2019public class N21AuthConfig
2120{
@@ -46,7 +45,6 @@ public static synchronized N21AuthConfig get()
4645
4746 if (!file .exists ())
4847 {
49- System .err .println ("[N21AuthConfig] " + file .getAbsolutePath () + " NOT FOUND — using defaults." );
5048 INSTANCE = fallback ();
5149 return INSTANCE ;
5250 }
@@ -65,27 +63,19 @@ public static synchronized N21AuthConfig get()
6563 boolean useSudo = Boolean .parseBoolean (text (root , "use-sudo" , "false" ));
6664
6765 INSTANCE = new N21AuthConfig (host , port , username , password , useSudo );
68-
69- // Explicit confirmation that the file was read and what was loaded (password masked)
70- System .out .println ("[N21AuthConfig] Read: " + file .getAbsolutePath ());
71- System .out .println ("[N21AuthConfig] host=" + host + " port=" + port
72- + " username=" + username + " password=" + (password .isEmpty () ? "(empty)" : "***" )
73- + " use-sudo=" + useSudo );
7466 }
7567 catch (Exception e )
7668 {
77- System .err .println ("[N21AuthConfig] Parse error: " + e .getMessage () + " — using defaults." );
7869 INSTANCE = fallback ();
7970 }
8071
8172 return INSTANCE ;
8273 }
8374
8475 /**
85- * 1. Runs "systemctl status mysql" to determine install + running state.
86- * 2. If installed but not running and use-sudo=true, starts it.
87- * 3. Runs a login test: mysql -h host -P port -u user -pPASS -e "SELECT 1"
88- * to confirm the username from the XML can authenticate.
76+ * 1. systemctl status mysql — printed via CommonRails with lime/yellow/red OID color.
77+ * 2. sudo systemctl start mysql if not running and use-sudo=true.
78+ * 3. JDBC login test using credentials from mysql.auth.xml.
8979 */
9080 public void ensureMysqlRunning ()
9181 {
@@ -99,76 +89,69 @@ public void ensureMysqlRunning()
9989 .lines ().collect (Collectors .joining ("\n " ));
10090 int exit = proc .waitFor ();
10191
102- if (output .contains ("not-found" ) || output .contains ("could not be found" ))
92+ boolean notInstalled = output .contains ("not-found" ) || output .contains ("could not be found" );
93+ boolean running = !notInstalled && ((exit == 0 ) || output .contains ("active (running)" ));
94+
95+ if (notInstalled )
10396 {
104- System .err .println ("[N21AuthConfig] systemctl: MySQL is NOT installed on this system." );
97+ CommonRails .printSystemComponent (this , this .hashCode (),
98+ ". systemctl status mysql — MySQL NOT INSTALLED on this system ." ,
99+ CommonRails .COLOR_STANDARD_RED );
105100 return ;
106101 }
107-
108- boolean running = ( exit == 0 ) || output . contains ( "active (running)" );
109-
110- System . out . println ( "[N21AuthConfig] systemctl status mysql → "
111- + ( running ? "active (running)" : "inactive/stopped" ) + " (exit=" + exit + ")" );
112-
113- if (! running && useSudo )
102+ else if ( running )
103+ {
104+ CommonRails . printSystemComponent ( this , this . hashCode (),
105+ ". systemctl status mysql — active (running) ." ,
106+ CommonRails . COLOR_LIME_GREEN );
107+ }
108+ else
114109 {
115- System .out .println ("[N21AuthConfig] Starting MySQL via sudo systemctl start mysql..." );
116- Process start = new ProcessBuilder ("sudo" , "systemctl" , "start" , "mysql" )
117- .inheritIO ().start ();
118- start .waitFor ();
119-
120- // re-check
121- Process recheck = new ProcessBuilder ("systemctl" , "is-active" , "--quiet" , "mysql" ).start ();
122- recheck .waitFor ();
123- System .out .println ("[N21AuthConfig] MySQL after start: "
124- + (recheck .exitValue () == 0 ? "running" : "still not running" ));
110+ CommonRails .printSystemComponent (this , this .hashCode (),
111+ ". systemctl status mysql — inactive / stopped ." ,
112+ CommonRails .COLOR_YELLOW );
113+
114+ if (useSudo )
115+ {
116+ new ProcessBuilder ("sudo" , "systemctl" , "start" , "mysql" ).inheritIO ().start ().waitFor ();
117+
118+ Process recheck = new ProcessBuilder ("systemctl" , "is-active" , "--quiet" , "mysql" ).start ();
119+ recheck .waitFor ();
120+ boolean nowRunning = (recheck .exitValue () == 0 );
121+
122+ CommonRails .printSystemComponent (this , this .hashCode (),
123+ ". systemctl start mysql — " + (nowRunning ? "now running ." : "FAILED to start ." ),
124+ nowRunning ? CommonRails .COLOR_LIME_GREEN : CommonRails .COLOR_STANDARD_RED );
125+ }
125126 }
126127 }
127128 catch (Exception e )
128129 {
129- System .err .println ("[N21AuthConfig] systemctl check failed: " + e .getMessage ());
130+ CommonRails .printSystemComponent (this , this .hashCode (),
131+ ". systemctl status mysql — check failed: " + e .getMessage () + " ." ,
132+ CommonRails .COLOR_STANDARD_RED );
130133 }
131134
132- // ── 2. Login test — credentials passed via --defaults-file (never in argv/ps) ──
133- File cnf = null ;
135+ // ── 2. JDBC login test using credentials from mysql.auth.xml ──────────
134136 try
135137 {
136- // Write a temp .cnf readable only by owner; deleted immediately after the test
137- cnf = File .createTempFile ("n21-mysql-" , ".cnf" );
138- cnf .setReadable (false , false );
139- cnf .setReadable (true , true ); // owner-only read
140- cnf .setWritable (true , true );
141- cnf .deleteOnExit ();
142-
143- try (java .io .FileWriter fw = new java .io .FileWriter (cnf ))
144- {
145- fw .write ("[client]\n " );
146- fw .write ("user=" + username + "\n " );
147- fw .write ("password=" + password + "\n " );
148- fw .write ("host=" + host + "\n " );
149- fw .write ("port=" + port + "\n " );
150- }
138+ String url = "jdbc:mysql://" + host + ":" + port
139+ + "/N21?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&connectTimeout=3000" ;
151140
152- String url = "jdbc:mysql://localhost:3306/N21" ;
153- String user = "mearvk" ;
154- String password = "$$Ironman1" ;
141+ Class .forName ("com.mysql.cj.jdbc.Driver" );
155142
156- try (Connection conn = DriverManager .getConnection (url , user , password ))
157- {
158- System .out .println ("Connected successfully without terminal commands!" );
159- }
160- catch (SQLException e )
143+ try (Connection conn = DriverManager .getConnection (url , username , password ))
161144 {
162- e .printStackTrace ();
145+ CommonRails .printSystemComponent (this , this .hashCode (),
146+ ". MySQL JDBC login — user '" + username + "' authenticated successfully ." ,
147+ CommonRails .COLOR_LIME_GREEN );
163148 }
164149 }
165150 catch (Exception e )
166151 {
167- System .err .println ("[N21AuthConfig] Login test error: " + e .getMessage ());
168- }
169- finally
170- {
171- if (cnf != null ) cnf .delete ();
152+ CommonRails .printSystemComponent (this , this .hashCode (),
153+ ". MySQL JDBC login — user '" + username + "' FAILED: " + e .getMessage () + " ." ,
154+ CommonRails .COLOR_STANDARD_RED );
172155 }
173156 }
174157
0 commit comments