I am using mysql.connector to connect to a mysql DB, while i can connect manually to the db using sql server management, if i try connecting via code, it returns this error after awhile :
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'host:1234', system error: 10054 An existing connection was forcibly closed by the remote host
These are the connection details :
connection = mysql.connector.connect(host='host',
port = '1234',
database='DBname',
user='Usr',
password='pwd')
If I create a local mysql DB, the connection works just fine.
I assume some security stuff is going on, anyone else had encountered this situation ? Anything that I'm doing wrong? Should I add anything to the connection.connect input ?
Full code for reference :
import mysql.connector
from mysql.connector import Error
connection = mysql.connector.connect(host='host',
port = '1234',
database='DBname',
user='Usr',
password='pwd')
sql_select_Query = "select * from TableName"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
print("Total numb of rows selected is : ", cursor.rowcount)
print("\nPrinting each row")
for row in records:
print(row)
connection.close()
Related
Wrote a code to create a sql database via Python:
import mysql.connector
from mysql.connector import Error
def create_connection(host_name, user_name, user_password):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
connection = create_connection("localhost", "phpmyadmin","mhldb2022!")
But I get an error message:
The error '2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (99)' occurred
In order for the database to be created in phpMyAdmin, I installed WAMP.
Access to phpMyAdmin :
localhost/phpmyadmin/
(default login is "root", no password).
Could you help me to solve the problem?
I really can't understand what is wrong here.
I'm trying to use import mysql.connector to SELECT * from a table with ~1.9million rows in a remote database.
When I run the same query using the mysql cli client everything works just fine so I don't believe the issue is network or serverside. I've tried increasing connection_timeout to ridiculous numbers, but I still run into mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
Any ideas what to try next?
import mysql.connector
mydb = mysql.connector.connect(
host="mysql.prod.example.com",
user="user",
password="pass",
database="db",
auth_plugin='mysql_native_password',
connection_timeout=1000000
)
mycursor = mydb.cursor(dictionary=True)
mycursor.execute("SELECT * FROM table")
update_list = []
for row in mycursor:
row_id = row["id"]
row_ip = row["ip"]
print("IP: "+row_ip)
newlist = [row_id,row_ip,"/128"]
update_list.append(newlist)
Switching from Python2 to 3 solved my problem.
- #!/usr/bin/python
+ #!/usr/bin/python3
I am having problems connecting to a remote Oracle DB using cx_Oracle in my python application.
I have tried a lot of different ways of configuring/formulating my connection string based on a lot of google searching etc but I seem to get the same error message almost each time no matter what I try.
My attempts looks like this:
import cx_Oracle
ip = '[IP ADDRESS]'
port = [PORT]
service_name = '[SERVICE NAME]'
dsn = cx_Oracle.makedsn(ip, port, service_name=service_name)
db = cx_Oracle.connect('[USERNAME]', '[PASSWORD], dsn)
Result: DatabaseError: ORA-12170: TNS:Connect timeout occurred
import cx_Oracle
conn_str = '[USERNAME]/[PASSWORD]#[HOST IP]/[SERVICE NAME]'
conn = cx_Oracle.connect(conn_str)
Result: DatabaseError: ORA-12170: TNS:Connect timeout occurred
import cx_Oracle
user= '[USERNAME]'
pwd = '[PASSWORD]'
host = '[HOST IP]'
service_name = '[SERVICE NAME]'
portno = '[PORT]'
conn = cx_Oracle.connect(user, pwd, '{}:{}/{}'.format(host,portno,service_name))
Result: DatabaseError: ORA-12170: TNS:Connect timeout occurred
import cx_Oracle
connstr = '[USERNAME]/[PASSWORD]#[SERVICE NAME]'
conn = cx_Oracle.connect(connstr)
Result: DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
I have Toad installed on my PC and have no problems what so ever connecting to the DB with that.
Any ideas what could be the problem ?
Thanks in advance
I have this standard way to connect
import cx_Oracle
host="myserver"
port=myport
sid='myservicename'
user='myuser'
password='mypassword'
sid = cx_Oracle.makedsn(host, port, service_name=sid)
connection = cx_Oracle.connect(user, password, sid, encoding="UTF-8")
cursor = connection.cursor()
cursor.execute('select 1 from dual')
And it works without any issue. In your case, it is quite strange that you got a timeout, which normally indicates a network problem rather than a connection issue. If you are using cx_Oracle version 8, you don't need to specify the encoding as UTF-8 as it is the default one.
See how it works.
C:\python>type testconn.py
#from __future__ import print_function # needed for Python 2.7
import cx_Oracle
import os
host="myserver"
port=myport
sid='database_service_name'
user='myuser'
password='mypassword'
sid = cx_Oracle.makedsn(host, port, service_name=sid)
connection = cx_Oracle.connect(user, password, sid, encoding="UTF-8")
cursor = connection.cursor()
cursor.execute('select 1 from dual')
for row in cursor:
print(row)
C:\python>python testconn.py
(1,)
C:\python>
I am not able to connect to MySQL sever using python it gives and error which says
MySQLdb._exceptions.OperationalError: (1130, "Host 'LAPTOP-0HDEGFV9' is not allowed to connect to this MySQL server")
The code I'm using:
import MySQLdb
db = MySQLdb.connect(host="LAPTOP-0HDEGFV9", # your host, usually localhost
user="root", # your username
passwd="abcd13de",
db="testing") # name of the data base
cur = db.cursor()
cur.execute("SELECT * Employee")
for row in cur.fetchall():
print(row[0])
db.close()
This is an authorization problem not a connectivity problem. Is the db running locally? If not, confirm with the admin where it is hosted. If so, try changing the host parameter to 127.0.0.1?
As described here the admin can get the hostname by running:
select ##hostname;
show variables where Variable_name like '%host%';
If the connection was timing out you could try setting the connect_timeout kwarg but that's already None by default.
I'm trying to connect to my SQL database on pythonanywhere and to connect I'm running this.
import MySQLdb
conn = MySQLdb.connect("MYUSERNAME.mysql.pythonanywhere-services.com","MYUSERNAME","DBPASSWORD","DBTABLENAME")
c = conn.cursor()
c.execute("SELECT * from game_db")
rows = c.fetchall()
for eachrow in rows:
print(eachrow)
I'm doing this from my laptop and I get this error when trying to connect.
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on MYUSERNAME.mysql.pythonanywhere-services.com' (10060)")
Is there some sort of authentication I need to do or is there some other problem.
Thank you!
the pythonanywhere mysql service isn't accessible from outside pythonanywhere (unless you have a paid account, in which case you can use an ssh tunnel: https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/)