Home > Blockchain >  How can I connect myremotesql to Python?
How can I connect myremotesql to Python?

Time:11-19

I tried to connect RemoteMySql as a host with PyMySql, it neither shows an error nor does it work.

The code is below:

db = pymysql.connect(
    host="remotemysql.com",user="USER",
    password="PASSWORD",db="DBNAME")
cur = db.cursor()
cur.execute("INSERT INTO `users` (ID, name, password,email) VALUES (93454623021,'Jeff','12345','[email protected]');")
db.close()

I also changed the host to localhost, but it showed this error:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] No connection could be made because the target machine actively refused it)")

It does work when I test it in phpMyAdmin but it does not work when I do it in any other compiler, it does not insert the data to the table so, what actually am I missing?

CodePudding user response:

This source code is correct.

At issue is: "does the client have TCP connectivity to the server?".

It's easy to check. Use any one of these commands.

$ ncat remotemysql.com 3306
L
8.0.13-4???3E>Z/l8Q?????hC !h&CsNmysql_native_password
^C
$
$ telnet remotemysql.com 3306
Trying 37.59.55.185...
Connected to remotemysql.com.
Escape character is '^]'.
L
8.0.13-4}??/}>lkfJ??zf *P
                         }XNN^mysql_native_password
^]
telnet> q
Connection closed.
$
$ time curl http://remotemysql.com:3306
curl: (1) Received HTTP/0.9 when not allowed

real    0m0.500s

If your local firewall is blocking such packets, and you can change the firewall config, arrange for it to pass outbound TCP port 3306 connections, to support the mysql DB protocol.

If you are blocked and cannot change the config, then seek another connectivity solution.

  • Related