【发布时间】:2023-11-09 14:03:01
【问题描述】:
我是 MySQL 新手,我正在尝试使用 MySQL 数据库和 python 以及 FLask 作为服务器端框架来制作一个简单的注册表单。
MySQL 配置:
app.config['MySQL_HOST'] ='localhost'
app.config['MySQL_USER'] = 'blacksmith'
app.config['MySQL_PASSWORD'] = '123456'
app.config['MySQL_DB'] = 'myflaskapp'
app.config['MySQL_CURSORCLASS'] = 'DictCursor'
向路由注册函数
@app.route('/register',methods=['GET','POST']) #route accepts get request per default but we precise here post also
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate(): # check if form is validated and its a post request
name = form.name.data
email = form.email.data
username= form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
#create cursor
cur =mysql.connection.cursor()
# execute SQL command
cur.execute("INSERT INTO users(name,email,username,password) VALUES (%s,%s,%s,%s)",(name,email,username,password) )
#commit to DB
mysql.connection.commit()
#close connection
cur.close()
return render_template('register.htm',form=form)
当我使用命令在我的 bash shell 中访问 MySQL shell 时,我也遇到了同样的错误(Access denied for user root@localhost)
mysql -u root
(我必须使用 sudo 命令 来访问数据库)
我没有为 MySQL 数据库访问设置 任何密码 的问题,即使尝试使用命令使用另一个密码更改它,同样的错误仍然存在:
ALTER USER 'user'@'hostname' IDENTIFIED BY 'newPass';
我也尝试在 MySQL 配置中更改密码,但没有成功。
请帮帮我!
【问题讨论】:
-
检查该用户的授权
-
@Bukse 谢谢,但这无济于事:操作 ALTER USER 为 'root@localhost' 失败
标签: python mysql flask localhost access-denied