sqlalchemy connection identical credentials refused when run on different machines - python

I have a problem which seems impossible to me, meaning I am fundamentally misunderstanding something. I've written a simple API using flask (a python library). This api, among other things, connects to a mysql server running on a remote web server. I am using the sqlalchemy library to perform this connection.
The connection string is quite simple. It looks like this:
db =create_engine('mysql+mysqlconnector://{user}:{password}#{host}:{port}/{database}'.format(user=Constants.Sql.USER, password=Constants.Sql.PASS, host=Constants.Sql.HOST, port=Constants.Sql.PORT, database=Constants.Sql.DATABASE))
connection = db.connect()
On my development machine this all works fine. However, when I deploy the api to a different remote machine, it doesn't work. I get the error:
sqlalchemy.exc.ProgrammingError: (ProgrammingError) 1045 (28000): Access denied for user 'user'#'domain' (using password: YES) None None
This doesn't make any sense to me because it is using exactly the same credentials (they are hard coded).
The working environment is a windows machine, the environment throwing the error is ubuntu 14.04. Both the windows and ubuntu machines are remote to the web server on which the database is running, so it can't be some weird localhost thing.
I am totally stumped with this. If anyone could give me some advice I'd really appreciate it!

Maybe the database only accepts connections from a particular IP address. That would explain why same username and password would succeed on one and fail on the other.
GRANT includes IP address information. Look at the MySQL documentation. Or this tutorial:
https://alvinalexander.com/blog/post/mysql/add-user-mysql/

Related

"OperationalError : no password supplied", when linking python and sql [duplicate]

This is probably a silly error but I cannot seem to find a satisfying solution.
When running db.create_all(), I got the following error.
sqlalchemy.exc.OperationalError: (OperationalError) fe_sendauth: no password supplied None None
My database link is set as
'postgresql://localhost/db_name'
This worked fine on my Mac and Heroku, but is not OK on ubuntu (digitalocean).
Any ideas what I might be doing wrong?
You probably just need to remove "localhost" from your connection string:
'postgresql:///db_name'
That tells psycopg2 to use Unix-domain sockets. Your default configuration will use "ident" so you'll be connecting as the user that runs the script. In the default configuration, "md5" only applies to TCP connections.
URL pattern should be:
postgresql://user:password#localhost:5432/database_name
pip install psycopg2
the user should be postgres or any other user you have created and intend to use
similarly for mySql it would be:
mysql://user:pass#localhost:3306/database_name
pip install mysql-python
On your Mac, PostgreSQL was set up for trust or peer authentication for connections from localhost.
On your Ubuntu box it's set up for md5 authentication for connections from localhost.
You'll want to configure a password, or change the authentication mode. See pg_hba.conf, and the Ubuntu guide for PostgreSQL (there's a section about this error).
Below worked for me. Your connection to your postgres database requires a password; thus, below is what you should write..
pg_user = "magicmike"
pg_pwd = "test123"
pg_port = "5432"
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://{username}:{password}#localhost:{port}/foodversity_db".format(username=pg_user, password=pg_pwd, port=pg_port)
First make sure that the database server is connected and then run the command again.Silly, but it worked for me.
For Remote Server
remote server => postgresql://<username>:<password>#<ipaddress>:<port>/<database>
For Local in configuration use
local db => postgressql:///<database>

SSL connection to MySQL dB failed

I have a python script running on an Amazon EC2 instance (linux) that scrapes the data from a source and outputs a pandas dataframe to me. I want to send this dataframe to MySQL on Amazon RDS. But when I run the script, it throws me the following error:
sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) 2026 (HY000): SSL connection error: SSL_CTX_set_tmp_dh failed
Before executing the script on an ec2 instance, I was running it on my local machine and it was working fine. Meaning, I was able to send the data to the RDS scraped by my scraper. But now that I am running the script on EC2, it is showing me SSL connection errors.
I might not have configured the EC2 instance correctly or might have forgotten to install something. I don't work with SSL much, so I'm finding it difficult figuring out how this is supposed to be set up. Any pointers, even obvious ones, will likely help at this stage.
I have tried some of the solutions that i found on stack but none of them worked. This is my code to connect to the database:
def dbConnect():
end_point = xxx
username = xxx
password = xxx
port=3306
global dbname
dbname=xxx
conn = pymysql.connect(end_point, user=username,port=port,passwd=password, db=dbname)
global eng
eng=create_engine('mysql+mysqlconnector://xxx:xxx#xxx:3306')
return dbname,eng
The solution is to downgrade openssl.
conda install openssl=1.0.2p
See this thread for more information:
https://github.com/ContinuumIO/anaconda-issues/issues/10646
Also this other question.
I have something very similar, although I use PuTTy for the SSH to create a port forwared tunnel.
Make sure you have the security groups set up correctly between your EC2 instance and RDS too.

App engine MySQL connection to remote db failing

I've got an app engine running and I'm struggling to get the MySQL to connect when it has been deployed. It connects fine on my pc running the dev server, but as soon as I deploy I get this error:
OperationalError: (2004, "Can't create TCP/IP socket (-1)")
Could this be because it is not a cloud sql database ? I've fiddled with a few things like firewall rules and dns things but I honestly just don't know where to even start solving this issue. Some research indicated it might be a TCP/IP vs Unix socket issue which does also kind of make sense as I've got another connection to a cloud sql instance which works fine (using a unix socket). It is a python app, any help is appreciated
You need to enable billing for your project to use socket connections in appengine..

Python mysql.connector connection DatabaseError - host is blocked occurring when running from remote vm

I am attempting to connect to a remote MySQL DB from a virtual machine (Ubuntu, running on Azure).
When I access the DB from my computer via the command line, I enter:
mysql -u username -h www.foobar.nyc -p
Which prompts me for the password. When I enter the password, it successfully logs me in to the remote db.
Now when I perform the same actions as above, but instead from a remote vm that I have ssh into, I get the following error returned after entering my pw.
ERROR 1129 (HY000): Host 'xxx.xx.xxx.xxx' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
Googling this error brings me to: http://dev.mysql.com/doc/refman/5.7/en/blocked-host.html
I'd like to understand why I am getting so many connection errors - is that normal? Is there a setting perhaps with Azure that I need to look into? I know Azure has the endpoints manager. When using the Python mysql library, it reads that errors occurring File "/site-packages/mysql/connector/connection.py", line 418, in _open_connection on self._do_handshake()
What I hope to gain from this question:
Understand why "many" errors are occurring - what is causing this and is receiving such high numbers of errors normal (as some comments in the MySQL documentation I linked to appear to suggest).
Understand the differences that enable the same actions to work from local in command line but not from command line when ssh into remote Azure based vm.
Thanks.
1.Understand why "many" errors are occurring - what is causing this and is receiving such high numbers of errors normal (as some comments in the MySQL documentation I linked to appear to suggest).
Per my experience, you can check the mysql error logs, details on enabling error logs are # How to see log files in MySQL?.
2.Understand the differences that enable the same actions to work from local in command line but not from command line when ssh into remote Azure based vm.
Based on the latest comment you provided, the new created VM was not experiencing the same issue, it may not be an azure platform specific related issue.

Python and Connecting to MySQL over SSH

I am trying to connect to a MySQL database on someone else's "machine". When I use Navicat for MySQL, I have no problem connecting to it. I am trying to do the same with Python so that I do not have to use the GUI interface. I know all my info below is correct (even though I swap fake info) -- can anyone spot where I went wrong? The error I get is OperationalError: (2005, "Unknown MySQL server host 'FTP_hostname' (0)")
My code (using paramiko for the SSH):
import MySQLdb
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('SSH_hostname', 22, username='me', password='pswrd')
time.sleep(1)
db = MySQLdb.connect(host="FTP_hostname",
user="root",
passwd="pswrd2",
db="MyDB")
cur = db.cursor()
Again, I put all this into Navicat and connect no problem. Hoping you can help! Thanks!
MySQL, like most databases, by default runs locally and disallows access from outside networks. As such, you cannot connect to it from an external computer.
Navicat, being a software explicitely for remote administration of databases, likely connects via SSH and tunnels the MySQL connection over it. That way it can act as if the database was installed locally, and for the database it looks as if it was accessed locally.
You could try to do the same by creating a tunnel using Paramiko; see also this question.
If you still in need of connecting to a remote MySQL db via SSH I have used a library named sshtunnel, that wraps ands simplifies the use of paramiko (a dependency of the sshtunnel).
You can check my answer in another similar question with some sample code to use it.
db = MySQLdb.connect(host="FTP_hostname",
Would the host not need to be 127.0.0.1 (localhost) as the tunnel is making the MySQL DB local to the machine that the python script is running on?

Categories