Can't connect to Cassandra using python but can through cqlsh - python

I'm trying to test my pod connection to Cassandra using python. This is the code that I have:
host = '<hostname>'
port_num = 9092
default_fetch_size = 5000
idle_heartbeat_interval = 30
idle_heartbeat_timeout = 30
connect_timeout = 10
executor_threads = 2
protocol_version = 4
db_keyspace = 'fraud'
auth_provider = PlainTextAuthProvider(username=username, password=password)
cluster = Cluster([host], port=port_num, auth_provider=auth_provider,
idle_heartbeat_interval=idle_heartbeat_interval,
idle_heartbeat_timeout=idle_heartbeat_timeout,
connect_timeout=connect_timeout,
protocol_version=protocol_version,
executor_threads=executor_threads)
session = cluster.connect()
And I get the error:
Traceback (most recent call last):
File "run.py", line 3, in <module>
CassProvider().test_cassandra()
File "/app/tmp_cassandra_connection.py", line 59, in test_cassandra
cassandra_connector = CassandraDbConnector().get_session()
File "/usr/local/lib/python3.6/site-packages/project/src/objects/type_objects.py", line 11, in __call__
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
File "/app/tmp_cassandra_connection.py", line 41, in __init__
session = cluster.connect()
File "cassandra/cluster.py", line 1664, in cassandra.cluster.Cluster.connect
File "cassandra/cluster.py", line 1700, in cassandra.cluster.Cluster.connect
File "cassandra/cluster.py", line 1687, in cassandra.cluster.Cluster.connect
File "cassandra/cluster.py", line 3485, in cassandra.cluster.ControlConnection.connect
File "cassandra/cluster.py", line 3530, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'<ip>:9092': OSError(None, "Tried connecting to [('<ip>', 9092)]. Last error: timed out")
but when I get on the same pod using bash I can connect to Cassandra using cqlsh without a problem using the same host, default port, username and password.
Do you know why the python code on the pod gives me problems but the cqlsh is working perfectly?
Thanks

'Unable to connect to any servers', {'<ip>:9092':
Try setting the port to 9042, which is the default.
port_num = 9042

Related

Cant connect to sql server with mysql-connector

I'm trying to connect to mySQL using mysql-connector and I'm getting an error:
runfile('C:/Users/Idan/Desktop/תכנות/ניסויים/idk/adding_test/connect_to_db.py', >wdir='C:/Users/Idan/Desktop/תכנות/ניסויים/idk/adding_test')
Traceback (most recent call last):
File "C:\Users\Idan\Desktop\תכנות\ניסויים\idk\adding_test\connect_to_db.py", >line 13, in
database = 'temp'
File "C:\Users\Idan\anaconda3\lib\site-packages\mysql\connector_init_.py", >line 271, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Idan\anaconda3\lib\site->packages\mysql\connector\connection.py", line 95, in init
self._in_transaction = False
File "C:\Users\Idan\anaconda3\lib\site-packages\mysql\connector\abstracts.py", >line 985, in connect
self._open_connection()
File "C:\Users\Idan\anaconda3\lib\site->packages\mysql\connector\connection.py", line 205, in _open_connection
ssl_options.get('verify_identity') or
File "C:\Users\Idan\anaconda3\lib\site->packages\mysql\connector\connection.py", line 193, in _get_connection
if client_flags & ClientFlag.SSL:
File "C:\Users\Idan\anaconda3\lib\site-packages\mysql\connector\network.py", >line 565, in init
super(MySQLTCPSocket, self).init()
TypeError: super(type, obj): obj must be an instance or subtype of type
I have tried searching online and I can't find problem with my code (and yes the password is correct):
import mysql.connector as mysql
db = mysql.connect(
host = 'localhost',
user = 'root',
password = '10iDun7lEvy3',
database = 'temp'
)
c = db.cursor()

Error while write into dockerized MongoDB

I have run mongo db in docker container with command:
docker run -d -p 127.0.0.1:27017:27017 --name my-mongo -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=pass mongo:4.4.0
It seems to work well.
I try to connect to this Mongo and make some writings with python libraries (I tried with synchronous and asynchronous one). Here is an example of code of motor library:
import asyncio
import motor.motor_asyncio
def initialization():
obj_client = motor.motor_asyncio.AsyncIOMotorClient(
host="127.0.0.1",
password="pass",
port=27017,
username="root"
)
obj_database = obj_client["test"]
obj_collection = obj_database["collection"]
return obj_database, obj_collection
async def do_insert(db, collection):
document = {'key': 'value'}
result = await collection.insert_one(document)
print('result %s' % repr(result.inserted_id))
if __name__ == "__main__":
obj_client, obj_database, obj_collection = initialization()
loop = asyncio.get_event_loop()
loop.run_until_complete(do_insert(obj_client, obj_database, obj_collection))
I get an error while insert_one() is been calling:
Traceback (most recent call last):
File "/Users/user1/Documents/python_projects/unsorted/mongo_inersection.py", line 25, in <module>
loop.run_until_complete(do_insert(obj_client, obj_database, obj_collection))
File "/usr/local/Cellar/python#3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/Users/user1/Documents/python_projects/unsorted/mongo_inersection.py", line 19, in do_insert
result = await collection.insert_one(document)
File "/usr/local/Cellar/python#3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.8/site-packages/pymongo/collection.py", line 698, in insert_one
self._insert(document,
File "/usr/local/lib/python3.8/site-packages/pymongo/collection.py", line 613, in _insert
return self._insert_one(
File "/usr/local/lib/python3.8/site-packages/pymongo/collection.py", line 602, in _insert_one
self.__database.client._retryable_write(
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1497, in _retryable_write
with self._tmp_session(session) as s:
File "/usr/local/Cellar/python#3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1829, in _tmp_session
s = self._ensure_session(session)
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1816, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1766, in __start_session
server_session = self._get_server_session()
File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1802, in _get_server_session
return self._topology.get_server_session()
File "/usr/local/lib/python3.8/site-packages/pymongo/topology.py", line 485, in get_server_session
self._select_servers_loop(
File "/usr/local/lib/python3.8/site-packages/pymongo/topology.py", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 61] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 5f67d9e04b411f070c264a7d, topology_type: Single, servers: [<ServerDescription ('127.0.0.1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 61] Connection refused')>]>
[Finished in 31.0s with exit code 1]
[cmd: ['/usr/local/bin/python3', '-u', '/Users/user1/Documents/python_projects/unsorted/mongo_inersection.py']]
[dir: /Users/user1/Documents/python_projects/unsorted]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
You can check if the IP of the docker container that runs the MongoDB is the one (the host) you are using to connect to MongoDB:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-mongo
Problem is that I use docker on MacOS in docker-machine (I forgot about it):
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v19.03.5
I used 192.168.99.100:27017 to connect to MongoDB and problem was resolved. Thanks a lot

Python package APSscheduler throws an error when starting scheduler if mongodb is used as a jobstore

I'm trying to run some code with APScheduler with MongoDB as a jobstore. I've downloaded pymongo and I tested it, so it does work. APScheduler will work when I instantiate the scheduler, but when I run scheduler.start() it throws the following error:
Traceback (most recent call last):
File "aps_ro.py", line 56, in <module>
scheduler.start()
File "/usr/lib/python3.8/site-packages/apscheduler/schedulers/background.py", line 33, in start
BaseScheduler.start(self, *args, **kwargs)
File "/usr/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 158, in start
store.start(self, alias)
File "/usr/lib/python3.8/site-packages/apscheduler/jobstores/mongodb.py", line 57, in start
self.collection.ensure_index('next_run_time', sparse=True)
File "/usr/lib/python3.8/site-packages/pymongo/collection.py", line 2028, in ensure_index
self.__create_index(keys, kwargs, session=None)
File "/usr/lib/python3.8/site-packages/pymongo/collection.py", line 1881, in __create_index
with self._socket_for_writes(session) as sock_info:
File "/usr/lib/python3.8/site-packages/pymongo/collection.py", line 195, in _socket_for_writes
return self.__database.client._socket_for_writes(session)
File "/usr/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1266, in _socket_for_writes
server = self._select_server(writable_server_selector, session)
File "/usr/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "/usr/lib/python3.8/site-packages/pymongo/topology.py", line 233, in select_server
return random.choice(self.select_servers(selector,
File "/usr/lib/python3.8/site-packages/pymongo/topology.py", line 192, in select_servers
server_descriptions = self._select_servers_loop(
File "/usr/lib/python3.8/site-packages/pymongo/topology.py", line 208, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
My actual code sample is as follows:
jobstores = {
'mongo': {'type': 'mongodb'},
'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
}
executors = {...}
job_defaults = {...}
scheduler = BackgroundScheduler()
scheduler.configure(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc)
#scheduler.scheduled_job(trigger='cron', day_of_week=4, hour=22, minute=21, second=0)
def tester():
print("hello")
scheduler.start()
By default, the mongodb python driver will connect to a database on host localhost and port 27017.
So you will need to either:
make sure mongodb server is running on the same machine as the python code and is running, on the default port (you can test this easily by typing mongo at your favourite shell and checking you get a > prompt and not an error).
supply a connection string or connection arguments if mongodb is running on a different server or a different port.

How to connect web2py with mysql on Windows 10

After adding
db = DAL('mysql://admin:admin#localhost/web2py')
I got this errors..
Failure to connect, tried 5 times: Traceback (most recent call last): File "C:\Projects\web2py\gluon\packages\dal\pydal\base.py", line 457, in init self._adapter = adapter(**kwargs) File "C:\Projects\web2py\gluon\packages\dal\pydal\adapters__init__.py", line 39, in call obj = super(AdapterMeta, cls).call(*args, **kwargs) File "C:\Projects\web2py\gluon\packages\dal\pydal\adapters\base.py", line 369, in init super(SQLAdapter, self).init(*args, **kwargs) File "C:\Projects\web2py\gluon\packages\dal\pydal\adapters\base.py", line 53, in init self.reconnect() File "C:\Projects\web2py\gluon\packages\dal\pydal\connection.py", line 154, in reconnect self.connection = self.connector() File "C:\Projects\web2py\gluon\packages\dal\pydal\adapters\mysql.py", line 51, in connector return self.driver.connect(**self.driver_args) File "MySQLdb/init.py", line 81, in Connect File "MySQLdb/connections.py", line 193, in init TypeError: 'password' is an invalid keyword argument for this function
I have installed mysql, mysql-connector for python, mysql-client, python2.7 and so on
db = DAL('mysql://admin:admin#localhost/web2py')
I expecting this will be connect but not 😒😒😒
Try something like this
uri = mysql://user:password#host/databasename
And also, use Python 3 goodluck :)))

cassandra.cluster.NoHostAvailable "Unable to connect to any servers" for cassandra-driver database

When I use this command:
session = cluster.connect('demo')
...it gives me an error saying:
>>> session = cluster.connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "cassandra/cluster.py", line 1152, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:17598)
File "cassandra/cluster.py", line 1185, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:17419)
File "cassandra/cluster.py", line 1172, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:17122)
File "cassandra/cluster.py", line 2618, in cassandra.cluster.ControlConnection.connect (cassandra/cluster.c:47646)
File "cassandra/cluster.py", line 2655, in cassandra.cluster.ControlConnection._reconnect_internal (cassandra/cluster.c:48543)
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1': ConnectionRefusedError(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
I am on Fedora.
Here is the standard process to connect to a Cassandra instance using the DataStaX python driver (cassandra-driver)
from cassandra.cluster import Cluster
from cassandra.policies import DCAwareRoundRobinPolicy
from cassandra.auth import PlainTextAuthProvider
def cassandra_conn():
auth_provider = PlainTextAuthProvider(username='cassandra', password='password')
cluster = Cluster(['127.0.0.1'], load_balancing_policy=DCAwareRoundRobinPolicy(local_dc='US-WEST'), port=9042, auth_provider=auth_provider)
session = cluster.connect()
return session, cluster
Note: I have included connection credentials based on my local cassandra cluster. However, these credentials would be different when connecting to a different instance.

Categories