Access to Virtual machine MySQL database from windows host with python - python

I´m trying to connect to a mysql database installed on a virtual machine with Ubuntu Server. I´m very new to server administration so I followed this tutorial LAMP on it. I also followed this tutorial to install phpMyAdmin. Apparently, everything is working fine. I can access to phpMyAdmin from the web browser on my host machine.
The network of the virtual machine is configured as bridge adapter.
The problem is that I cannot connect when using python (also from host machine). This is the code I'm using:
import mysql.connector
cnx = mysql.connector.connect(user='root', password='XXXXX',
host='192.168.1.138',
port=3306,
database='mysql')
cnx.close()
The code returns this error:
DatabaseError: 1130: Host 'desktop-p7v30jj.home' is not allowed to connect to this MySQL server
I looked for information and found that it could be due to the bind-address. So I edited the file /etc/mysql/mysql.cnf and included the following two lines at the end with no success:
[mysqld]
bind-address = 0.0.0.0
What else should I try? Any idea?
Thanks in advance!

I managed to find the solution by simply creating another user, as stated in this topic:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
Sorry for answering.
Regards.

Related

sqlalchemy engine trying to connect to 127.0.0.1 instead of provided IP

I'm writing here because I can't find any other information on this specific bug.
Whenever I try to connect to my remote DB using
engine = sqlalchemy.create_engine(f'mysql+pymysql://{creds.user}:{creds.dbpassword}#{creds.host}:{creds.port}/{creds.database}')
It tells me:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' ([WinError 10061] No connection could be made because the target machine actively refused it)")
(Background on this error at: http://sqlalche.me/e/e3q8)
I explicitly have creds.host set to the IP of the sever as well as I get the same error when I put it directly into the engine. This works on the remote machine under root but does not work for me and it works for a partner who runs the exact same code. Some of the links I found was suggesting to change the bind-address in the mysql config. I uninstalled my local mariadb/mysql server and went and found the config file with bind-address and uncommented it so it was bind-address=0.0.0.0 after a reboot its still not working so I have to think that it is my machine that is not letting me connect to the server.
New info: My partner can no-longer connect through pymysql. I can still connect through datagrip and mysql workbench. The port is open and mysql is listening on port 3306. I have appropriate permissions as I know this because I can connect through datagrip and mysql workbench. I'm thinking this is an us problem and not a server problem but I just don't know why because nothing has changed before the issue started.

Cannot connect mysql through python

I did a python -m pip install mysql-connector and able to successfully run import mysql.connector through python. But when I am trying to run the below code.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
It is failing with InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
Since I did pip install for mysql.connector I am not sure of user and passwd.
I connected my database instance from Amazon RDS to mySql workbench, created a python file that looks like his and got the exact error. Everyone says you could have a system firewall problem but nothing straight forward.
The connector is just a means of communicating with a mysql database programically.
You need this, or access to a mysql server to use the connector.
https://dev.mysql.com/downloads/mysql/
The host is obviously localhost when you want to access it locally on your machine. This only works if the server is running on your machine. You can connect a remote server by changing the host to a valid IP address and providing valid credentials. If you use it remotely make sure the server has access through the firewall and that you properly forward the TCP port you decide to use. You may not have to forward, but I would as a general rule of thumb to make it one less thing to check when troubleshooting.
Good luck

'ORA-21561: OID generation failed for remote Oracle 12c XE Instance - Oracle on Windows 10 client (cx_Oracle using python) on Mac

I am trying to connect to the Oracle instance which is running on Windows 10 through python using cx_Oracle package from a mac machine.
Now while connecting it throw below error.
'ORA-21561: OID generation failed\n'
My Sample code:
import cx_Oracle
DSN = cx_Oracle.makedsn(host=server, port=port, service_name=database)
# Below is the DNS
# (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.9)(PORT=50244))(CONNECT_DATA=(SERVICE_NAME=devXDB)))
con = cx_Oracle.Connection(user, password, DSN)
However I am able to connect from same machine (mac) using SQL developer and PyCharm's database browser. I searched across and did not find any solution related to remote instance. The solutions suggested for seems to be working only for the local instances in which one has to edit/update etc/hosts or related file on windows 10.
Thanks in advance.
This was indeed the problem of /etc/hosts file issue.
One thing to note here even if the oracle instance is running on a remote machine you client machine's (from where you are connecting to the oracle instance) /etc/hosts file should have the entry like this.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost localhost.localdomain Amits-iMac.local
Replace 'Amits-iMac.local' to your client's hostname.

Peewee can't connect to MySQL on Remote Host

I'm using Python 3.5 + Spyder 2 (from Anaconda) on Windows 10. I have an Ubuntu 16.04 desktop machine which is running a MySQL server on a LAN addressable IP. PhpMyAdmin works fine with this remote server. However, every time I attempt to connect to this server from my Windows 10 Spyder I get the following error:
OperationalError: (1045, "Access denied for user 'root'#'10.0.0.30' (using password: YES)")
The command I'm using is:
import os
from peewee import *
from playhouse.db_url import connect
db = MySQLDatabase('test', host="10.0.0.80", port=3306, user='root', passwd='*********')
db.connect()
The IP of the machine I'm using to make the call is 10.0.0.30 shown in the Error. I originally tried it before the "test" db existed. Then I created the "test" db using PhpMyAdmin. I then tested again. Same error. I have created a table in the "test" db via PhpMyAdmin to confirm that root has the appropriate privileges (which wasn't really a question but I wanted confirm).
I have downloaded and installed what I could find via several StackOverflow questions prior to posting this question. I have also rebooted to make sure that any new drivers and such that I installed were actually up and running.
Note my issue is not the same as this one:
Peewee - Can't connect to MySQL server on host
My connection error shows that despite using the host argument and setting it to "10.0.0.80" that peewee is still trying to connect to 10.0.0.30. IP 10.0.0.30 is the Windows machine I'm running peewee from while 10.0.0.80 is the machine I'm attempting to connect to.
TIA

Python and Connecting to MySQL over SSH

I am trying to connect to a MySQL database on someone else's "machine". When I use Navicat for MySQL, I have no problem connecting to it. I am trying to do the same with Python so that I do not have to use the GUI interface. I know all my info below is correct (even though I swap fake info) -- can anyone spot where I went wrong? The error I get is OperationalError: (2005, "Unknown MySQL server host 'FTP_hostname' (0)")
My code (using paramiko for the SSH):
import MySQLdb
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('SSH_hostname', 22, username='me', password='pswrd')
time.sleep(1)
db = MySQLdb.connect(host="FTP_hostname",
user="root",
passwd="pswrd2",
db="MyDB")
cur = db.cursor()
Again, I put all this into Navicat and connect no problem. Hoping you can help! Thanks!
MySQL, like most databases, by default runs locally and disallows access from outside networks. As such, you cannot connect to it from an external computer.
Navicat, being a software explicitely for remote administration of databases, likely connects via SSH and tunnels the MySQL connection over it. That way it can act as if the database was installed locally, and for the database it looks as if it was accessed locally.
You could try to do the same by creating a tunnel using Paramiko; see also this question.
If you still in need of connecting to a remote MySQL db via SSH I have used a library named sshtunnel, that wraps ands simplifies the use of paramiko (a dependency of the sshtunnel).
You can check my answer in another similar question with some sample code to use it.
db = MySQLdb.connect(host="FTP_hostname",
Would the host not need to be 127.0.0.1 (localhost) as the tunnel is making the MySQL DB local to the machine that the python script is running on?

Categories