I have an instance of Mongo running and can connect and authenticate successfully to a database. I can bulk insert records using collection.insert([list of records to insert]).
However, when I add safe=True to ensure that the records are inserted, like the following command, I get the error below, which seems like a permissions issue. How can I fix this?
collection.insert(records_to_insert, safe=True)
File "/.../python2.6/site-packages/pymongo/collection.py", line 270, in insert
check_keys, safe, kwargs), safe)
File "/.../python2.6/site-packages/pymongo/connection.py", line 732, in _send_message
return self.__check_response_to_last_error(response)
File "/.../lib/python2.6/site-packages/pymongo/connection.py", line 684, in __check_response_to_last_error
raise OperationFailure(error["err"])
pymongo.errors.OperationFailure: unauthorized
You are running in MongoDB in auth mode and did not provide the related the related credentials upon connection time. Calling db.authenticate(...) should be your friend.
Related
I created a system where sometimes I need to batch delete some records. I am using Spring and after using deleteAllBy(String fieldName) I get the following:
Write operation error on server xxxxxxx.mongo.cosmos.azure.com:10255. Write error: WriteError{code=16500, message='Error=16500, RetryAfterMs=1, Details='Batch write error.', details={}}.
Any idea what this is and how to prevent it?
I am trying to use the external database log plugin in Moodle to copy over the standard log table into an external database for easier access to do some analytics work.
I activated the external db log and added all correct settings on the settings page. I clicked "test connection" and it connected successfully and returned the table column headers successfully. But if I click around and make some logs, they are visible in the standard log store but my external db table is still empty.
So I tried connecting to my external db locally in TablePlus using identical credentials as I put in the external db log store settings, and I could connect and write successfully.
Next I went into the live logs and picked standard logs, and they showed up just fine. Then I clicked on external db logs (nothing inside except for 2 manually entered rows of data), and got this error:
URL: https://ohsu.mrooms3.net/
Debug info: ERROR: syntax error at or near "{" LINE 1: SELECT COUNT('x') FROM {OpenLMSLog} WHERE courseid = $1 AND ... ^ SELECT COUNT('x') FROM {OpenLMSLog} WHERE courseid = $1 AND timecreated > $2 AND anonymous = $3 [array ( 0 => '1', 1 => 1604556625, 2 => 0, )] Error code: dmlreadexception
Stack trace:
* line 486 of /lib/dml/moodle_database.php: dml_read_exception thrown
* line 329 of /lib/dml/pgsql_native_moodle_database.php: call to moodle_database->query_end()
* line 920 of /lib/dml/pgsql_native_moodle_database.php: call to pgsql_native_moodle_database->query_end()
* line 1624 of /lib/dml/moodle_database.php: call to pgsql_native_moodle_database->get_records_sql()
* line 1697 of /lib/dml/moodle_database.php: call to moodle_database->get_record_sql()
* line 1912 of /lib/dml/moodle_database.php: call to moodle_database->get_field_sql()
* line 1895 of /lib/dml/moodle_database.php: call to moodle_database->count_records_sql()
* line 262 of /admin/tool/log/store/database/classes/log/store.php: call to moodle_database->count_records_select()
* line 329 of /report/loglive/classes/table_log.php: call to logstore_database\log\store->get_events_select_count()
* line 48 of /report/loglive/classes/table_log_ajax.php: call to report_loglive_table_log->query_db()
* line 59 of /report/loglive/classes/renderer_ajax.php: call to report_loglive_table_log_ajax->out()
* line 462 of /lib/outputrenderers.php: call to report_loglive_renderer_ajax->render_report_loglive()
* line 53 of /report/loglive/loglive_ajax.php: call to plugin_renderer_base->render()
This is the only error message I get even after turning on debugging in the developer settings. My goal is to successfully configure an external db to track logs, but due to a lack of error messages when testing the connection it is hard to debug.
Environment configuration: Open LMS 3.8 MP2 (Build: 20201008)
The external db is a postgres db so we set it on the postgres driver.
It looks like the SQL used by the external db log store plugin is not compatible with Postgres, despite using the Postgres driver in the external db log store plugin settings, as shown by the '{' error in the SQL shown in the question. See here for documentation.
To fix this, we used MariaDB instead of Postgres, and we ended up getting the external db log store working.
Another note, you have to match your columns and data types exactly to the schema in the Moodle db, and then you have to set the ID column of your db table to auto-increment. If you don't know what the schema looks like, there is an Admin SQL interface under "Reports" that lets you run SQL. On the side, there a button to view the schema for any table in the db.
I try to insert and pull some data from MongoDB.
The connection was setup correctly follow there instruction on mongodb.com
try:
client = MongoClient(
'mongodb+srv://user:pw!#cluster0-nghj0.gcp.mongodb.net/test?retryWrites=true',
ssl=True)
print("connected")
except:
print('failed')
I manually create a Database: messager.messager and put some json file in it
when I try to use collection.find() or collection.insert_one(...)
db = client.messager
collection = db.messager
for i in collection.find():
print(i)
It returns Timeout error:
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/cursor.py", line 1225, in next
if len(self.__data) or self._refresh():
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/cursor.py", line 1117, in _refresh
self.__session = self.__collection.database.client._ensure_session()
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1598, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1551, in __start_session
server_session = self._get_server_session()
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1584, in _get_server_session
return self._topology.get_server_session()
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/topology.py", line 434, in get_server_session
None)
File "/Users/anhnguyen/Documents/GitHub/GoogleCloud_Flask/comming soon/env/lib/python3.7/site-packages/pymongo/topology.py", line 200, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed
Where did it goes wrong ?
Here is my Mongodb.com setup:
From the pymongo documentation for errors you have the following issue.
exception pymongo.errors.ServerSelectionTimeoutError(message='', errors=None)
Thrown when no MongoDB server is available for an operation
If there is no suitable server for an operation PyMongo tries for serverSelectionTimeoutMS (default 30 seconds) to find one, then throws this exception. For example, it is thrown after attempting an operation when PyMongo cannot connect to any server, or if you attempt an insert into a replica set that has no primary and does not elect one within the timeout window, or if you attempt to query with a Read Preference that the replica set cannot satisfy.
You have to check whether your network has good connectivity to the network where your MongoDB server resides.
There might be a case, where the Primary Node of the Replica Set becomes unresponsive. In such cases you need to restart your cluster(if you have the access permissions).
Also, create the connections as follows:
mongo_conn = MongoClient('mongodb+srv://cluster0-nghj0.gcp.mongodb.net/test?retryWrites=true', username=your_username, password=pwd, authSource='admin', authMechanism='SCRAM-SHA-1')
above is the best practice to follow. mongodb+srv urls do not need the ssl=True mention.
the following code leaves an empty dummy database. Is this system behavior intended?
mongodb is running --auth mode and the user is part of the readAnyDatabase Role.
import pymongo
print CORE_PROD_URL
mongo = pymongo.MongoClient(CORE_PROD_URL)
print mongo.database_names()
print mongo.dummy.test.count()
print mongo.database_names()
which gives:
mongodb://read_only_user:pw#localhost:27017
[u'admin', u'local']
0
[u'admin', u'local', u'dummy']
the same behaviour happens with find()
while
mongo.dummy.test.insert({‘foo’: ‘bar’})
throws an exception
OperationFailure: not authorized on new_db to execute command
This is a known bug, SERVER-11051. The database name will disappear from "database_names()" the next time you restart the server, but of course it will reappear next time you read from the "dummy" database.
I'm using Mulestudio and am trying to insert into the Postgres database some data. I am modifying the log4j.properties file and below is how it looks like:
log4j.rootLogger = DEBUG, postgres
#
log4j.appender.postgres=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.postgres.layout=org.apache.log4j.PatternLayout
log4j.appender.postgres.driver=org.postgresql.Driver
log4j.appender.postgres.URL=jdbc:postgresql://127.0.0.1:5432/testing
log4j.appender.postgres.user=postgres
log4j.appender.postgres.password=pw
log4j.appender.postgres.sql=INSERT INTO LOGS VALUES ('%x', '%d{yyyy-MM-dd}','%C','%p','%m');
The error message that I get is
log4j:ERROR Failed to excute sql
org.postgresql.util.PSQLException: ERROR: syntax error at or near "edu"
'edu' is the first part of my project name (edu-stream-ucdnews). The instance of 'edu' only comes up in the title name and not in my data. I know that the error arises when I have the '%m' when I try to insert the data because when I change it to a hard-coded message like 'Hello', I don't get any error.
How do I solve this issue?
Are you sure your are connected to the database?
Try putting the fields name in your sql and lets check if we get a better error log:
log4j.appender.postgres.sql=INSERT INTO LOGS (field1, field2, ...) VALUES ('%x', '%d{yyyy-MM-dd}','%C','%p','%m');