python mysql operationalerror - python

I have a Python script that processes some data and then inserts it into mysql using MySQLdb.
When I run the script on my local server all is well. When I run it on our server I straightaway get
OperationalError: (2006, 'MySQL server has gone away')
Any ideas?

2006, 'MySQL server has gone away' means time out. Most times when it happend to me when I try to connet to external mysql server and this server was not set up to accept external connections (from your localhost). Straightaway response would confirm it.

Check the logs of your MySQL installation and see if it is really gone and for whatever reason. Apart from that check your network connectivity and the network configuration of mysqld (it must listen on the related external network addresses). Not a Python issue.

changed hosting provider and problem went away. Must have been configured poorly.

Related

(2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

I am on mac with homebrew mysql, making a django app. I also use jupyter. I don't know which of these things is the root cause of the issue.
I used to get this error message coming up every now and again, and I know how to fix it: just type brew services restart mysql and wait a bit.
The problem is that it's now happening with increasing frequency, several times an hour. Is there a known fix for this, when working on a local machine?
I thought, since I'm ctrl-c ing programs frequently, that might be exhausting the connection pool so I increased the connection pool size using SET GLOBAL max_connections = 5000;, but that has not helped.
Just a suggestion - I would give Docker a shot (since you keep getting these interruptions).
Create a new container which will run your MySQL server exposing a port to the DB server then connect to that from your mac.
Here's a good resource to get you started - https://docs.docker.com/samples/library/mysql/#-via-docker-stack-deploy-or-docker-compose
Alternatively, if you don't have to use MySQL you could try using PostgreSQL. Here's a good article for that one - https://medium.com/agatha-codes/painless-postgresql-django-d4f03364989 . Many django projects are using that in production as it has a lot more features to offer.

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.

sqlalchemy connection identical credentials refused when run on different machines

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/

Weird MySQL Python mod_wsgi Can't connect to MySQL server on 'localhost' (49) problem

There have been similar questions on StackOverflow about this, but I haven't found quite the same situation. This is on a OS X Leopard machine using MySQL
Some starting information:
MySQL Server version 5.1.30
Apache/2.2.13 (Unix)
Python 2.5.1
mod_wsgi 3
mysqladmin also has skip-networking listed as OFF
I am able to connect to mysql from the python command line. But when I try to do it through mod_wsgi using code that is copy and pasted or via Django I receive the generic connection refusal
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (49)")
I've looked at the mysql manual and tried its troubleshooting tips such as
telnet localhost 3306
and I do get a connection.
I am not trying to connect as root, either.
Any ideas on what else I could check?
Thanks in advance!
I came across this error and it was due to an SELinux denial. /usr/bin/httpd didn't have permission to connect to port 3306. I corrected the issue with:
setsebool httpd_can_network_connect_db on
Seems to work great and should be more secure than just disabling SELinux. As Avinash Meetoo points out below, you can use:
setsebool -P httpd_can_network_connect_db
To make the selinux change persist across reboots.
I was getting the exact same error message in Django:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (49)")
To fix it, I had to explicitly set the mysql port to 3306 in the django settings file.
Not too much out there on this. Just a random guess but try using:
DATABASE_HOST = 'localhost'
instead of 127.0.0.1
and/or try commenting out in your my.ini:
bind-address 127.0.0.1
worth a shot.
Bit odd that the telnet connection works. Maybe some more ways to trouble shot:
shell> perror 49
OS error code 49: Can't assign requested address
I would check the localhost interface first, check if it has IPv4 address. Far fetched maybe, but I had troubles ones when I didabled IPv6.
shell> ifconfig lo0
Maybe the name resolution doesn't work correctly from within Apache/mod_wsgi/etc..
import python
print socket.gethostbyname('localhost')
print socket.gethostbyaddr('127.0.0.1')
Maybe to get you going (something I contributed to Django) try the UNIX Socket in Django, it works setting the database host to the path (start with forward-slash):
DATABASE_HOST = '/tmp/mysql.sock'
Or where ever your socket file is.
Last, check the MySQL error log if there are any weird messages, like it failing to bind on the IP address or port.
Hope this helps a bit.

Categories