shell - How to test connection to mysql server? -


i'm trying figure out how test if connection possible given set of user, pw, , db name info. parse out user_name, password , database_name environment file , test if connection possible. if possible continue rest of script , if not possible, exit , print error message. want test connection because have larger script runs can't tell fails if fail.

you can make no-op connection:

mysql -e '\q' 

if run suitable .my.cnf file, $? zero. if try use different user:

mysql -u root -e '\q' 

then error message (which can redirected away 2>/dev/null) , $? non-zero.

note limited test, compared using language sql library , connection spans multiple commands. e.g. 1 connection may succeed, later 1 may fail because of network conditions or change user's conf file or server authentication.


Comments

Popular posts from this blog

Capture and play voice with Asterisk ARI -

java - Why database contraints in HSQLDB are only checked during a commit when using transactions in Hibernate? -

visual studio - Installing Packages through Nuget - "Central Directory corrupt" -