I am coming across an issue with mysql connector.
I have an environment that i can't connect from my local machine.
I use simple command for connection:
from mysql.connector import connect
connection = mysql.connector.connect(user='dbuser', database='dbname',
host='amazon_link', password='dbpassword')
This works for all environment except one. And this also works in the server as well where the database lives.
I can access the database using my MySqlWorkbench from local machine. But when i try from my script, i get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
self._open_connection()
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 210, in _open_connection
self._ssl)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 144, in _do_auth
self._auth_switch_request(username, password)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 177, in _auth_switch_request
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'dbuser'#'10.0.1.72' (using password: YES)
And the ip address that i get for the host link is different from what mysql connector is showing here as well.
Is there a way to avoid mysql connector to change link to url here, as when i use the link instead of ip address in my Workbench i am able to connect. So i doubt it is an issue related to vpn or security groups. Otherwise i wouldn't be able to connect to database via work bench at all.
Any help would be appreciated. Thanks.
And sorry in case i missed something basic here.
Try to use the global IP address of your server instead of the domain name.
Refer the stackoverflow link for more details:
Remotely connect to MySQL with Python mysql.connector
Related
Thanks for taking the time to read through this question.
I've spent quite a bit of time trying to find an answer to this and some time digging through code in pydobc and mysqldb to no avail.
Summary of Problem:
I can connect to a MySQL server using the same method+connection string from many machines, but cannot use the same method from a RHEL server. I continually get an Access denied error; it seems unlikely this is actually the issue though.
Background:
I am using sqlalchemy to connect to a MySQL server hosted on a remote network. I am able to connect to the MySQL server using the same credentials from MySQL Workbench, MySQL Shell, my local Jupyter Notebook, local Python scripts, a virtual Ubuntu 18.04 WSL1 instance, and the troublesome RHEL server- but I cannot use the same connection method on RHEL as the rest of the instances.
I am positive my user has access to read/write tables on the MySQL server (and ostensibly make ODBC connections since I believe I'm doing so from my laptop), so despite the error below, I'm having a hard time believing it is actually a true permission error. That said, I'm happy to listen to any suggestions.
Problem:
When connecting from any of the sources listed above except for the RHEL server, I am able to utilize this code to create an engine and subsequently a connection:
engine = sqlalchemy.create_engine('mysql+mysqldb://{user}:{password}#{server}:{port}/{database}?charset=utf8mb4'.format(user=user
,password=password
,server=server
,port=port
,database=database))
con = engine.connect()
I am then able to use that connection to do any number of things- pd.to_sql(), direct code executions- works like a charm.
The issue comes up when I try to use that same connection info on the RHEL Server, I get the following error:
>>> engine.connect()
Traceback (most recent call last):
File "/python_dir/python3.8/site-packages/sqlalchemy/engine/base.py", line 3211, in _wrap_pool_connect
return fn()
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
self._dec_overflow()
File "/python_dir/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/python_dir/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
self.__connect()
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/python_dir/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/python_dir/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/python_dir/python3.8/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
connection = pool._invoke_creator(self)
File "/python_dir/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/python_dir/python3.8/site-packages/sqlalchemy/engine/default.py", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/python_dir/python3.8/site-packages/MySQLdb/__init__.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/python_dir/python3.8/site-packages/MySQLdb/connections.py", line 187, in __init__
super().__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'USER'#'SERVER_ADDRESS' (using password: YES)")
Obviously I've changed a few things in the error for security, but that last line is the only real error I see:
MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'USER'#'SERVER_ADDRESS' (using password: YES)")
Connecting from RHEL
Despite the failure using the connection info above, I am able to connect to the MySQL server from the RHEL server with a different connection setup:
engine = sqlalchemy.create_engine('mysql+pyodbc://my_dsn')
By using pyodbc, I'm able to connect utilizing a DSN (I think DSN is the right term- this is getting a bit murky for me) that I have configured on the RHEL server in /etc/odbc.ini
Here's an example of what I have for that specific entry (I've retained capitalizations + blanks where they exist in the file, but have changed all sensitive fields):
[ODBC Data Sources]
my_dsn = My Special DSN
[my_dsn]
Driver = /dir/libmyodbc5.so
Description = Description
SERVER = Server
PORT = Port
USER = User
Password = Password
Database = Database
OPTION = 3
SOCKET =
The only major difference that I see between the DSN configuration listed above and the connection string that I'm providing to mysqldb is that the DSN lets me specify the OPTION parameter. I can't figure out how pyodbc uses this, though.
I've tried digging through the documentation from quite a few sources (sqlalchemy,pyodbc,MySQL,StackOverflow,SuperUser, direct source code) to find if/how to specify the OPTION for mysqldb to no avail. I'm not sure if that OPTION is really making that big of a difference, but it's the only piece I can't provide.
That being said, I did find this question that explains what the options are a bit more and this corresponding page that the answer came from. Although that doesn't help me much.
Goal
Ideally, I'd like to be able to run my connections the exact same regardless of server or OS. I'm not sure if that's possible, but it's what I'm attempting to do. I would rather not set up the same DSN on every machine that needs to connect (so the pyodbc option I'm using on RHEL isn't ideal), but no other connection method from that RHEL server will work.
Troubleshooting
I don't have a stellar understanding of the drivers/Linux config, so there's definitely a possibility that I've done something wrong here. Here's some stuff I've tried:
Tried many different connection strings and different drivers/connection info specified
Tried installing various different MySQL drivers on RHEL (with varied success)
Read dozens of questions and articles on similar issues
Other janky configuration attempts that didn't help
Wept softly over my keyboard
Configuration Information:
Problem server: RHEL Version 7
Other machines: Ubuntu 18.04, Windows 10
Python: 3.8
SQLAlchemy: RHEL- 1.4.20, Windows- 1.4.15, Ubuntu- 1.4.22
pyodbc: RHEL- 4.0.30, Windows- 4.0.30, Ubuntu- 4.0.31
If I can provide any more information, please let me know! Really appreciate any ideas.
I'm having trouble connecting to a db I set up in Atlas MongoDB.
I have already read multiple other questions but no answer made my connection work.
I want to point out that I am trying to access it behind a company's proxy (if that has anything to do with it).
My code is this and fails only on the last line where I try to count the documents:
from pymongo import MongoClient
client = MongoClient(
"mongodb+srv://name:pass#iliastrialcluster-1tl2y.azure.mongodb.net/test?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE"
)
db = client.get_database('sample_airbnb')
print(db)
listings_and_reviews = db.listingAndReviews
print(listings_and_reviews)
listings_and_reviews.count_documents({})
The error I get is:
Traceback (most recent call last):
File "C:~/mongo_connection1.py", line 11, in <module>
listings_and_reviews.count_documents({})
File "C:~\lib\site-packages\pymongo\collection.py", line 1721, in count_documents
_cmd, self._read_preference_for(session), session)
File "C:~\lib\site-packages\pymongo\mongo_client.py", line 1454, in _retryable_read
read_pref, session, address=address)
File "C:~\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "C:~\lib\site-packages\pymongo\topology.py", line 235, in select_server
address))
File "C:~\lib\site-packages\pymongo\topology.py", line 193, in select_servers
selector, server_timeout, address)
File "C:~\lib\site-packages\pymongo\topology.py", line 209, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: iliastrialcluster-shard-00-00-1tl2y.azure.mongodb.net:27017: timed out,iliastrialcluster-shard-00-02-1tl2y.azure.mongodb.net:27017: timed out,iliastrialcluster-shard-00-01-1tl2y.azure.mongodb.net:27017: timed out
ServerSelectionTimeout error is the client's way of telling you you can't connect to the server. The primary cause of these errors when using MongoDB Atlas is the failure to enable access for the IP address of the node the client is running on.
To verify try connecting using the MongoDB shell mongo using the same connection string. if you get a connection failed then you know it's not a Python problem. Your client code looks OK BTW so I am pretty sure this is what it is.
The connection to the server is lazily evaluated so we don't try to initiate a connection until you make an actual request. In this case the count_documents call. This is why this is the call that generates the error.
#JoeDrumgoole Thanks for your answer. The problem was the proxy after all, after deactivating it the connection works fine. Good to know that the connection to the server is lazily evaluated.
I tried connecting my application to my cluster on mongodb Atlas like the examples but I'm still having trouble.
I used
import pymongo
client = pymongo.MongoClient('mongodb+srv://kay:myrealpassword#cluster0.mongodb.net/test')
I replaced myrealpassword with my password and cluster0 with my cluster's name .
This is the error i got :
Traceback (most recent call last): File "", line 1, in
File
"/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 468,
in init
res = uri_parser.parse_uri(entity, port, warn=True) File "/Library/Python/2.7/site-packages/pymongo/uri_parser.py", line 399,
in parse_uri
nodes = _get_dns_srv_hosts(fqdn) File "/Library/Python/2.7/site-packages/pymongo/uri_parser.py", line 289,
in _get_dns_srv_hosts
raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: None of DNS query names exist:
_mongodb._tcp.saudimood.mongodb.net., _mongodb._tcp.saudimood.mongodb.net.Home.
I was having similar DNS error, I misspelled the DNS seed list :s
To avoid this and another errors try:
In the Clusters view, click Connect for the cluster to which you
want to connect.
Select Connect your application.
Select your driver and version.
Copy the connection string.
Replace <password> with the password for the <username> user. Replace myFirstDatabase with the name of the database that connections will use by default.
Try it
pymongo.MongoClient('mongodb://user:password#servername/dbname')
I've read everything possible and I just can't connect to my mysql server. Here is my code. very simple. (I used xxx to hide all the private info. everything else is the way i got it.
import mysql.connector
conn = mysql.connector.connect(user='xxxx',password='xxxxx',host='xxxx',db='xxx',port=3306)
The login info is definitely correct. I tried it in a mysql client and it connected just fine. The MySQL server is hosted on dreamhost. I set the allowable IPs for the db user to wildcard %.%.%.% so anyone could connect.I don't know what else could possibly be wrong. When I try to connect in python i get:
C:\Python33\python.exe D:/Dropbox/python/v2/test.py
Traceback (most recent call last):
File "D:/Dropbox/python/v2/test.py", line 3, in <module>
conn = mysql.connector.connect(user='xxx',password='xxx',host='xxx',db='xxx',port=3306)
File "C:\Python33\lib\site-packages\mysql\connector\__init__.py", line 101, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 117, in __init__
self.connect(**kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 383, in connect
self._open_connection()
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 350, in _open_connection
self._ssl)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 176, in _do_auth
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1044 (42000): Access denied for user 'xxx'#'%.%.%.%' to database 'xxx'
Any help would be greatly appreciated.
You will have to GRANT permissions to the user.
Something like:
GRANT ALL PRIVILEGES ON *.* TO 'user2'#'localhost' IDENTIFIED BY PASSWORD 'xxxxx'
I'm trying to connect to database built in into a physical device, and I don't want to change any configuration of this mysql server.
When I try to connect i get following error:
Traceback (most recent call last):
File "connect3031.py", line 3, in <module>
cnx = mysql.connector.connect(user='..', database='...', password='...', host='192.168.88.100')
File "/usr/lib/pymodules/python2.7/mysql/connector/__init__.py", line 45, in Connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/mysql/connector/connection.py", line 366, in __init__
self.connect(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/mysql/connector/connection.py", line 411, in connect
ssl=(ssl_ca, ssl_cert, ssl_key))
File "/usr/lib/pymodules/python2.7/mysql/connector/connection.py", line 449, in _open_connection
self._charset)
File "/usr/lib/pymodules/python2.7/mysql/connector/protocol.py", line 350, in do_auth
"Authentication with old (insecure) passwords "\
mysql.connector.errors.NotSupportedError: Authentication with old (insecure) passwords is not supported: http://dev.mysql.com/doc/refman/5.1/en/password-hashing.html
Typical response for such problem is, for example, here mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication, however I don't want to touch this server (apart from selecting data), so updating user is out of question.
Is there any way I can configure client so it accepts these old insecure passwords. I really don't care for security, since it will be accessed via VPN, and all passwords are essentially common knowledge, as they are printed in the manual.
Server in question is (anyways that's what mysqldump tells me:
Server version 4.1.7-max
You probably need to use an other connector.
MySQL-python seems to support MySQL3.23+.
mysql-connector-python simply can't connect using old authentication.