0

Good day, I have a Synology NAS DS120J hosting a MariaDB 10 (10.3.32-1040) database, on the same local network, I have a Linux machine running Ubuntu 22.04. I am trying to connect the Linux machine to the MariaDB database using Python 3.10 and the MariaDB connector. However, it seems that I am missing a step. When I access this database using the command line, I SSH from the Linux machine to the NAS, mentioning the port where the database is located on the NAS. Then I populate my credentials to login to the database, this works fine.
But when using Python 3.10 and the MariaDB connector, I only provide with my credentials to login to the database, so I feel like I am missing the SSH step to the NAS, at least this is my assumption. How could I achieve that ? Can someone please help ?

Here is my script in Python:

import mariadb
import json
import sys

with open('config.json') as config_file:
    config = json.load(config_file)['mariadb']

try:
    conn = mariadb.connect(
        user=config['raspi-svr']['user'],
        password=config['raspi-svr']['password'],
        host=config['raspi-svr']['host'],
        port=config['raspi-svr']['port'],
        database="test_db"

    )
except mariadb.Error as e:
    print(f"Error connecting to MariaDB Platform: {e}")
    sys.exit(1)

cur = conn.cursor()

Here is the error message I am getting when running my script:

Error connecting to MariaDB Platform: Lost connection to server at 'handshake: reading initial communication packet', system error: 0

I tried running the MariaDB connector for Python.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Can you please detail the exact steps via commandline that work? I have a vague feeling that there is info hidden in there that you don't provide and that you don't reproduce in your Python code. – Ulrich Eckhardt Jan 22 '23 at 21:16
  • 1
    Have you checked out https://pypi.org/project/sshtunnel/ – JonSG Jan 22 '23 at 21:18
  • There are two ways: expose and allow a connection via a port to MariaDB (try to look it up in the MariaDB doc) or do a sshtunneling first and provide that port to the connector - been a while since I last did that in practice. – Daraan Jan 22 '23 at 21:40
  • @Daraan: thank you very much, I'll have a look at the MariaDB doc. – user16243249 Jan 23 '23 at 23:10
  • 1
    @JonSG: thank you for pointing to that link, much appreciated. – user16243249 Jan 23 '23 at 23:12
  • @UlrichEckhardt: definitely agree, but I don't actually know how to add this missing step. 1. ssh [synology_user]@[synology_IP] -p[MariaDB_port] 2. mysql -u [MariaDB_user] -p I need to look into an sshtunneling option as suggested. Thank you very much. – user16243249 Jan 23 '23 at 23:16
  • [edit] your question and add that info there, please! – Ulrich Eckhardt Jan 24 '23 at 07:00

0 Answers0