Cannot connect to Remote MySql database using python - python

Hello , i am trying to connect to the database on my Hosting . I followed this tutorial : https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python
This is my code :
import pymysql.cursors
import pymysql
try:
connection = pymysql.connect(host='195.201.204.153',
user='turboweb_jobsuser',
password='TYcfvA*****',
db='turboweb_jobsdb_user',
)
print("Connected to:", connection.get_server_info())
except Exception as e:
print('connection failed')
print(e)
However, everytime I try to connect, this error occurs :
(2003, "Can't connect to MySQL server on '195.201.204.153' (timed out)")
The website address is : assamjobsapp.turboweb.online and doing a ping here,give me this IP :
195.201.204.153
I have created the Database , User and tables from the Hosting PHPMyadmin, but I dont know how to connect to it.
Should I provide the database username/pass or the Cpanel username/pass for the connection
Please Help. Thanks

As comments have mentioned, many (most!) hosting providers put a firewall between their database servers and the public internet. Because cybercreeps. Attempts to do things that firewalls forbid usually results in the kind of timeout you got.
If you want to connect to your MySQL database from your laptop or some other machine that's outside the provider's firewall, you must ask them to adjust their firewall rule to allow your particular machine to connect. Once the firewall allows your machine to connect, use your database username / password to establish the connection.
Some providers have self-service user interfaces for this, often in their cpanels. Others require you to ask a customer support agent to do it.

Related

Python API client for Elastic Search: can't connect due to Connection timed out

I am working with the Python API client for Elastic Search and I am trying to connect to it.
I create the client like this:
def setup_es():
ES_USER = os.getenv("ES_USER")
ES_PASS = os.getenv("ES_PASS")
print(f"Setting up ES with HOST={ES_HOST}, USER={ES_USER}, PASS={ES_PASS}")
return Elasticsearch([ES_HOST], basic_auth=(ES_USER, ES_PASS))
But whenever I try using the client, I always get a connection timed out error.
For example:
client.info()
or
client.options(ignore_status=[400,404]).indices.delete(index=MY_INDEX)
Always produce:
***_transport.ConnectionTimeout: Connection timed out
I know the host, user and password are right. Am I missing something else? Any ideas please?
I solved this issue by specifying port 443 in the elastic search host variable.

What does 'DPY-6001: cannot connect to database' mean with python-oracledb?

With Python code that uses the
python-oracledb driver:
import oracledb
import os
un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "localhost/doesnotexist"
c = oracledb.connect(user=un, password=pw, dsn=cs)
what does this error message mean?
DPY-6001: cannot connect to database. Service "doesnotexist" is not registered with the listener at host "localhost" port 1521. (Similar to ORA-12514)
The error means that Python successfully reached a computer (in this case
"localhost" using the default port 1521) that is running a database. However
the database service you wanted ("doesnotexist") doesn't exist there.
Technically the error means the listener doesn't know about the service at the
moment. So you might also get this error if the DB is currently restarting.
This error is similar to the ORA-12514 error that you would see when connecting
with python-oracledb in Thick mode, or might see with some other Oracle tools.
The solution is to use a valid service name, for example:
cs = "localhost/xepdb1"
You can:
Check and fix any typos in the service name you used
Check the hostname and port are correct
Ask your DBA for the correct values
Wait a few moments and re-try in case the DB is restarting
Review the connection information in your cloud console or cloud wallet, if you are using a cloud DB
Run lsnrctl status on the database machine to find the known service names

Flask looks for db locally when it's on another server

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

pymsql not connecting to a database

I am trying to make software that works on a large number of people's computers by connecting to a login server. I have set up a MSQL server using 24hosting and added a database. I then tried to access the database using python, but it gives me the error "No connection could be made because the target machine actively refused it".
I need everyone who downloads this program to be able to connect, not just this computer.
This is probably something to do with my server, and not to do with code, but I will post the code below anyway.
from os import getenv
import pymysql
server = getenv("31.220.17.13")
user = getenv("shutdow1_user")
password = getenv("DSAEWQ321")
conn = pymysql.connect(server, user , password, "tempdp")
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
cur.close()
conn.close()
Try to run tcpdump or tshark host 31.220.17.13 to see what exactly happens.
Most likely TCP connection to mysql port is filtered (you would see RST replies on SYN send), or if you see that connection is closed by remote party after TCP session is established - that would be a sign of remote mysql server not configured properly to accept client connection for your IP/username/password/database.

Problems in connecting to MusicBrainz database using psycopg2

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.

Categories