Mysql不会加载应用程序的用户数据

时间:2016-12-31 22:15:33

标签: python mysql sql flask

我刚刚将我的本地dev env中的应用程序复制到prod env,我的数据未加载到我的烧瓶应用中,而是显示no blog posts right now...

我已经验证了以下内容:

  • db user具有表格权限。
  • mysql正在运行
  • 再次验证查询(工作正常)
  • 尝试将MYSQL_DATABASE_HOST设置为localhostxx.xxx.xxx.xxx,但无济于事。
  • 验证了我的导入(from flask_mysqldb import MySQLmysql正常工作) - 它是

app.py

from flask_mysqldb import MySQL

app = Flask(__name__)
app.secret_key = 'mysecret'

# mail server config
app.config['MAIL_SERVER'] = 'mailserver.net'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'contact@mysite.com'
app.config['MAIL_PASSWORD'] = 'mypass'


# mysql config
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_PORT'] = '3306'
app.config['MYSQL_DB']   = 'mydb'
app.config['MYSQL_USER'] = 'myusr'
app.config['MYSQL_PASSWORD'] = 'mypass'

mysql = MySQL()
mysql.init_app(app)
c = mysql.connect().cursor()
@app.route('/', methods=('GET', 'POST'))
def email():
    form = EmailForm()

c.execute("SELECT post_title, post_name, YEAR(post_date) as YEAR, MONTH(post_date) as MONTH FROM mydb.wp_posts WHERE post_status='publish' ORDER BY RAND() LIMIT 3")
    blogposts = c.fetchall()

    print(blogposts)
    if request.method == 'POST':
        if form.validate() == False:
            return 'Please fill in all fields <p><a href="/">Try Again</a></p>'
        else:
            msg = Message("Message from your visitor",
                          sender='contact@mysite.com',
                          recipients=['contact@mysite.com'])
            msg.body = """
            From: %s
            """ % (form.email.data)
            mail.send(msg)
            return render_template('email_submit_thankyou.html')
    elif request.method == 'GET':
        return render_template('index.html', form=form, blogposts=blogposts)
if __name__ == '__main__':
    app.run()

templates/index.html包含:

<ul>
    {% for blogpost in blogposts %}
    <li><a href="mysite.com/wordpress/{{blogpost[2]}}/{{blogpost[3]}}/{{blogpost[1]}}">{{blogpost[0]}}</a></li>
    {% else %}
    <li>no blog posts right now...</li>
    {% endfor %}
<div class="clearL"> </div>
</ul>

我找到了一个&#34;慢&#34;在/var/log/mysql/mysql-slow.log中查询:

# Time: 170101 16:34:32
# User@Host: myuser[myuser] @ localhost [127.0.0.1]
# Query_time: 0.004649  Lock_time: 0.001712 Rows_sent: 3  Rows_examined: 53
SET timestamp=1483306472;
SELECT
        post_title, post_name, YEAR(post_date) as YEAR, MONTH(post_date) as MONTH
FROM mydb.wp_posts WHERE post_status='publish' ORDER BY RAND() LIMIT 3;

因此,这里出了什么问题?

0 个答案:

没有答案