Cannot connect mysql through python - 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

Related

Mysql Python Connector No connection could be made because the target machine actively refused it

first i did
pip install mysql-connector-python
and then I made a file names "my.py" containing the following code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="abc",
password="1234"
)
print(mydb)
and im getting the following error
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
I have already closed firewalls, I have tried on 127.0.0.1, my LAN is not on proxy settings, and im not able to run connect on either port 3306 or 33060 or 22.
Here's everything from MYSql Installer - Community that I have installed
I think there are 2 possible options:
Database is not running (as proposed by John).
Your database is not running directly on your pc but on a VM or docker image.
In that case, user shall not be bound to 'localhost' but to '%' that stands for 'any location'. This is because VMs and docker containers run in a separate environment, and 'localhost' users login is accepted from the host running the database only.
Obviously this introduces a security concern since that login will be accepted from any place (localhost, other PCs in private network and other PCs on the internet network).

Access to Virtual machine MySQL database from windows host with 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.

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?

Pymysql can not link to the mysql

ps:I work in win7 virtualbox system,and I blocked the virtual network card,is that reason it?
You're using a Unix socket (hence the AF_UNIX in the error) to connect to MySQL, where it's not available on the platform (Windows). I suggest using a TCP connection instead.
Remove the unix_socket argument to pymysql.connect call, and replace it with port=3306.
So the connection line will be like this:
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='something', db='mysql')
Also make sure MySQL server accepts network connections as well. This can be set on MySQL server configurations.
If MySQL server is running on the same host where you're writing the client, then it should be fine. Otherwise you might need to allow connection from clients on other hosts to MySQL server.

Categories