MySQL 数据库在连接时确实可以不使用 DNS(域名系统)。通常,MySQL 连接可以使用以下几种方式指定目标服务器:
/etc/hosts
文件中有对应的主机名解析,可以使用主机名连接。/etc/hosts
文件中有对应的主机名解析,可以使用主机名连接。~/.my.cnf
)中预先配置好连接信息,这样在连接时就不需要每次都指定 -h
参数。~/.my.cnf
)中预先配置好连接信息,这样在连接时就不需要每次都指定 -h
参数。以下是一个简单的 Python 脚本示例,演示如何使用 mysql-connector-python
库连接到 MySQL 数据库,不使用 DNS:
import mysql.connector
config = {
'user': 'username',
'password': 'yourpassword',
'host': '192.168.1.1',
'database': 'yourdatabase',
'raise_on_warnings': True
}
try:
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
query = ("SELECT * FROM yourtable")
cursor.execute(query)
for row in cursor:
print(row)
except mysql.connector.Error as err:
print(f"Something went wrong: {err}")
finally:
if cnx.is_connected():
cursor.close()
cnx.close()
希望以上信息对你有所帮助!
领取专属 10元无门槛券
手把手带您无忧上云