Having trouble connecting with MySQL.connector in python - python

I am learning how to do MySQL in python using the mysql.connector module. However, whenever I try creating a connection, I get the error "mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)".
I done the exact same as the example that I am using states, and it still isn't working.
Below is the two different type of the example provided:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
print(mydb)
and
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="myusername",
password="mypassword"
)
print(mydb)
I tried exactly what the example said, and it didn't work. I also tried using a different username and password, but kept on getting the same error.
Any idea what I can do to get the expected output which, according to the second example that I am referring to, is:
<mysql.connector.connection.MySQLConnection object ar 0x016645F0>
?
Thank you in advance

import mySQL.connector
Mybd=mysql.connector.connect(
host='myusername',
user='myusername',
password='mypassword',
database='mydatabase')
Cursor=mycon.cursor()
Cursor.execute('show databases')
Data=cursor.fetchall()
for row in data:
Print(row)
mycon.close()

Related

MySQL connection with Python is not working

I want to connect with MySQL through Python, although I am successful in connecting using directly writing the credentials like user name, passwd, etc, but I want to make it a little dynamic. So I tried:
import mysql.connector
import pandas as pd
def connection():
host=input('Enter host')
user=input('Enter User Name')
passwd=int(input('passwd'))
database=input('database')
mydb= mysql.connector.connect(host="host", user="user", passwd="passwd", database="database")
return mydb
connection()
However, it gives error in the end:
InterfaceError: 2003: Can't connect to MySQL server on 'host:3306' (11001 getaddrinfo failed)
Please help me out. And if you have better solution while bringing some dynamic user input credential, I will be really grateful. Thank you.
You are passing stirng like "host" to the database and not the variables
So remove the double quotes like
import mysql.connector
import pandas as pd
def connection():
host=input('Enter host')
user=input('Enter User Name')
passwd=int(input('passwd'))
database=input('database')
mydb= mysql.connector.connect(host=host, user=user, passwd=passwd, database=database)
return mydb
connection()
In the connect method of the mysql.connector you are hardcoding the values when you pass them as strings.
Use mysql.connector.connect(host=host, user=user, passwd=passwd, database=database) instead.
You're also parsing the password to a int, I don't think mysql will like that, just use it as a string.

Python & MySQL mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

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

How to fix this error "Connection Error with MySQL database" in python

I an setting up the connection with the MySQL server.
This is the code I wrote and got the errors:
import mysql.connector as mysql
def user_registration():
connection_to_mysql = mysql.connect(host='localhost', user='root',
passwd='aWais$777')
Please first trying with double quotes i.e. " " with password parameter passing. If not success then trying double quotes with all passing parameter i.e. host, user and password.
Because of passwd field contains this symbol '$' that's why not accepting.
For more info visit check this code: https://www.w3schools.com/python/python_mysql_getstarted.asp
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
Try using password instead of passwd and make sure to specify database.

Can't connect to pymssql

I can't connect to pymssql I keep geteing the error 18456. The problem might be that I don't know the right sintax for the connection.
import pymssql
conn = pymssql.connect(server='GABRIEL-PC',
user='GABRIEL#GABRIEL-PC',
password='',
database='mydb')
The server I found by using SQLCMD -L, but how can I get the user? Should this be working?
When I use MySQL and wrapserver I don't get this kind of problem, and it works fine
conn = (server='mysql://root:#localhost/mydb')

Pymysql Windows Authentication Error

I am attempting to connect to SQL Server Management Studio. My code is below and returns the error: "init() got an unexpected keyword argument 'trusted'." I am using pymysql, would like to connect using Windows Authentication, and am running on 64 bit windows machine. Any help would be appreciated.
Edit - removed trusted. Error I am now receiving is 'No connection could be made because the target machine actively refused it'
import pymysql
import pymysql.cursors
conn = pymysql.connect(host = 'DESKTOP-6CIMC97')
Have you checked out the example on GitHub? trusted doesn't seem to be required.
https://github.com/PyMySQL/PyMySQL/blob/master/example.py
#!/usr/bin/env python
from __future__ import print_function
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()

Categories