0

I all,

I have a series of MYSQL databases with different users and passwords, nevertheless the DB structure is the same for all databases. I can't create a user with the same username and password to all of them and I need to quickly perform operations on all of them.

I was thinking about a bash script to run via cron.

Any suggestion? I was thinking to something like this but it is not working :(

#!/bin/bash
uconn=(
            'mysql -u user_db1         --password=pass_db1      db1      '
            'mysql -u user_db2         --password=pass_db2      db2      ' 

             )

for f in "${uconn[@]}"
do
    exec ${f}
    echo `mysql show tables`
    echo `mysql exit`
done
exit

2 Answers 2

1

Why not use the documented way?

do
  ${f} <<EOF
show tables
\\q
EOF
done
Sign up to request clarification or add additional context in comments.

Comments

0

Just pasting the full code taking into consideration @Ansgar Wiechers:

#!/bin/bash
uconn=(
            'mysql -u user_db1 --password=pass_db1  db1'
            'mysql -u user_db2 --password=pass_db2  db2' 

             )

for f in "${uconn[@]}"
    do
      ${f} <<EOF
    show tables
    \\q
    EOF
    done
exit

To execute the code from the local machine to the remote one this works for me:

ssh [email protected] 'bash -s' < /local/path/to/multiple_db_connections.sh 

where the content of multiple_db_connections.sh is the code above

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.