Unable to connect to MySql db from Airflow - mysql

So I have 2 EC2s in a single VPC. One EC2 is running airflow, the other ec2 is running a wordpress db.
I've created a new Airflow job that uses the MySqlHook to connect to the wordpress db.
mysql_hook = MySqlHook(mysql_conn_id='wp')
however, when I run the above code, I get the following response:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '{PRODHOSTURL}.us-east-1.rds.amazonaws.com' (110)")
I've tried making sure there are inbound rules that allow for 3306 port connections on the wordpress db. I've tried adding bind address rules to the .conf file for mysql. I've checked and re-checked the db rules. But nothing is working. Any ideas?
PS The airflow ec2 has many jobs, most of which connect to other dbs in this same VPC...so I don't know why MySql is acting differently. Could I use a different hook?

We found the answer. The security rules were incorrect. To troubleshoot, i went to one machine and ran the command "ping {other machine's ip address}" until I could get through, in both directions. Then re-ran the code, and it worked. Thanks!

Related

Connecting MySQL on EC2 to Tableau

I used a quick start guide to create a mysql database on EC2 that I can access and query using RStudio. However, I'd like to use Tableau Desktop to visualize the data as well. Tableau can connect to MySQL, but Im not sure how to make it work with EC2. I think I need to use port forwarding. I've Googled extensively and found a few examples but I'm completely new to this and don't know where to begin.
I'm using Terminal on Mac OSX. I'm able to login to SSH and to MySQL once in SSH.
Tableau needs a server, port, user, pass, and initial SQL statement. I'm assuming the initial SQL statement is just "use MyDatabase;"
I remember running into this issue awhile ago and found the solution again. I think the main thing to remember is that you have to use the localhost as the address in Tableau after you ssh into EC2 instead of the address of the actual EC2. You also have to setup your ssh to forward the port as you mentioned. Here is how you do that with PuTTY.
In the first screenshot you will see I added 127.0.0.1:3306 as the destination. This is the address of MySQL as setup on your server. I included 9990 as the source port. This is the port that PuTTY will be monitoring for any given calls. Once you have these defined click add and then save your session and connect with your server credentials through PuTTY. Go to Tableau.
In the second screenshot you will see that I used 127.0.0.1 for the server and 9990 for the port that I defined in PuTTY. You then add the credentials in Tableau that correspond to your MySQL user that is setup on EC2.

OperationalError Lost connection to MySQL server at 'reading initial communication packet' DigitalOcean

Im having trouble connecting to my sql server on digital ocean through my flask webapp. Im using flask-sqlalchemy to bind the mysql database to Flask.
Im able to access the mysql server through the phpmyadmin interface at myipaddress:5000/phpmyadmin
Since Im using Nginx (I bound it to port 80). Bound Apache to Port 5000. So my phpmyadmin interface is accessible at
myipaddress:5000/phpmyadmin
In my flask app, i specify the
SQLALCHEMY_DATABASE_URI ='mysql://root:password#myipaddress:5000/databasename'
when i try to create the tables on my database using the shell with db.create_all() - it just doesnt respond. The cursor blinks forever and then i get the operational error that i quote on the title afte a few minutes
Im able to get the same setup running on my local dev machine. So i know its not a flask configuration problem but just a mysql access issue. I have my webapp up on digitalocean (Not sure if mysql server is behind a firewall or something like that making it inaccessible
On the
/etc/mysql/my.cnf
for the bind-address under mysql_d section, i tried all possible combinations and restarted the mysql server with no success
i tried localhost, 127.0.0.1, 127.0.0.1:5000, myipaddress for the bind-address (Also tried commenting it out) without any results.
Also i tried to get the user, current_user on the table properties from the mysql command line, it's listed as root#localhost for both
From this post:Lost connection to MySQL server at 'reading initial communication packet', system error: 0; i get the idea that its related to firewall but not sure how to rectify this.
Anyidea how can i connect my flask app to the mysql server ? Any help would be much appreciated
I was specifying the mysql address as
mysql://root:password#myipaddress:5000/databasename
But since my flask app and the mysql server are running on the same server, the flask app was be able to access the mysql server when i replaced the myipaddress:5000 with localhost:5000
mysql://root:password#localhost:5000/databasename
The phpmyadmin is just a web interface for your database, if you're looking at connecting your Flask application to your MySql database, you need to point the SQLALCHEMY_DATABASE_URI to the actual MySQL database, rather then the web interface to that database.
Usually MySql runs on port 3306. I believe that SQLAlchemy is clever enough to know that's the default, so your connection string should just be: SQLALCHEMY_DATABASE_URI ='mysql://root:password#localhost/databasename' or if you are running on a different port/external IP address: SQLALCHEMY_DATABASE_URI ='mysql://root:password#myipaddress:4444/databasename'. Remember, if you're connecting to a MySQL database on an external server (not the same one as your Flask app is running on) you will have to change the configuration to allow that kind of access.

Connect Remotely to mysql running on a Amazon EC2 Instance

I'm trying to connect MySQL Workbench to an Amazon EC2 (Linux) instance that hosts a MySQL Database. (Not RDS but localhost). However for some reason, I can't get it to connect remotely.
Things Ive done:
- Set the security group to allow any IP to access port 3306
- Created a mysql user and granted all privledges on it.
- Modified the my.cnf to include bind-address=0.0.0.0 However i still cant connect.
On this instance I do have SSL cert installed and I am forwarded all http request to https?.. But im not sure if this has anything to do with it.
If anyone could guide my in the righ direction I would appreciate it.
I would personally give it an Elastic IP so it will have a public IP, then bind the mysql to that IP. Make sure the iptables are set to accept the mysql connections. I also wouldnt suggest leaving mysql open to every ip unless this is an absolute necessity.

How to fix 'Can not connect to MySQL server' error?

First of all I apologize for my English is not perfect.
I'm trying to connect to mysql database remotely.
I have already done the basic steps such as, for example, comment the bind address in my.cnf.
I gave permissions to the appropriate users in MySQL.
I tried with my friend to do it connect to the database, and it worked but we were in the same room with the same router.
Now I'm home and I tried to connect my friend, but I always get error:
ERROR 2003 (HY000): Can not connect to MySQL server ....
Maybe it's a problem of my home router? I can not even do the telnet.
P.S. I've also taken steps to set the firewall.
Some reasons for that,
mysqld is not running on the local host. Check your operating
system's process list to ensure the mysqld process is present.
You're running a MySQL server on Windows with many TCP/IP connections
to it. If you're experiencing that quite often your clients get that
error, you can find a workaround here: Section B.5.2.2.1, “Connection
to MySQL Server Failing on Windows”.
Check whether the server is running on that host by executing telnet
some_host 3306 and pressing the Enter key a couple of times. (3306 is
the default MySQL port number. Change the value if your server is
listening to a different port.) If there is a MySQL server running
and listening to the port, you should get a response that includes
the server's version number. If you get an error such as telnet:
Unable to connect to remote host: Connection refused, then there is
no server running on the given port.
Look at this mySQL full documentation, it will be helpful.

OperationalError, Django Cannot Connect to MySQL Database

I am working a Django tutorial and I want to connect my project to a MySQL database. I did everything as told by the djangoproject tutorial to connect to database. When running python manage.py syncdb , however, I got the
following error:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
What seems problematic? I have XAMMP downloaded and I have created databases through XAMMP before but I don't know how
that affects the connection with Django.
Thanks.
Actually under the settings.py, I set HOST to "127.0.0.1" and PORT to 3306, filled out the rest of DATABASE
portion and it worked!
If you are using Windows 7, you might run into problems if your MySQL is configured to use named pipes. Set your MySQL config to use TCP instead.
 Once your reconfigured MySQL allows local network connections, your MySQLdb should work over TCP.  
It doesn't seem to be able to use
Windows 7 named pipes, although the "mysql" command line client can.
There may be a bug.  This wouldn't be noticed unless MySQL
was configured without network connections, which is rare.  I set
up MySQL that way on a development machine with a local MySQL instance.
A detailed article here - http://interconnectit.com/764/using-mysql-workbench-with-xampp/ - shows you the screen where you can switch your Connection Method from named pipes (Local Socket/Pipes) to TCP/IP via Xampp.
Alternatively, if using named pipes (windows) or sockets (linux/mac) for your python/django app to connect to your MySQL is important to you and you do not want to change your connection method to TCP/IP, you should try setting the servername to "." in your xampp's my.conf configuration file; instead of "localhost" or "127.0.0.1". Using pipes/sockets bypasses the TCP/IP network protocol, giving slightly better performance.