I am trying to retrieve some data from a MariaDB server (located in a different network).I can access the server via phpmyadmin but I cannot connect to it by means of a python script.Following MariaDB documentation I wrote the following Python code:

# Module Importsimport mariadbimport sys# Connect to MariaDB Platformtry:conn = mariadb.connect(user="myUSER",password= "myPSW",host="localhost"port=3306,database="myDATABASE")except mariadb.Error as e:print(f"Error connecting to MariaDB Platform: {e}")sys.exit(1)# Get Cursorcur = conn.cursor()

This is not working and I get the following error:

Error connecting to MariaDB Platform: Can't connect to MySQL server on 'localhost' (10061)Process finished with exit code 1

I retrieved MariaDB infos from here:

enter image description here

Am I missing some important point?

1

Best Answer


I found using 127.0.0.1 instead of localhost solved my problem.

Install MariaDB Connector/C, which is a dependency.

sudo apt-get install libmariadb3 libmariadb-dev

Use PIP to install MariaDB Connector/Python.

pip3 install mariadb

Both of these work for me:

import mariadbimport systry:# connection parametersconn_params = {'user' : "harley",'password' : "KTJ7UCS74mv]hh[I",'host' : "127.0.0.1",'port' : 3306,'database' : "phishing_emails"}# establish a connectionconnection = mariadb.connect(**conn_params)cursor = connection.cursor()except mariadb.Error as e:print(f"Error connecting to MariaDB Platform: {e}")sys.exit(1)print(cursor)

Or...

import mariadbimport sys# Connect to MariaDB Platformtry:connection = mariadb.connect(user = "harley",password = "KTJ7UCS74mv]hh[I",host = "127.0.0.1",port = 3306,database = "phishing_emails")# Get Cursorcursor = connection.cursor()except mariadb.Error as e:print(f"Error connecting to MariaDB Platform: {e}")sys.exit(1)print(cursor)