I used the mysql-connector-python
library to try to connect to the MariaDB
database on other devices within the local area network, but it failed and exited directly without outputting any error messages.
Problem Description#
Software versions:
-
MariaDB server 10.11.6
-
mysql-connector-python 9.2.0
-
MariaDB server, IP is 192.168.1.60
-
Client, IP is 192.168.1.35
The Python code is as follows:
import mysql.connector
import logging
# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
try:
logging.info("Connecting to the database...")
connection = mysql.connector.connect(
host="192.168.1.60",
port=3306,
user="mysql",
password="xxx")
logging.info("Connection established.")
# Create a cursor object to execute SQL queries
cursor = connection.cursor()
# Get all databases
logging.info("Executing SHOW DATABASES query...")
cursor.execute("SHOW DATABASES")
# Print all databases and the table names in each database
for (database,) in cursor.fetchall():
logging.info(f"Database: {database}")
# Switch to the current database
cursor.execute(f"USE {database}")
# Get and print all tables in the current database
cursor.execute("SHOW TABLES")
for (table,) in cursor.fetchall():
logging.info(f" Table: {table}")
# Close the cursor and connection
cursor.close()
connection.close()
logging.info("Connection closed.")
except mysql.connector.Error as err:
logging.error(f"Error: {err}")
except Exception as e:
logging.error(f"Unexpected error: {e}")
After running this code, it exited directly without outputting any error messages.
I checked the service logs on the MariaDB
server and found the following error message.
mariadbd: [Warning] Aborted connection 48 to db: 'unconnected' user: 'unauthenticated' host: '192.168.1.35' (This connection closed normally without authentication)
Attempt to Solve the Problem#
Since the MariaDB
server can receive connection requests, it indicates that the firewall configurations on both devices are normal.
I checked the permissions of the login user and found them to be normal as well.
I searched for a lot of information and tried various methods. However, I still could not resolve this issue.
Finally, I tried using another Python library pymysql
(version: 1.1.1) to connect to the MariaDB
server, with the following code:
import pymysql
# Connect to the MariaDB server
conn = pymysql.connect(
host="192.168.1.60",
port=3306,
user="mysql",
password="xxx"
)
cursor = conn.cursor()
cursor.execute("SELECT VERSION()")
print("MariaDB version:", cursor.fetchone()[0])
cursor.close()
conn.close()
There were no errors, and I successfully connected and printed the MariaDB version.
But I still do not know the reason for the previous connection failure.
Other Versions of This Page#
This article has versions in multiple languages.
If you want to leave a comment, please visit the following page:
These pages are for browsing only and do not support comments or messages, but they provide more language options and load faster:
ZH EN ZH-TW JA RU KO CS ES AR FR PT DE TR IT NL SV DA FI PL UK HE RO HU EL HR TH HI BN ID SW VI NO