虚拟机(Virtual Machine, VM)是一种通过软件模拟的完整计算机系统,可以在物理计算机上运行多个独立的操作系统实例。MySQL是一种流行的关系型数据库管理系统(RDBMS),广泛用于存储和管理数据。
my.cnf
或my.ini
),确保bind-address
设置为虚拟机的IP地址或注释掉该行以允许所有IP连接。假设虚拟机的IP地址为192.168.1.100
,以下是连接MySQL数据库的示例代码(Python):
import mysql.connector
config = {
'user': 'remote_user',
'password': 'password',
'host': '192.168.1.100',
'database': 'your_database',
'raise_on_warnings': True
}
try:
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
query = ("SELECT * FROM your_table")
cursor.execute(query)
for row in cursor:
print(row)
except mysql.connector.Error as err:
print(f"Something went wrong: {err}")
finally:
cursor.close()
cnx.close()
通过以上步骤,你应该能够成功连接到虚拟机上的MySQL数据库。如果遇到具体问题,请根据错误信息进行排查和解决。
领取专属 10元无门槛券
手把手带您无忧上云