Unable to Connect to database in phpmyadmin through Python - python

I'm trying to connect to database of https://www.freesqldatabase.com but it's not connecting through given code.
But as I connect to my database through phpmyadmin it connects.
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass',
host='host',
database='database',
port='3306')
try:
cursor = cnx.cursor()
cursor.execute("""
select * FROM USERS
""")
result = cursor.fetchall()
print(result)
finally:
cnx.close()
Everytime I run this code I get these errors.
Traceback (most recent call last):
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\network.py", line 482, in open_connection
addrinfos = socket.getaddrinfo(self.server_host,
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "as.py", line 2, in <module>
cnx = mysql.connector.connect(user='user', password='pass',
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
self._open_connection()
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection
self._socket.open_connection()
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\network.py", line 500, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host:3306' (11001 getaddrinfo failed)

Related

Handle internet exception on python script startup

I want a python script to execute on startup, so for that, I created a bat file and a vbs file
and it works when there is internet but most of the time when I turn on the pc and log in, the script launches before the pc connects to the internet and it gives me this error:
Traceback (most recent call last):
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\util\connection.py", line 72, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\ACERq\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 358, in connect
conn = self._new_conn()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000020F127D5FD0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
Traceback (most recent call last):
File "d:\Python-projects\Personal_assistant\Friday.py", line 5, in <module>
import pywhatkit as kit
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\__init__.py", line 16, in <module>
from pywhatkit.whats import (
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\whats.py", line 13, in <module>
core.check_connection()
File "D:\Python-projects\Personal_assistant\.venv\lib\site-packages\pywhatkit\core\core.py", line 41, in check_connection
raise InternetException(pywhatkit.core.exceptions.InternetException: Error while connecting to the Internet. Make sure you are connected to the Internet!
I am also using a virtual environment for the dependencies. Is there any way I can handle these exceptions or any way to execute a python script only after getting connected to the internet. Any help would be appreciated!
.bat file
#echo off
python D:\Python-projects\Personal_assistant\Friday.py
#pause
.vbs file
CreateObject("Wscript.Shell").Run "D:\Python-projects\Personal_assistant\Friday.bat", 0 , True
:wait
timeout /t 1 >nul
ping -n 1 google.com|find "TTL">nul
if errorlevel 1 goto wait
Should ping google.com every second until a response is received containing TTL

Can't connect to RDS database with PyMySQL

I'm trying to connect to an RDS database in AWS but i get the following traceback:
File "/Users/andreschaves/miniconda3/lib/python3.8/site-packages/pymysql/connections.py", line 613, in connect
sock = socket.create_connection(
File "/Users/andreschaves/miniconda3/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/Users/andreschaves/miniconda3/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Server.py", line 133, in <module>
main(sys.argv)
File "Server.py", line 31, in main
cur, connection = start_connection(host, user, psswd, db_name)
File "Server.py", line 48, in start_connection
connection = pymysql.connect(host=host, user=user, password=psswd)
File "/Users/andreschaves/miniconda3/lib/python3.8/site-packages/pymysql/connections.py", line 353, in __init__
self.connect()
File "/Users/andreschaves/miniconda3/lib/python3.8/site-packages/pymysql/connections.py", line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '**********' (timed out)")
When i created the database i check that i had public access.
This is the code i'm using for the connection:
connection = pymysql.connect(host=host, user=user, password=psswd, database=db_name)

SQLAlqhemy database operational error 11001

When I try to user the command db.create_all() to build my sqlalchemy database (with flask-sqlalchemy) for my flask application I get an issue, and I can not fix this, this is the error code:
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\pymysql\connections.py", line 614, in connect
(self.host, self.port), self.connect_timeout, **kwargs
File "C:\Program Files\Python37\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python37\lib\socket.py", line 752, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\sqlalchemy\engine\default.py", line 509, in connect
return self.dbapi.connect(*cargs, **cparams)
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\pymysql\connections.py", line 353, in __init__
self.connect()
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\pymysql\connections.py", line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'dbserver' ([Errno 11001] getaddrinfo failed)")
I hope this will help other people, to fix this error I verified my database connection URL, and that was incorrect.

I tried to make my first database and I got error

I just started learning MySql for Python on W3Schools. I copied the code that was given as an example and I run it in IDLE, but I got an error.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="myusername",
passwd="mypassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
Here is the full traceback:
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 509, in open_connection
self.sock.connect(sockaddr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/risto/AppData/Local/Programs/Python/Python37-32/mysql1.py", line 6, in <module>
passwd="mypassword"
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
self._open_connection()
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection
self._socket.open_connection()
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 512, 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 No connection could be made because the target machine actively refused it)
Can someone help me?
the service about MySQL is not running, or filtered by some firewall rule.
Check it out

socket.gaierror: [Errno 11004] getaddrinfo failed Python

Im currently having a problem with a Python program I made. The program is supposed to connect to google translate using the http.client module. My program works if im just connection to google.com, but it gives me an error when I try to connect to translate.google.com.
import urllib.request,re,http.client
while True:
print('')
question = input(">: ").replace(" ","%20").replace("?",'%3F')
x = "/#auto/es/%s"%(question)
conn = http.client.HTTPSConnection("www.translate.google.com",80)
http.client.HTTPConnection.connect(conn)
conn.request("GET",x)
response = conn.getresponse()
data = str(response.read())
conn.close()
print("Done")
Traceback (most recent call last):
File "P:\CS5\translater.py", line 9, in <module>
http.client.HTTPConnection.connect(conn)
File "C:\Program Files\Python35-32\lib\http\client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Program Files\Python35-32\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python35-32\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed

Categories