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
Related
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)
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)
I tried to push my Flask app to heroku and my app uses Postgres as db. However, when I tried to migrate db on heroku using this command
heroku run python manage.py db init --app app
It gives this "too many clients" error for both psycopg2 and sqlalchchemy.
Running python manage.py db migrate on ⬢ petscom... up, run.5707 (Hobby)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2285, in _wrap_pool_connect
return fn()
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
rec = pool._do_get()
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 238, in _do_get
return self._create_connection()
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 657, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 488, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: sorry, too many clients already
FATAL: sorry, too many clients already
After a bit googling, I tried:
heroku pg:killall
heroku restart
None of those works.....
And I checked heroku pg, it shows 0/20 connections......
Some said dont put db connection in loop, but I have to check if some data is in db. Here's my code:
try:
connection = psycopg2.connect(user="postgres",
password="pass",
host="127.0.0.1",
port="5432",
database="pets")
cursor = connection.cursor()
select_all = "select * from pets"
cursor.execute(select_all)
# Check if api data in db and save to db
for index in range(len(myData)):
# if not exist in db => save to db
key = myData[index]["animal_id"]
exists = check_exists(key, cursor)
if(exists):
pass
else:
doSomething()
I can connect to my local postgres db but not the db on heroku.
How can I deal with this problem? Any advise would help :) Thanks!
I had a similar problem. I was able to fix it by setting SQLALCHEMY_TRACK_MODIFICATIONS=False in the environment, destroying the database, and creating a new one.
You can destroy it by going to https://data.heroku.com/datastores, opening the database, going to Settings, and selecting 'Destroy Database'.
Then provision a new one as you normally would.
I'm trying to create a small app with Python and MySQL.
I managed to connect Python and MySQL using the MySQL connection. But now when I try to run this script to actually connect to the database:
import mysql.connector
mydb = mysql.connector.connect(
passwd="12345",
db="DB",
host="hostname",
user="root"
)
print(mydb)
I get a lot of errors like:
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 485, in open_connection
socket.SOL_TCP)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
File "c:/Users/Krystian/Desktop/DB/db.py", line 7, in <module>
user="root"
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 94, in __init__
self.connect(**kwargs)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 722, in connect
self._open_connection()
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 207, in _open_connection
self._socket.open_connection()
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 501, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'hostname:3306' (11001 getaddrinfo failed)
I tried other ways of conecting to Database but they didn't work and I'm not really sure what to do now.
Thanks in advance for any help!
EDIT1
As Juergen mentioned, after fixing the hostname most of the errors above are gone, but new ones showed up:
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 94, in __init__
self.connect(**kwargs)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 722, in connect
self._open_connection()
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 211, in _open_connection
self._ssl)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 141, in _do_auth
auth_plugin=self._auth_plugin)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\protocol.py", line 102, in make_auth
auth_data, ssl_enabled)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\protocol.py", line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supporte
I guess, your computer (where the database runs) is not named "hostname":
host="hostname",
If it is the local machine, try localhost. "hostname" is just a placeholder inside example coding. You always have to replace such.
About your edit:
Looks as your db server is misconfigured. Maybe you need that plugin that is named in the error message.
try this :
mysql.connector.connect(
host="localhost",
user="user_name",
passwd="db_password",
database="db_name")
I have a server built in python, using Tornado and RethinkDB. Its been running for a long time, opening several connections to the DB per user. I never had any major problems by leaving them open -never closing them-. But checking out the log file I found out there's a lot of this warnings:
File "/home/bundleroot/commentserver/app.py", line 30, in <module>
db_connection = r.connect(RDB_HOST,RDB_PORT)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 542, in connect
return conn.reconnect(timeout)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 475, in reconnect
return self._instance.connect(timeout)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 360, in connect
self._socket = SocketWrapper(self, timeout)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 268, in __init__
(self.host, self.port, ex))
rethinkdb.errors.RqlDriverError: Could not connect to localhost:28015. Error: [Errno 111] Connection refused
Traceback (most recent call last):
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 244, in __init__
socket.create_connection((self.host, self.port), timeout)
File "/usr/lib/python3.4/socket.py", line 509, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 500, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Should I worry about it? In that case, what are the good practices?
Yes, you need to close the connection to avoid connection leaks. This Flask example, from RethinkDB, does it:
https://github.com/rethinkdb/rethinkdb-example-flask-backbone-todo/blob/master/todo.py#L47-L65
Even if RethinkDB does not enforce a hard limit on number of connections (I'm not sure if it does or not), you can run into the limits of the OS.