PythonAnywhere - Mysql错误:1044(42000):访问被拒绝

时间:2017-09-08 21:58:10

标签: python mysql

我正在尝试使用PythonAnywhere在线托管Python / Mysql项目。当我尝试连接到数据库时:(主机和密码明显改变)

conn = mysql.connector.connect(host='host',user="BobbyQ",password='password',db='mm-database')

我收到此错误:

mysql.connector.errors.ProgrammingError: 1044 (42000): Access denied for user 'BobbyQ'@'%' to database 'mm-database'

如果我从Mysql控制台运行SHOW GRANTS,我会得到:

mysql> SHOW GRANTS;
+------------------------------------------------------------------------------------------------+
| Grants for BobbyQ@%                                                                            |
+------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'BobbyQ'@'%' IDENTIFIED BY PASSWORD <secret> WITH MAX_USER_CONNECTIONS 3 |
| GRANT ALL PRIVILEGES ON `BobbyQ$default`.* TO 'BobbyQ'@'%'                                     |
| GRANT ALL PRIVILEGES ON `BobbyQ$mm-database`.* TO 'BobbyQ'@'%'                                 |
+------------------------------------------------------------------------------------------------+

此时我应该能够连接到数据库,但我不知道为什么我不能。我怎么解决?

1 个答案:

答案 0 :(得分:0)

使用 sudo user 连接到数据库(在终端中)并创建 newuser

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';

然后使用此命令转到 mysql 数据库:

USE MYSQL

并使用 :

授予用户 (newuser) 的所有访问权限
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

写入所需数据库的名称而不是 database_name 和所需的用户名而不是 username

最后:

FLUSH PRIVILEGES;