第一步:安装pymysql库pip3 install pymysql
第二步:在pycharm中配置pymysql
第三步:在项目代码中导入pymysql模块import pymysql
第四步:创建连接
try:
connect=pymysql.connect(
host="localhost",
port = 3306,
user = "root",
password = "123456",
database = "test"
)
print("数据库连接成功")
except pymysql.Error as e:
print("数据库连接失败"+str(e))
```