0

I'm trying to figure out why I can't connect to the MySQL database from JavaScript/PyScript project.

Here's my index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />

    <title>PyScript - MySQL DB connection test</title>

    <link rel="icon" type="image/png" href="https://pyscript.net/examples/favicon.png" />
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    <py-env>
      - mysql-connector-python
    </py-env>
    <py-config>
packages = [
  "mysql-connector-python"
]
    </py-config>
  </head>

  <body>
    <div id="outputDiv"></div>
    <py-script src="./connectDB.py" output="outputDiv">  </py-script>
    <p>PyScript - MySQL DB connection test</p>

  </body>
</html>

and my connectDB.py:

import mysql.connector

mydb = mysql.connector.connect(
  host="sql7.freemysqlhosting.net",
  user="dbuser",
  password="dbpassword",
  database="mytestdatabase"
)

print(mydb)

Result after I loaded my project page with http://...:

PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/mysql/connector/network.py", line 567, in open_connection self.sock.connect(sockaddr) BlockingIOError: [Errno 26] Operation in progress The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 506, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 357, in run_async coroutine = eval(self.code, globals, locals) File "", line 3, in File "/lib/python3.10/site-packages/mysql/connector/pooling.py", line 287, in connect return MySQLConnection(*args, **kwargs) File "/lib/python3.10/site-packages/mysql/connector/connection.py", line 137, in init self.connect(**kwargs) File "/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 1108, in connect self._open_connection() File "/lib/python3.10/site-packages/mysql/connector/connection.py", line 533, in _open_connection self._socket.open_connection() File "/lib/python3.10/site-packages/mysql/connector/network.py", line 569, in open_connection raise InterfaceError( mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'sql7.freemysqlhosting.net:3306' (26 Operation in progress)

In browsers console, I can also see this warning:

pyodide.asm.js:10 WebSocket connection to 'ws://sql7.freemysqlhosting.net:3306/' failed: WebSocket is closed before the connection is established. close @ pyodide.asm.js:10

Result after I loaded my project page with https://...:

PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/mysql/connector/network.py", line 567, in open_connection self.sock.connect(sockaddr) OSError: [Errno 23] Host is unreachable The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 506, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 357, in run_async coroutine = eval(self.code, globals, locals) File "", line 3, in File "/lib/python3.10/site-packages/mysql/connector/pooling.py", line 287, in connect return MySQLConnection(*args, **kwargs) File "/lib/python3.10/site-packages/mysql/connector/connection.py", line 137, in init self.connect(**kwargs) File "/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 1108, in connect self._open_connection() File "/lib/python3.10/site-packages/mysql/connector/connection.py", line 533, in _open_connection self._socket.open_connection() File "/lib/python3.10/site-packages/mysql/connector/network.py", line 569, in open_connection raise InterfaceError( mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'sql7.freemysqlhosting.net:3306' (23 Host is unreachable)

In browsers console, I can also see this error:

Mixed Content: The page at 'https://****.**/psproject/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://sql7.freemysqlhosting.net:3306/'. This request has been blocked; this endpoint must be available over WSS. createPeer @ pyodide.asm.js:10

Has anyone here already solved a similar problem and could advise me how to fix my issue, please? I've also tried to switch to different MySQL DB hosting, but same problem there.

Thank you very much for any hint!

  • opened WebSockets connection to DB before executing the Python script.
  • switched to another MySQL DB hosting = same problem.
  • I've no I idea what else I could try
  • Packages such as mysql connector are not compatible inside the web browser. The security sandbox does not allow TCP sockets. – John Hanley Oct 29 '22 at 20:16
  • You may be interested in **Datasette Lite** described [here](https://datasette.io/) and [here](https://datasette.io/). "Datasette Lite: Datasette running in your browser using WebAssembly and Pyodide" ... "You can use this tool to open any SQLite database file that is hosted online and served with a access-control-allow-origin: * CORS header. Files served by GitHub Pages automatically include this header, as do database files that have been published online using datasette publish." – Wayne Oct 31 '22 at 18:35

0 Answers0