MongoDB Server Configuration - python

I have a MongoDB server running on my localhost. I wrote a simple Python program that reads/writes to the database using "localhost"; however, I want to give other clients access to my MongoDB server. For now, I am not concerned about access security and would like to grant access to anyone. How should I configure the Mongo Server to do this?
Here is the simple program connecting to localhost.
from pymongo import MongoClient
connection = MongoClient("Localhost")
db = connection.hockey.players
results = db.find()
print()
print('+-+-+-+-+-+-+-+-+-+-+-+-+-+-')
for record in results:
print(record['name'] + ',',record['position'])
connection.close()
The error message I'm getting:
File "C:/Users/Peter/PycharmProjects/Test/helloWorld.py", line 8, in
for record in results:
File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 1097, in next
if len(self.__data) or self._refresh():
File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 1019, in _refresh self.__read_concern))
File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 850, in __send_message **kwargs)
File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 777, in _send_message_with_response server = topology.select_server(selector)
File "C:\Python34\lib\site-packages\pymongo\topology.py", line 142, in select_server address))
File "C:\Python34\lib\site-packages\pymongo\topology.py", line 118, in select_servers self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: x.y.z.w:27017: timed out
Process finished with exit code 1
Below is my current localhost setup that works fine if I run it on my client which hosts the MongoDB. Current setup
Thanks

Give them your ip address and the port number (Default:27017) to connect to your server. Also edit the bindIp in the mongod.conf file as bindIp: 0.0.0.0. Ask them to connect to your database by using something like:
from pymongo import MongoClient
connection = MongoClient("mongodb://your_ip:yourport")
If you're on windows, create the configuration file mongod.cfg. And add the entry as
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
net:
bindIp: 0.0.0.0
port: 27017
Make sure you've already created log and db folder or change the path where your data and log folder are located.
Start the mongodb by specyfying thr configuration file.

Verify that you local interface is up. It should explain the timeout.
ifup lo

Related

Setting up a Postgres DB for Django with Fly.io

I'm a novice trying to spin up my first webapp with a combination of Fly.io, Django, and a postgres DB but I'm having some trouble and can't find an answer in walkthroughs or Q&A.
I've set up a simple "Hello world" Django app (models.py is empty so far) and I'm trying to get all the components up and running before I build it out any further.
I've successfully deployed my app on Fly.io with no errors
I've created a postgres cluster on Fly.io using the instructions here: https://fly.io/docs/postgres/
I've attached the cluster to my app, which generates a DB and sets an environment variable with the appropriate details (username, password, port, host, dbname)
I've updated my settings.py file:
DATABASES = {}
DATABASES["default"] = dj_database_url.config(conn_max_age=600, ssl_require=True)
I've added to my fly.toml:
[[services]]
internal_port = 5432 # Postgres instance
protocol = "tcp"
# Open port 10000 for plaintext connections.
[[services.ports]]
handlers = []
port = 10000
I've confirmed I can get into the psql shell with flyctl postgres connect -a MYAPP-pg
But unfortunately when I run python manage.py migrate to check that everything is working, I get the following error:
File "<my_path>\venv\lib\site-packages\django\db\backends\base\base.py", line 282, in ensure_connection
self.connect()
File "<my_path>\venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "<my_path>\venv\lib\site-packages\django\db\backends\base\base.py", line 263, in connect
self.connection = self.get_new_connection(conn_params)
File "<my_path>\venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "<my_path>\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 215, in get_new_connection
connection = Database.connect(**conn_params)
File "<my_path>\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "top2.nearest.of.MYAPP-pg.internal" to address: Unknown host
Any ideas what might be happening? Any help would be very much appreciated!

Lost connection to MySQL server during query on Google Cloud MySQL server

I am aware that this question has been asked multiple times, but I haven't found a question that pertains to the same issue I am dealing with. I have a MySQL database hosted on Google Cloud SQL. I am running a python script out of a Google Cloud VM to connect to the database to run queries. Everything used to run fine, but now I am getting the following error on my initial connection to the server.
File "/usr/local/lib/python3.5/dist-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 597, in connect
self._get_server_information()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 966, in _get_server_information
packet = self._read_packet()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 656, in _read_packet
packet_header = self._read_bytes(4)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 702, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
And here is the code I am using to connect to the server
connection = pymysql.connect(host='127.0.0.1',
user='xxxxxx',
password='xxxxxx',
database='foobar',
cursorclass=pymysql.cursors.DictCursor,
max_allowed_packet=16777216,
connect_timeout=100)
Initially, my connection code did not have the max_allowed_packet and connect_timeout, and it was working fine. After some research into my issue, these seemed to resolve others' issues, but it hasn't resolved mine.
You must add wait_timeout and start by 10000 and then see.
The other will not help, but i added iy you have another timeout that will not be catch by connection and wait timeout
interactive_timeout Number of seconds the server waits for activity on an interactive connection before closing it
wait_timeout Number of seconds the server waits for activity on a connection before closing it
It seems
use a mysql command right after the connection
SET session wait_timeout=300;

IO Error - Flask-mail and running server with proxy

I'm with a very very wierd bug...
I have a flask app using flask-mail to send email messages.
In a RedHat Server, I tryied using runserver (flask-manager) and gunicorn. So I have a apache server connecting to this app using Proxy.
When I run the app, using any user (root or other), the app runs and it sends emails normally.
But when i close the session with the server (exit in terminal) it stops to send mail and gives me this stack trace:
in send_mail
return mail.send(msg)
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 415, in send
with self.connect() as connection:
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 123, in __enter__
self.host = self.configure_host()
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 144, in configure_host
host.login(self.mail.username, self.mail.password)
File "/usr/local/lib/python2.7/smtplib.py", line 575, in login
self.ehlo_or_helo_if_needed()
File "/usr/local/lib/python2.7/smtplib.py", line 535, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "/usr/local/lib/python2.7/smtplib.py", line 406, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "/usr/local/lib/python2.7/smtplib.py", line 336, in putcmd
self.send(str)
File "/usr/local/lib/python2.7/smtplib.py", line 320, in send
print>>stderr, 'send:', repr(str)
IOError: [Errno 5] Input/output error
Running with manager:
python myapp.py
Running with gunicorn I use:
gunicorn -w 2 -b 0.0.0.0:8388 myapp:app
I'm really stuck here.. as i tested using 2 different containers... i do not have any other ideias to solve it... using wsgi i could not make it work on this server cause the lib do not install at all =(
any other ideas?
thanks!
Looking at smtplib source (https://hg.python.org/cpython/file/2.7/Lib/smtplib.py#l324), it looks like what's happening is you're trying to write to stderr, which may be the source of the I/O error when running under a server.
If you're setting SMTP(...).debuglevel anywhere, try removing that line.

'Interface error: 2003' when connecting to database

I'm trying to use MySQL Connection/Python to connect to my database.
Here's the output I'm getting:
Traceback (most recent call last):
File "bh2000.py", line 33, in <module>
cnx = mysql.connector.connect(**config)
File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 155, in connect
return MySQLConnection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 123, in __init__
self.connect(**kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 430, in connect
self._open_connection()
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 393, in _open_connection
self._socket.open_connection()
File "/Library/Python/2.7/site-packages/mysql/connector/network.py", line 375, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host xxx.db.1and1.com:3306' (8 nodename nor servname provided, or not known)
Here's the code I'm using:
import mysql.connector
cnx = mysql.connector.connect(user='xxx', password='xxx',
host='xxx.db.1and1.com',
database='xxx')
cnx.close()
Where am I going wrong?
Did you specify the correct port?
Is your MySQL server running?
Is a firewall blocking access?
Try removing anonymous user account from your MySQL server?
Default port if not specified is 3306. Otherwise there is nothing wrong with your code. The problem is with your MySQL server or the connection is being blocked by your firewall or the server firewall. Make sure port 3306 is open and not blocked.
db = mysql.connector.connect(user='xxx', password='xxx', host='xxx.db.1and1.com', port=3306)
I believe the answer is because the hosting service you use, 1and1.com, uses a firewall that blocks all outside access to their databases. You have to run the code from a url that they host or it will not work.
I use the same server and had to figure this out the hard way myself. Below is a copy of what they have on one of their pages:
Access to the database via your website/presence only Please always
establish the connection to your database via your website/presence.
For security reasons, it is not possible to access the database
directly, for example via your local computer (external ODBC
connection).
To protect your data, your MySQL database is located on a dedicated
database server that is protected by a firewall.
Add a port argument in the command.
cnx = mysql.connector.connect(user='myuser',
password='mypassword', host='localhost', port='3306', database='mydb')
I got the same error. Once crosscheck the details you have entered. The host must be in the format 127.0.0.1. There should not be any / after the url.
Use localhost instead of IP, if you are working on same server.
myConnection = mysql.connector.connect(user='uname', password='pass', host='localhost', port='3306', database='databasename')

How to connect django developer server to MySQL database?

Before I begin please respect that I'm a newbie, ask me what more information you need to solve this problem rather than close the question or something. ;) OK. I am trying to connect my Django developer server to a MySQL database that is being hosted on a remote server. I've installed MySQL and MySQL-Python and have these settings in 'settings.py':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database_name', # Or path to database file if using sqlite3.
'USER': 'database_user', # Not used with sqlite3.
'PASSWORD': 'database_password', # Not used with sqlite3.
'HOST': 'db9.subsys.no', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
When i try to start the developer server using python manage.py runserver I get this:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'db9.subsys.no' (60)")
When I changed 'HOST': 'db9.subsys.no' to 'HOST': '' I got this instead:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'database_user'#'localhost' (using password: YES)")
Is it not possible to connect to MySQL using the developer server or do I need to do some kind of configuration on with the database? Reply if you have the answer or need more information.
Big Thanks in Advance!
Three things you should make sure of if you want to connect to a remote MySQL server:
The server is using networking and not sockets (there is no skip-networking line in my.cnf), and the server is listening on the public IP (with bind-address).
Your firewall allows remote access to the MySQL port, which is 3306 by default.
The user is allowed remote access:
GRANT ALL ON someDatabase.* to someUser#some.remote.ip IDENTIFIED BY 'foopass';
Just a quick sanity check - I assume you replaced the placeholder values (e.g. *database_name*) with the actual values?
Also, have you tried connecting to the database over the command line first (on your local development machine - not through some sort of SSH connection to a VPS etc.) to check that it's reachable from wherever you're developing from, e.g. mysql -u - p? If not, it probably means your web hosting company (I'm making an assumption that this isn't your database - please correct me if this is wrong and/or see #Burhan Khalid's answer) doesn't allow external connections to that database server and you won't be able to connect remotely from any software - this has nothing to do with Django. In this case either contact the hosting company to find out what port number to use (or if external database connections are even allowed under their firewall rules) or use a local database for development and then populate the production database from a dump-file of your local database. Here is a tutorial on this process - http://php.about.com/od/learnmysql/ss/mysql_backup.htm.
In my opinion this is a better way of doing things, as it is probably easier and more practical to develop locally and then upload. It can also prevent you from making a costly mistake if that winds up being the live database and you run one of the manage.py commands that can wipe out a table(s)!
You can use the database with the development server.
When you keep the host and port blank, django uses the default port to connect to the database on the local machine. In your case, you are getting an error because the username or password that you are using while connecting to the database on localhost is incorrect.
While connecting to a remote server, please make sure that you are
allowed to access it remotely. MySQL doesnt allow remote connections
by default.
Also, make sure that the port you are trying to connect
on is not blocked by a firewall.
Read this to figure out what you need to do to allow this behaviour: http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
I hope this helps you figure out the solution.
Try using an IP-Adress instead of 'db9.subsys.no' after the 'HOST': Tag.

Categories