Lost connection to MySQL server during query on Google Cloud MySQL server - python

I am aware that this question has been asked multiple times, but I haven't found a question that pertains to the same issue I am dealing with. I have a MySQL database hosted on Google Cloud SQL. I am running a python script out of a Google Cloud VM to connect to the database to run queries. Everything used to run fine, but now I am getting the following error on my initial connection to the server.
File "/usr/local/lib/python3.5/dist-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 597, in connect
self._get_server_information()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 966, in _get_server_information
packet = self._read_packet()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 656, in _read_packet
packet_header = self._read_bytes(4)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 702, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
And here is the code I am using to connect to the server
connection = pymysql.connect(host='127.0.0.1',
user='xxxxxx',
password='xxxxxx',
database='foobar',
cursorclass=pymysql.cursors.DictCursor,
max_allowed_packet=16777216,
connect_timeout=100)
Initially, my connection code did not have the max_allowed_packet and connect_timeout, and it was working fine. After some research into my issue, these seemed to resolve others' issues, but it hasn't resolved mine.

You must add wait_timeout and start by 10000 and then see.
The other will not help, but i added iy you have another timeout that will not be catch by connection and wait timeout
interactive_timeout Number of seconds the server waits for activity on an interactive connection before closing it
wait_timeout Number of seconds the server waits for activity on a connection before closing it
It seems
use a mysql command right after the connection
SET session wait_timeout=300;

Related

Can't connect Python program to remote MySQL database over the same network

Can someone please help me? I am trying to make a simple python - MySQL connection. the code is running on my Dell-Windows Laptop, while the server is running on my Acer-Linux Laptop, they are connected on the same network. But upon running the query it prompts me with this error : Firewall is disabled for both. I tried pinging both of them vise versa and is working. In fact, I have a program with LibreOffice Base and they are interacting properly. But when I try to translate just a simple query in PYTHON seems like it is not working, I just don't know what could be the error on my code. Thank you so much for you help.
Traceback (most recent call last):
File "C:\Users\JM Dela Cruz\Documents\pyproj\myproject\test1.py", line 3, in <module>
mydb = mysql.connector.connect(
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\pooling.py", line 287, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\connection.py", line 137, in __init__
self.connect(**kwargs)
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\abstracts.py", line 1095, in connect
self._open_connection()
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\connection.py", line 533, in _open_connection
self._socket.open_connection()
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\network.py", line 569, in open_connection
raise InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '198.168.101.42:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
My Python Code :
import mysql.connector
mydb = mysql.connector.connect(
host="198.168.101.42",
user="vsearch",
password="vsearchpasswd",
database="classicmodels",
port="3306"
)
mycursor = mydb.cursor()
_SQL="Select * From employees"
myresult = mycursor.execute(_SQL)
print(myresult.fetchall())

How to create a database in MySQL Python?

I've recently installed the official MySQL extension for Python. However, when connecting to the server it asks me to select the database, but I have not made a database yet.
I don't really know what to do here. So I tried to connect using my information without the database but received errors with the following code:
import mysql.connector
cnx = mysql.connector.connect(user='ubuntulogin', password='ubuntupassword',
host='localhost')
cursor = cnx.cursor()
query = ("CREATE DATABASE database")
cursor.execute(query)
cursor.close()
cnx.close()
Please let me know any issues with my code or how to get MySQL information when I don't know mydatabase name.
Thanks
EDIT: My error message when running the code was:
File "/home/liam/sqltest.py", line 3, in <module>
cnx = mysql.connector.connect(user='ubuntulogin', password='ubuntupassword', host='localhost')
File "/usr/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 162, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 129, in __init__
self.connect(**kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 454, in connect
self._open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 417, in _open_connection
self._socket.open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/network.py", line 475, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
Okay, so it turns out that I am a complete idiot. I was under the impression that MySQL was pre-installed and running since I am used to PHP. However, I just installed it and use my code above and everything seems to be working.
Thanks to #DYZ for pointing out that it looked like I hadn't started the MySQL server and my credentials were incorrect (which turned out to be both true.
You forgot something)
cursor.execute("CREATE DATABASE database;")

Python mysql (using pymysql) failing to connect

I am using Python Pymysql to connect to AWS RDS db.
This was working all these days but now I am not able to connect to the db.
However when I am using the same machine and same credentials it does connect through Mysql workbench, but through pycharm this does not work.,
Code :
configParser = ConfigParser.RawConfigParser()
configFilePath = r'docs/credentials.cfg'
configParser.read(configFilePath)
User = configParser.get('aws-config','user')
Pswd = configParser.get('aws-config','pswd')
Host = configParser.get('aws-config','hostdps')
Databs = configParser.get('aws-config','databs')
db = pymysql.connect(host=Host, user=User, password=Pswd, db=Databs, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor,local_infile=True)
Error :
File "/usr/local/lib/python2.7/dist-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 688, in __init__
self.connect()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 906, in connect
self._request_authentication()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1114, in _request_authentication
auth_packet = self._read_packet()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 961, in _read_packet
packet_header = self._read_bytes(4)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 995, in _read_bytes
"Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([Errno 104] Connection reset by peer)')
How to make sure that this does not happen on production? (prevent such failures in the future).
why u not use connection pool for these?
connection pool can auto manage connection,so can fix this.
U can try any python mysql pool .
pool example
for this this link
or directly use github handy code .PyMysqlPool

mysql.connector.errors.interfaceerror 2003

My summer project is to put data into SQL for processing. Unfortunately I can not establish a connection to the SQL with mysql connector.
The python file I am testing with are two lines:
import mysql.connector
db1 = mysql.connector.connect(host="localhost", database="thename", user="user",password="passw")
The error message is:
C:\Projekt\Python efforts...>test.py Traceback (most recent call
last): File
"C:\Users\Niklas\AppData\Local\Programs\Python\Python35\lib\site-packages\mysql\connector\network.py",
line 472, in open_connection
self.sock.connect(sockaddr) ConnectionRefusedError: [WinError 10061] Det gick inte att göra en anslutning eftersom måldatorn aktivt
nekade det
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Projekt\Python
efforts...\test.py", line 4, in
db1 = mysql.connector.connect(host="localhost", database="thename", user="user",password="passw") File
"C:\Users\Niklas\AppData\Local\Programs\Python\Python35\lib\site-packages\mysql\connector__init__.py",
line 179, in connect
return MySQLConnection(*args, **kwargs) File "C:\Users\Niklas\AppData\Local\Programs\Python\Python35\lib\site-packages\mysql\connector\connection.py",
line 95, in init
self.connect(**kwargs) File "C:\Users\Niklas\AppData\Local\Programs\Python\Python35\lib\site-packages\mysql\connector\abstracts.py",
line 719, in connect
self._open_connection() File "C:\Users\Niklas\AppData\Local\Programs\Python\Python35\lib\site-packages\mysql\connector\connection.py",
line 206, in _open_connection
self._socket.open_connection() File "C:\Users\Niklas\AppData\Local\Programs\Python\Python35\lib\site-packages\mysql\connector\network.py",
line 475, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err))) mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL
server on 'localhost:3306' (10061 Det gick inte att göra en anslutning
eftersom måldatorn aktivt nekade det)
C:\Projekt\Python efforts...>
I have tried restarting and enabling services and computer several times. The user and password I use grants me access in SQL Server Management Studio. I have created a firewall rule to allow communication on port 3306, though it is not listed as one of the active ports when I look. I have also tried disabling the firewall on my computer (though not on the router as it should not be needed for a localhost query?). Using wireshark I don't seem to get any traffic at all on port 3306(!?). I cannot telnet to localhost though tracert has no problems. I have tried with different IPs and domain name that are valid. I have enabled TCP/IP settings for a bunch of the different IP versions under TCP/IP protocol in the SQL Server Configuration manager (though I'm not sure how I did that as it is write protected so I can't do the rest of them).
Everything listed here yields the same result so I assume that there is something very fundamentally (basic and easy?) wrong with what i am doing. Please pitch in with any and all troubleshooting tips you can find for resolving this connection issue as I am on my third day of getting absolutely nowhere.
Once again it has been proven that it is easy to get blinded when digging. I was trying to use mysql connector to connect to a ms sql. I have now switched to pypyodbc to handle the communication with the SQL.

'Interface error: 2003' when connecting to database

I'm trying to use MySQL Connection/Python to connect to my database.
Here's the output I'm getting:
Traceback (most recent call last):
File "bh2000.py", line 33, in <module>
cnx = mysql.connector.connect(**config)
File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 155, in connect
return MySQLConnection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 123, in __init__
self.connect(**kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 430, in connect
self._open_connection()
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 393, in _open_connection
self._socket.open_connection()
File "/Library/Python/2.7/site-packages/mysql/connector/network.py", line 375, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host xxx.db.1and1.com:3306' (8 nodename nor servname provided, or not known)
Here's the code I'm using:
import mysql.connector
cnx = mysql.connector.connect(user='xxx', password='xxx',
host='xxx.db.1and1.com',
database='xxx')
cnx.close()
Where am I going wrong?
Did you specify the correct port?
Is your MySQL server running?
Is a firewall blocking access?
Try removing anonymous user account from your MySQL server?
Default port if not specified is 3306. Otherwise there is nothing wrong with your code. The problem is with your MySQL server or the connection is being blocked by your firewall or the server firewall. Make sure port 3306 is open and not blocked.
db = mysql.connector.connect(user='xxx', password='xxx', host='xxx.db.1and1.com', port=3306)
I believe the answer is because the hosting service you use, 1and1.com, uses a firewall that blocks all outside access to their databases. You have to run the code from a url that they host or it will not work.
I use the same server and had to figure this out the hard way myself. Below is a copy of what they have on one of their pages:
Access to the database via your website/presence only Please always
establish the connection to your database via your website/presence.
For security reasons, it is not possible to access the database
directly, for example via your local computer (external ODBC
connection).
To protect your data, your MySQL database is located on a dedicated
database server that is protected by a firewall.
Add a port argument in the command.
cnx = mysql.connector.connect(user='myuser',
password='mypassword', host='localhost', port='3306', database='mydb')
I got the same error. Once crosscheck the details you have entered. The host must be in the format 127.0.0.1. There should not be any / after the url.
Use localhost instead of IP, if you are working on same server.
myConnection = mysql.connector.connect(user='uname', password='pass', host='localhost', port='3306', database='databasename')

Categories