问题描述
我只是尝试使用 Flask 和 Python 连接我的数据库。
我从1 week
收到此错误,我搜索并尝试了所有方法。
每次都这样说:
OperationalError: (1045, "Access denied for user 'rajendra'@'localhost' (using password: NO)")
这是我的源代码:
from flask import Flask
from flask_mysqldb import MySQL
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '1234'
app.config['MYSQL_DATABASE_DB'] = 'EmpData'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
@app.route("/")
def main():
conn = mysql.connect()
cursor = mysql.connect().cursor()
cursor.execute("SELECT * FROM User")
data = cursor.fetchone()
print data
它显示此。
和控制台错误:
/usr/bin/python2.7 /home/rajendra/PycharmProjects/untitled/myApp/run.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
127.0.0.1 - - [17/Oct/2015 19:00:09] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/rajendra/PycharmProjects/untitled/myApp/views.py", line 15, in nain
conn = mysql.connect()
File "/usr/local/lib/python2.7/dist-packages/flask_mysqldb/__init__.py", line 81, in connect
return MySQLdb.connect(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (1045, "Access denied for user 'rajendra'@'localhost' (using password: NO)")
127.0.0.1 - - [17/Oct/2015 19:00:10] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [17/Oct/2015 19:00:10] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [17/Oct/2015 19:00:10] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [17/Oct/2015 19:00:10] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [17/Oct/2015 19:00:11] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [17/Oct/2015 19:00:11] "GET /?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
1楼
你应该使用MYSQL_HOST
, MYSQL_USER
, MYSQL_PASSWORD
和MYSQL_DB
配置指令: : MYSQL_DB
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '1234'
app.config['MYSQL_DB'] = 'EmpData'
app.config['MYSQL_HOST'] = 'localhost'