I'm pretty new to mongodb and i'm tryin to figure out how can I connect to my database with python externally.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
in this example, you connect to your mongodb using a localhost, but I cant figure out how to connect my DB by remote and not locally. (More like, how do I get even a URI to put in there, im digging in mongodb website but im lost)
Thanks in advance!
From the documentation:, connection string:
mongodb://[username:password#]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
What you need at least:
User
Password
Host (you are using localhost, you will need an ip or address to connect to a remote database)
Dabase name
Related
I have made a free account on https://www.freemysqlhosting.net which provides a free mysql database hosting facilities. I have created a database there, but I don't know how to connect to it from my python code on VSCode.
As of now, I am using a database that is on my computer.
This is the code in a config.py file that establishes connection to the MySQL server.
import mysql.connector as mysql
mysqlobj = mysql.connect(host='localhost', user='root',password='Mypass', database='timetable')
cursorobj = mysqlobj.cursor()
cursorobj.execute('USE timetable')
So how do I connect to a remote database on the said website with a piece of code that can be executed, because that way the connection can be established from any computer if they have the code to do so, without installing extensions(unless it's necessary).
If you require more details, please do ask in the comment section.
You should use remote address of server on which MySQL server is hosted and provide the IP address or Domain name exposed as connection properties to you in host,
import mysql.connector
config = {
'user': 'test',
'password': 'password',
'host': 'Remote IP or Domain Name',
'database': 'employees',
'raise_on_warnings': True
}
cnx = mysql.connector.connect(**config)
cnx.close()
This looks like a dup of Connecting to remote database located on web (freemysqlhosting.net) via visual studio c#. I've never used that service(looks pretty sketchy). They should provide you a connection string or at least the connection parts which consists of:
IP
port
username
password
database
NOTE: it's generally a bad idea to have a mysql server bound to ports exposed to the public internet, even with a username password.
I think you should be using pyodbc instead of mysql connector. That's what I remember using.
To install pyodbc it is a simple pip install pyodbc command
You may find more useful information here
If you're unable to connect to your hostedDB, try to whitelist your local IP address in your hosting server.
This might work.
I'm trying to setup my Flask application to work with a database hosted on a different server. My whole setup works, if i try to work with a simple PyMysql script i will be able to connect to the database, but when i try to do that from Flask i get any kind of problem.
I'm keeping my db configurations on config.py:
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:pass#external_ip/mydb'
But whenever i try to do a query, i will get the following error:
SELECT command denied to user 'user'#'local_ip'
So flask is looking for the db locally, for some reason, even though i set it to point to an external server. Can anyone help me out on this?
On the same environment, the following will connect and allow me to make queries:
connection = pymysql.connect(host='external_ip', user='user', password='pass', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
This error looks more like a db server error, more then a local flask client error.
If it was a Flask local error of inability to connect to the mysql server you should end up with something like:
Unable to connect to the server
Hostname unreachable
Connection refused (if you reach the server but with the wrong port, for instance)
Reading this error I guess that you have reached the server, but for that user + IP + Database combination you have no read permissions.
See the GRANT Statement doc for further details
I just started to learn python and try to connect to oracle 11g, but I always get following error
cx_Oracle.InternalError: No Oracle error?
Here is my simple script to connect to oracle
import cx_Oracle as oracle
con = oracle.connect('user/password#ip:port/service')
Already try to look for any reference in other sites including here but can't find the solution. I don't think I have connection issue to oracle, because I use the same PC to connect to oracle using PHP.
Any advise would be appreciated, thanks.
One thing to keep in mind anytime you work with Oracle is that they use a proprietary connection protocol TNS (Transparent Network Substrate).
Therefore, you might need to use the cx_Oracle.makedsn(ip, port, SID) method and then pass it to cx_Oracle.connect() method to create your connection. Thus the general format on how to set up Oracle connection is:
import cx_Oracle
ip = 'xxx.xxx.xx.xxx'
port = 'xxxx'
SID = 'SID'
username = 'username'
password = 'password'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
db = cx_Oracle.connect(username, password, dsn_tns)
This is assuming you have already gotten cx_Oracle to work and import properly, which can be finicky depending on your environment.
How can I connect to MS SQL Server database remotely by IP in Python using mssql and pymssql modules.
To connect locally I use link = mssql+pymssql://InstanceName/DataBaseName
I enabled TCP/IP Network Configurations.
But How can I get the connection link?
Thank you.
You need to create a Connection object
import pymssql
ip = '127.0.0.1'
database_connection = pymssql.connect(host=ip, port=1433, username='foo', password='bar')
If you're using SQLAlchemy, or another ORM that supports connection strings, you can also use the following format for the connection string.
'mssql+pymssql://{user}:{password}#{host}:{port}'
I am trying to connect to the MusicBrainz database using the psycopg2 python's module. I have followed the instructions presented on http://musicbrainz.org/doc/MusicBrainz_Server/Setup, but I cannot succeed in connecting. In particular I am using the following little script:
import psycopg2
conn = psycopg2.connect( database = 'musicbrainz_db', user= 'musicbrainz', password = 'musicbrainz', port = 5000, host='10.16.65.250')
print "Connection Estabilished"
The problem is that when I launch it, it never reaches the print statement, and the console (I'm on linux) is block indefinitely. It does not even catches the ctrl-c kill, so I have to kill python itself in another console. What can cause this?
You seem to be mistaking MusicBrainz-Server to be only the database.
What's running on port 5000 is the Web Server.
You can access http://10.16.65.250:5000 in the browser.
Postgres is also running, but listens on localhost:5432.
This works:
import psycopg2
conn = psycopg2.connect(database="musicbrainz_db",
user="musicbrainz", password="musicbrainz",
port="5432", host="localhost")
print("Connection established")
In order to make postgres listen to more than localhost you need to change listen_addresses in /etc/postgresql/9.1/main/postgres.conf and make an entry for your (client) host or network in /etc/postgresql/9.1/main/pg_hba.conf.
My VM is running in a 192.168.1.0/24 network so I set listen_addresses='*' in postgres.conf and in pg_hab.conf:
host all all 192.168.1.0/24 trust
I can now connect from my local network to the DB in the VM.
Depending on what you actually need, you might not want to connect to the MusicBrainz Server via postgres. There is a MusicBrainz web service you can access in the VM.
Example:
http://10.16.65.250:5000/ws/2/artist/c5c2ea1c-4bde-4f4d-bd0b-47b200bf99d6.
In that case you might be interested in a library to process the data:
python-musicbrainzngs.
EDIT:
You need to set musicbrainzngs.set_hostname("10.16.65.250:5000") for musicbrainzngs to connect to your local VM.