Python连接到数据库不能连接

2024-09-19 23:38:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的问题

当我通过python将数据放入本地主机服务器的数据库时,我执行以下操作

db= MySQLdb.connect("localhost", "root", "pass", "Database")

现在我有一个主机服务器,我和我的朋友共享

Cpanel数据库部分的快照如附件所示。在

enter image description here

现在我应该在下面的代码中输入什么来连接数据库

^{pr2}$

我试过了,但失败了]

db= MySQLdb.connect("moeenmh.com","moeenmh_ahmed","<mypass>","moeenmh_ahmed")

我为自己创建的用户是moeenmh_ahmed,并且有密码]

错误是"Access denied for user 'moeenmh_ahmed'@'91.74.98.6' (using password: YES)")


Tags: 数据服务器数据库localhostdbconnect朋友pass
2条回答

数据库名称必须是全名,而不仅仅是_ahmed部分。所以它应该是moeemnh_adhmed。对于远程连接,协议应为“TCP”。在

另外,检查您是否为远程连接配置了MySQL。因此,首先尝试使用:
mysql -u moeenmh_ahmed -h moeenmh.com -p

请看下面这个设置特权的答案:https://stackoverflow.com/a/12844804/1431750。引用:

You have to put this as root:

GRANT ALL PRIVILEGES ON *.* TO  'USERNAME'@'IP'  IDENTIFIED  BY  'PASSWORD';

where IP is the IP you want to allow acess and USERNAME is the user you use to connect

If you want to allow access from any IP just put % instead of your IP

and then you only have to put

FLUSH PRIVILEGES

or restart mysql server and that's it

从Cpanel屏幕截图中我可以看到moeenmh_ahmed没有访问moeenmh_ahmed DB的权限。 您可以在Cpanel文档中看到如何执行此操作: http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/MySQLDatabases#Define用户的权限

相关问题 更多 >