我想将 python 连接到数据库,但出现此错误:
连接到 MySQL 2013 (HY000) 时出错:在“读取初始通信数据包”时与 MySQL 服务器失去连接,系统错误:0
代码是这样的:
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='172.31.20.456', # The server name or Ip address on which MySQL is running.
port=5432,
database='jru_ddbb', #The name of the database to which you want to connect and
# perform the operations.
# bind-address = 127.0.0.1
user='root', # The username that you use to work with MySQL Server.
# The default username for the MySQL database is a root.
password='12345') # Password is given by the user at the time of installing
# the MySQL server. If you are using root then you won’t need the password.
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
我怎样才能解决这个问题来连接python到数据库?