mysqlbashubuntuinfinidb

mysql hangs at login


I am using InfiniDB (not by choice). When I use the linux shell, I am able to connect to a database just fine:

<user>@<host>:~$ /usr/local/Calpont/mysql/bin/mysql -u <user> -p -h <host>
Enter password: <password>

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1725
Server version: 5.1.39 MySQL Community / Calpont InfiniDB Community 2.2.11-1 Fin                                                                             al (COSS LA)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use <database_name>
Database changed
mysql>

But when I use a bash script, it hangs:

<user>@<host>:~$ bash -x /home/aliceell/db_export.sh
+ DB=al05_09
+ export_dir=/tmp/
++ /usr/local/Calpont/mysql/bin/mysql -u <user> -p -h <host> <db_name>
Enter password:

It stops responding after I enter my password and press 'enter'. I have to use ctrl-C to get out of it.

Here is the code that I have:

DB=<db_name> 
export_dir="/tmp/"

err_exit()
{
 echo -e 1>&2
 exit 1
}

mysql_data_dir=$(/usr/local/Calpont/mysql/bin/mysql -u <user> -p -h <host> <db_name>) #it hangs at this stage

if ! (mysqlshow $DB 1>/dev/null); then
  echo ERROR: unable to access database
  exit 1
fi

echo "connected"

if ! [ -w $export_dir ]; then
  echo ERROR: export dir is not writable
  exit 1 
fi

ExportTable()
{
 /usr/local/Calpont/mysql/bin/mysql -u <user> -p -h <host> <db_name>" 
   SELECT * INTO OUTFILE '/tmp/$T.csv' 
   FIELDS TERMINATED BY '|' 
   LINES TERMINATED BY '\n' 
   FROM $T
 "
}

/usr/local/Calpont/mysql/bin/mysql -u <user> -p -h <host> <db_name> "show tables" | 
while read T; do 
  echo exporting $T
  ExportTable || err_exit
  sudo mv $mysql_data_dir/$DB/$T.psv $export_dir/ || err_exit
done

I'm new to bash and trying to use it to loop through all the tables in my database and export them (similar to this script), but it's no use if I can't even connect to the database...

Thanks in advance for your help.


Solution

  • The command substitution that you are assigning to mysql_data_dir is not run interactively so the mysql command must run, generate its output and terminate before the output will be returned to the shell variable. Look closely at your linked example (excerpted below) and you will see that his mysql commmand is just this kind of command line; it includes a mysql command that gets executed and piped into an awk command for post-processing.

    mysql_data_dir=$(mysql $myopts "show variables like 'datadir'" | awk '{sub(/\/$/,"");print$NF}')
    

    What you need to do is identify what your "one-liner" mysql command is and put that into your $().