Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

Pythonanywhere解决链接Mysql问题(部署flask)

2019/09/15 Python 环境部署 MySQL
Word count: 606 | Reading time: 3min

Pythonanywhere解决链接Mysql问题(部署flask)

由于在bash控制台里通过命令mysql -p(这其实是因为错误的连接方式,由于不是默认的端口3306,自然连接不到,官方文档中有正确的连接方式)链接不到Mysql时,再加上进虚拟环境用python manage db migrate也失败后,我是异常慌张的…然后立马发现关于pythonanywhere链接Mysql的解决不是很多,要么就是说免费账号是无法用的…心顿时拔凉,但最后还是找到了pythonanywhere官方的文档,解决了这个问题,发现都是配置的问题

一开始的我本地及阿里云链接mysql数据库配置文件是这样写的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#[config.py]
DIALECT = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'root'
PASSWORD = 'zxcqwe' #瞎写的
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'restaurant'
DEBUG = True

SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(
DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)

SQLALCHEMY_TRACK_MODIFICATIONS = False

后来根据说明改成了如下:

1
2
3
4
5
6
7
8
9
10
11
12
#[config.py]
DIALECT = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'nymrli'
PASSWORD = 'zxcqwe' #瞎写的
HOST = 'nymrli.mysql.pythonanywhere-services.com'
PORT = '3306'
DATABASE = 'nymrli$restaurant'
DEBUG = True
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(
DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False

其中修改的地方为

  • USERNAME为pythonanywhere账号
  • PASSWORDDatabase详情页设置的数据库密码
  • HOSTDatabase详情页上的Database host address
  • DATABASEDatabase详情页上 create a database的数据库名,但pythonanywhere会自动给你加上前缀<username>$<database_name>,所以我这边就是nymrli$restaurant
  • PORT默认还是3306

附上官方文档:

配置说明:

开始使用MySQL,您需要去在仪表板的MySQL选项卡,并设置一个密码。你也会找到连接设置选项卡(主机名、用户名),以及创建新的数据库的能力。

你可以开始一个新的MySQL控制台访问数据库从这个选项卡,或者您可以打开一个MySQL从bash shell使用以下命令控制台或ssh会话:

mysql -u USERNAME -h HOSTNAME -p 'USERNAME$DATABASENAME'

通过SSH链接,据说免费账户不支持:
1
2
3
4
5
6
7
8
9
10
Setting	Value
SSH Hostname: ssh.pythonanywhere.com
SSH Username: your PythonAnywhere username
SSH Password: the password you use to log in to the PythonAnywhere website
SSH Key file: should not be necessary when you specify the password
MySQL Hostname: your PythonAnywhere database hostname, eg. yourusername.mysql.pythonanywhere-services.com
MySQL Server Port: 3306
Username: your PythonAnywhere username
Password: your PythonAnywhere database password
Default Schema: your database name, eg yourusername$mydatabase

Author: Mrli

Link: https://nymrli.top/2018/11/10/Pythonanywhere解决链接Mysql问题-部署flask/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
Ubuntu更改nginx默认端口
NextPost >
写爬虫过程中碰到的编码问题
CATALOG
  1. 1. Pythonanywhere解决链接Mysql问题(部署flask)
  2. 2. 附上官方文档:
    1. 2.1. 通过SSH链接,据说免费账户不支持: