Home:ALL Converter>Bash script fails to connect remote MySQL while it is possible to access it manually

Bash script fails to connect remote MySQL while it is possible to access it manually

Ask Time:2020-12-30T17:15:47         Author:Murat

Json Formatter

I need to write a bash script which would connect to MySQL server on remote host with below command:

$MYSQL_HOME/bin/mysql --socket=$MYSQL_UNIX_PORT -u$MYSQL_USER -p$MYSQL_PWD --host $x --connect-timeout=5 -e 'show master status'

The script finds 2 database servers (10.5.5.233 and 10.5.5.234) in the config file and it tries to access to each of these hosts. However, the connection fails with "No route to host":

ERROR 2003 (HY000): Can't connect to MySQL server on '10.5.5.233' (113 "No route to host")
ERROR 2003 (HY000): Can't connect to MySQL server on '10.5.5.234' (113 "No route to host")

On the other hand, the connection to the master database succeeds when I try to connect it manually:

$MYSQL_HOME/bin/mysql --socket=$MYSQL_UNIX_PORT -u$MYSQL_USER -p$MYSQL_PWD --host 10.5.5.233 --connect-timeout=5 -e 'show master status'
+------------------------+----------+-----------------------------------------------+------------------+
| File                   | Position | Binlog_Do_DB                                  | Binlog_Ignore_DB |
+------------------------+----------+-----------------------------------------------+------------------+
| xxx-bin.000006 | 65284117 | xxx,sched,qua,tadb,nodb |                  |
+------------------------+----------+-----------------------------------------------+------------------+

I could not find any similar post what could cause this behavior. Could you please help me understand why bash script fails to connect to MySQL on remote host while the same command succeeds when I run it manually? Thanks.

Author:Murat,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65504541/bash-script-fails-to-connect-remote-mysql-while-it-is-possible-to-access-it-manu
yy