Pymysql can not link to the mysql - python

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.

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

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?

Tornado MySQLDB SSL Connection

I’m using tornado and connecting to MySQL using MySQLDB. How I can connect to MySQL using SSL?
If the MySQL server is on the same host as the web server then you don't really need to do this since traffic will not leave that box. You can use an ssh tunnel to encrypt traffic if you'd like. Something like the following should do the trick:
$ ssh <mysql_port>:localhost:<mysql_port> user#mysqlserver.host
That will create a tunnel from your MySQL server to the web server. You can then connect to localhost:port instead of mysqlserver.host:port.

SSH Tunnel for Python MySQLdb connection

I tried creating a SSH tunnel using
ssh -L 3306:localhost:22 <hostip>
Then running my python script to connect via localhost
conn = MySQLdb.connect(host'localhost', port=3306, user='bob', passwd='na', db='test')
However, I receive the following error
(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
How can I make sure I'm hitting the correct host and not just some problem with the bind?
Try changing "localhost" to "127.0.0.1", it should work as you expect. This behavior is detailed in the manual:
UNIX sockets and named pipes don't
work over a network, so if you specify
a host other than localhost, TCP will
be used, and you can specify an odd
port if you need to (the default port
is 3306):
db=_mysql.connect(host="outhouse", port=3307, passwd="moonpie", db="thangs")
If you really had to, you could
connect to the local host with TCP by
specifying the full host name, or
127.0.0.1.
Does mysqld run on port 22 on the remote? Call me ignorant but I think what you're trying to do is
ssh -n -N -f -L 3306:localhost:3306 remotehost
Then making MySQL connections on local machine will transparently get tunneled over to the target host.
You can't specify localhost as the hostname, as this suggests that MySQLdb should try to use a UNIX socket. Use 127.0.0.1 for the host instead.
If you want to make sure the connection works, you can use the standard mysql client.

Categories