I am using the mysql-connector-python
library of mysql to try to connect to the MariaDB
database of other devices in the local area network, but it failed, and it exited directly without outputting any error messages.
Problem Description
Software version:
-
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
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 exits directly without outputting any error messages.
I checked the service record of 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)
Trying to solve the problem
Because the MariaDB
server can receive the connection request, the firewall configurations of the two devices are normal.
I checked the permissions of the logged-in user and found that they are also normal.
I searched a lot of information and tried various methods. But no matter what, I have not been able to solve this problem.
Finally, I tried using another python library pymysql
(version: 1.1.1) to connect to the MariaDB
server. The code is as follows:
import pymysql
# Connect to 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 are no errors, and the connection is successfully established and the MariaDB version is printed.
But I still don’t know the reason why I couldn’t connect before.
Other versions of this page
This article has multiple language versions.
If you would like to leave a comment, please visit the following pages:
These pages are view-only and do not support comments or messages, but 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
This article is translated from Chinese (Simplified) to English by AI.