0

I'm writing a script to connect mysql server on an EC2 to RDS, normally I do this:

mysql -h $RDS_ENDPOINT -P 3306 --user=$RDS_MYSQL_USERNAME --password=$RDS_MYSQL_PASSWORD

and then exit

I just want to establish the connection, nothing else

Now I need to put these commands into a script to automate this process without typing exit. I got 2 issues: the mysql is not found when I run bash; and how do I exit mysql from script?

Casper
  • 117
  • 8

2 Answers2

0

Try to install a mysql client. Choose what fits your current linux distribution.

sudo apt-get install mysql-client

or

yum install mysql
bobbywebz
  • 96
  • 4
0

Simple:

#!/bin/bash

RDS_MYSQL_ENDPOINT="your-endpoint-goes-here";
RDS_MYSQL_USER="your-username-goes-here";
RDS_MYSQL_PASS="your-password-goes-here";
RDS_MYSQL_BASE="your-database-name-goes here";

mysql -h $RDS_MYSQL_ENDPOINT -u $RDS_MYSQL_USER -p$RDS_MYSQL_PASS -D $RDS_MYSQL_BASE -e 'quit';

if [[ $? -eq 0 ]]; then
    echo "MySQL connection: OK";
else
    echo "MySQL connection: Fail";
fi;

Now, the problem you're facing is because you probably don't have the MySQL client installed. To do it on a Ubuntu/Debian system, just type:

sudo apt-get install -y mysql-client

And you're done.

Stefano Martins
  • 1,221
  • 8
  • 10
  • Thanks for the `-e` tip, I got it working by this command `mysql -h $RDS_ENDPOINT -P 3306 --user=$RDS_MYSQL_USER --password=$RDS_MYSQL_PASS -e 'quit'` – Casper Oct 22 '15 at 22:22