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

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -