Home  >  Article  >  Backend Development  >  Solution to unsuccessful installation of mysql in Python under Mac

Solution to unsuccessful installation of mysql in Python under Mac

小云云
小云云Original
2018-02-27 11:18:381628browse

If mysql-python is installed, an error is reported after importing MySQLdb. It is recommended not to use mysql-python. It is not supported after Python 3.6.

I personally use Third-party library pymysql, enter the following code in the terminal to install

sudo pip install pymysql

After installation, if you are using flask_sqlalchemy, please change the driver name to pymysql

SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:
[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'SQLALCHEMY_TRACK_MODIFICATIONS = True

The following is an example of a configuration file used to connect to the database

# -*- coding:utf-8 -*-# config.pyDIALECT = 'mysql'DRIVER = 'pymysql'USERNAME = 'root'PASSWORD = 'root'# HOST = '127.0.0.1'    # 自己电脑的ip 或者localhostHOST = 'localhost'      # 自己电脑的ipPORT = '3306'           # MySQL默认的端口号DATABASE = 'db_demo1'# SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'# SQLALCHEMY_TRACK_MODIFICATIONS = True# 数据库连接必须用这个名字  SQLALCHEMY_DATABASE_URISQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
# -*- coding:utf-8 -*-# db_test.pyfrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport config

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

db.create_all()@app.route('/')def index():
    return 'index'if __name__ == '__main__':
    app.run()

             

If mysql-python is installed and an error is reported after importing MySQLdb, it is recommended not to use mysql-python. It is not supported after Python 3.6.

I personally use a third-party library pymysql. Enter the following code in the terminal to install

sudo pip install pymysql

After installation, if Using flask_sqlalchemy Please change the driver name to pymysql

SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:
[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'SQLALCHEMY_TRACK_MODIFICATIONS = True

The following is an example of a configuration file used to connect to the database

# -*- coding:utf-8 -*-# config.pyDIALECT = 'mysql'DRIVER = 'pymysql'USERNAME = 'root'PASSWORD = 'root'# HOST = '127.0.0.1'    # 自己电脑的ip 或者localhostHOST = 'localhost'      # 自己电脑的ipPORT = '3306'           # MySQL默认的端口号DATABASE = 'db_demo1'# SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'# SQLALCHEMY_TRACK_MODIFICATIONS = True# 数据库连接必须用这个名字  SQLALCHEMY_DATABASE_URISQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
# -*- coding:utf-8 -*-# db_test.pyfrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport config

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

db.create_all()@app.route('/')def index():
    return 'index'if __name__ == '__main__':
    app.run()

Related recommendations:

Detailed explanation of the steps to install MySql5.7.21 in Linux

How to install MySQL5.7 on Windows 10 and change the forgotten root password

Linux environment Graphic tutorial for installing MySQL5.6

The above is the detailed content of Solution to unsuccessful installation of mysql in Python under Mac. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn