Note:**I am connecting using Python2.7 in a virtualenv to access MySQL on XAMPP, which was not installed on the virtualenv.
I am trying to connect with MySQL via python. I recently downloaded MySQL connect from Oracle. I also downloaded the ODBC driver from the same site. When I plug in the information below, I get a 2003 error telling me that it cannot connect:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1'
but when I include the port number in my connection, it freezes up and nothing happens.
This is my code:
>>>import mysql.connector
>>>cnx = mysql.connector.connect(user='[my username]',password='[my password]',host='127.0.0.1', database='FXPrices',port=80)
After that, the cursor indents--so there are no arrows for the python prompt. I have to hit control-z every time I want it to stop. Do I need mod_wsgi to fix this?
You're trying to connect to the http port (port 80) on your machine.
MySQL by default listens to port 3306, you shoud try that (it's the same if you omit the port argument), or try to find out on which port your mysql server is really listening.
Related
I'm a student and I'm trying to write some sensor values into a MySQL database.
As IDE I'll be using Inteliji.
First off I started by installing the database Plug-in.
This was done successfully
Next I tried to connect to the data base (see figure below)
Figure of successful connection
Now The next thing I want to do is use a MySQL connector.
Therefore I've installed MySQL onto the r-PI and used following code to implement it.
import mysql.connector
print("Step 1")
cnx = mysql.connector.connect(user='a21ib2a01',
password='secret',
host='mysql.studev.groept.be',
database='a21ib2a01')
Print("Step 2")
When now I run my code the terminal will output:
Step1
For some reason I don't know; the connect function always times my program out with the next occurring errors:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql.studev.groept.be:3306' (110 Connection timed out)
So does anyone know why my connection is successful but I can't connect to it?
Long story short what am I doing wrong and how do I fix this?
Thanks in advance!
Your timeout means the network on your rPi cannot reach -- cannot find a route to -- your MySQL host mysql.studev.groept.be.
If you do traceroute mysql.studev.groept.be in a shell in the rPi you may see what's wrong.
When in a shell on your rPi, can you ssh to any machine in your uni's network? If so, you might be able to use ssh port-forwarding to get a route to the database server.
Do you run intelliJ on the rPi directly, or on your laptop? If you run it on the laptop, it looks like the laptop can find a route to your server but the rPi cannot.
(If this were my project, I'd install a MySQL server on my laptop to reduce the network-engineering hassles of connecting through multiple hops involving a VPN.)
I need to create a mysql DB for my project. I have a ubuntu server and have followed this guide to install required modules:
https://www.javahelps.com/2018/10/install-mysql-with-phpmyadmin-on-ubuntu.html
After installing everything I can reach the phpmyadmin using the browser 192.168.3.146/phpmyadmin works without any issues:
phpmyadmin home screen
But the problem is when I try to reach the database through the python code, it does not work. My program hangs after mysql.connector.connect and will never print the next print statement.
print("trying MYSQL")
myConnection = mysql.connector.connect(host=192.168.3.146, user='test_user', passwd='test_user123', db='test', autocommit=True)
print("Connection ID:", myConnection)
print(myConnection)
I have previously sucesfully connected to the mysql this way to the mysql server which was created on Raspbian and did not have any issues. It seems that UBUNTU is not as straightforward.
Please can someone suggest me what could be the problem if I can easily reach the database through the web browser but python program fails
UPDATE
Trying out to comment out bind address in the configuration file but still no luck. The configuration file:
enter image description here
I have allowed firewall through port 3306 with the following command:
sudo ufw allow 3306
There is some progress. Now my Python program does not hang on the mysql connection but instead gives me error:
ERROR 1130 (HY000): Host '192.168.3.251' is not allowed to connect to this MySQL server
First of all, I am unsure where does this IP come from 192.168.3.251. The machine am using to connect to the database is configured with static IP and it is 192.168.4.200. I do not know why it says 192.168.3.251
Is the python script running from another machine? If so, maybe the problem is that mysql is listening only in localhost interface. You can change this by editing the file mysqld.cnf located in the /etc/mysql/mysql.conf.d/ folder and comment (with an #) the line that says:
bind-address = 127.0.0.1.
Once you've done this, restart the service this way:
$ sudo systemctl restart mysql.service
And, verify that everything is ok with:
$ systemctl status mysql.service
I have a snippet of code that allows me to connect to my psql DB via ssh in Python. It works perfectly on Ubuntu 18.10 (via VirtualBox) but fails every time on windows with an error that it can't reach the remote host and port.
I'm been developing a user interface that can query data from a remote DB (logs etc.) and visualize it.
All of the development has been done using Spyder3 on Ubuntu 18.10. I never had an issue until I tried to execute the same code on Windows 10.
I tried Telnet to both the localhost:port and remote host:port (via ssh) and it works. Having looked up all the possible answers on stackoverflow and other places, I still haven't been able to fix the issue. The fact that it works on one environment and not on the other, while on the same machine, tells me it's some sort of environment setting but I don't know what it could be.
The code:
import psycopg2
import logging
logging.basicConfig(level=logging.DEBUG)
from sshtunnel import SSHTunnelForwarder
PORT = 5432
REMOTE_HOST = '111.222.111.222'
REMOTE_SSH_PORT = 22
curs = None
conn = None
server = SSHTunnelForwarder((REMOTE_HOST, REMOTE_SSH_PORT),
ssh_username='username',
ssh_password='password',
remote_bind_address=('localhost', PORT),
local_bind_address=('localhost', PORT))
server.start()
conn = psycopg2.connect(database='db_name', user='db_username', password='db_password', host='127.0.0.1', port='5432')
curs = conn.cursor()
Expected:
A successful connection to ssh and subsequent successful log-in to the database. This works on Ubuntu 18.10 via VirtualBox on the same machine.
Actual result:
2019-01-02 10:54:51,489 ERROR Problem setting SSH Forwarder up: Couldn't open tunnel localhost:5432 <> localhost:5432 might be in use or destination not reachable
I realized that my local postgres (psql) service was interfering with the port mapping as it was also using port 5432. Once I disabled the service, it worked like a charm.
I may be wrong, but I think the remote_bind_address should be set to your server's private IP. As this is where the remote machine would communicate to your machine.
remote_bind_address=(<PRIVATE_SERVER_IP>, PORT)
I'm writing a small python program locally as i don't have root access on the server. It basically does a lot of mysql queries using python MySQLdb module.
The thing is I cant use MySQLdb with the server, as the mysql server is hosted locally and I need to ssh into the server and then use mysql from there.
Is there any module available where I can connect to a mysql database via SSH.
At the moment I can connect to the mysql instance using SSH credentials (IP, User, Pass)
I also have the user/pass for the mysql instance and I'm pretty sure it runs on 127.0.0.1/localhost.
If you're set on Python, I would use paramiko:
https://github.com/paramiko/paramiko
This seems to be one of the most widely used Python SSH libraries. There are some other StackOverflow questions that address how to do this.
How to open an SSH tunnel using python?
SSH Tunnel for Python MySQLdb connection
The main idea is to create a tunnel with paramiko and then connect to the localhost port through which you are tunneling traffic to the remote server using the Python library MySQLdb.
I am not a network/web/internet programmer so please excuse my noobness in this area. I have gotten a website using a free hosting service. They include one MySQL database. Here are the details for the database:
port = 3306
host = "fdb4.biz.nf"
database = "1284899_6067"
user = "1284899_6067"
password = "somepass9351"
I am using MySQLdb module (installed on my CLIENT machine - not server) to connect to this database:
db = MySQLdb.connect(host=host, user=user, passwd=password, db=database,port=port)
But I get the following error:
OperationalError: (2003, "Can't connect to MySQL server on 'fdb4.biz.nf' (10060)
What I have already tried
tried two different databases from different hosts
tried changing the port
tried searching SO for similar answers but all others connect to 'local host'
What I think:
could this be caused by my firewall? I am using my school's internet. I don't think this could be it because I am on CLIENT so if anything it is the SERVER'S firewall.
Two questions
Can MySQLdb be used to connect to a db on a SERVER when it is imported on a CLIENT?
If yes, what am I doing wrong?
Thank you so much for any help, its greatly appreciated! Been stuck the whole day on this.
For security reasons, mysql only listens for connections from localhost. Error code 10060 is basically that: you are not allowed to connect remotely.
Solution: find a my.ini (or my.cnf in linux) and try to find a line:
bind-address = 127.0.0.1
this line says: allow only local connections. So, you should comment-out this line, or set your IP address.
Yes, MySQLdb can connect to remote hosts.
And your usage of the connect method is correct.
You should first check if you can connect to the remote mysql server from your mysql client.
In terminal you can type mysql -h hostname -u username -p databasename
This should prompt you for the password. Enter the password. Can you connect?
If you can't connect, then you have an access problem, and its not a python - mysqldb problem
Either the server is not reachable because it is behind a firewall, in that case your client machine's ip needs to be whitelisted. Check your firewall settings
Or, the mysql server running on the remote machine is configured to accept only local connections. I think this is the default, but I'm not sure. You should ssh into the server remote host where the database server is running, locate the my.cnf file on the server and check the settings. Depending on your mysql version, the configuration would look slightly different.
Or, the user that you're trying to connect as is not associated with the ip that you're trying to connect from. Mysql users have two parts, like this: 'username'#'host'. To enable a user to connect from all ips the user needs to look like this 'user'#'%'.
I hope I've given you enough to try to debug this issue.