使用App Engine连接MySQL DB

时间:2017-05-07 13:49:44

标签: python mysql google-app-engine mysql-python

我正在关注Google Cloud的MySQL教程here。我复制了main.py中的代码,但是我收到了以下错误:

File "D:\BitBucket\Code\main.py", line 44, in get db = connect_to_cloudsql() File "D:\BitBucket\Code\main.py", line 34, in connect_to_cloudsql db = MySQLdb.connect(host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD) File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 191, in __init__ super(Connection, self).__init__(*args, **kwargs2) TypeError: connect() argument 2 must be string, not None

我看到connect_to_cloudsql()需要带两个参数,但我在文档中找不到任何指向什么的东西。

我的app.yaml看起来像这样:

  # application: reliance-it
  # # application: reliance-test
  # module: default
  # version: 5
  # runtime: python27
  # api_version: 1
  # instance_class: F2
  # threadsafe: true
  # automatic_scaling:
  #   min_idle_instances: 0
  #   max_idle_instances: automatic
  #   min_pending_latency: 30ms
  #   max_pending_latency: automatic

  application: reliance-group
  module: default
  version: 1
  runtime: python27
  api_version: 1
  threadsafe: yes

  env_variables:
      CLOUDSQL_CONNECTION_NAME: reliance-group:us-central1:cash-flow
      CLOUDSQL_USER: root
      CLOUDSQL_PASSWORD: p_ROOTadmin$123

  libraries:
  - name: MySQLdb
    version: "latest"

  - name: webapp2
    version: latest
  - name: jinja2
    version: latest
  - name: pycrypto
    version: "2.6"
  - name: PIL
    version: latest

  inbound_services:
  - mail

  handlers:
  - url: /stylesheets
    static_dir: css
    mime_type: text/css
  - url: /scripts
    static_dir: js

  - url: /favicon\.ico
    static_files: css/images/favicon.ico
    upload: css/images/favicon.ico



  #========Default Handler=========
  - url: /.*
    script: main.application

1 个答案:

答案 0 :(得分:0)

原来CLOUDSQL_CONNECTION_NAME = os.environ.get('connection-name')的输出为None。只需将其替换为CLOUDSQL_CONNECTION_NAME = 'connection-name'并对CLOUDSQL_USERCLOUDSQL_PASSWORD执行相同操作即可。如果他们遇到同样的问题,我希望人们觉得我的答案很有帮助。