【问题标题】:Can't connect as root without a password sqlalchemy没有密码sqlalchemy无法以root身份连接
【发布时间】:2014-06-30 03:29:50
【问题描述】:

我在不提供密码的情况下从 python 连接到 mysql 时遇到问题。

我可以使用命令行 mysql 客户端以 root 用户身份连接到 MySQL 服务器,而无需密码:

# mysql -u root -h localhost

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.5.37-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

但是当我尝试执行 pything 脚本时:

# cat tomas.py
import sqlalchemy
engine = sqlalchemy.create_engine("mysql://root:@localhost:3306",echo=True)
connection = engine.connect()

我收到此错误:

# python tomas.py
Traceback (most recent call last):
  File "tomas.py", line 3, in <module>
    connection = engine.connect()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1709,     in connect
    return self._connection_cls(self, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 59, in     __init__
    self.__connection = connection or engine.raw_connection()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1778,     in raw_connection
    return self.pool.unique_connection()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 273, in     unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 626, in     _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 433, in     checkout
    rec = pool._do_get()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 945, in     _do_get
    return self._create_connection()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 278, in     _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 404, in     __init__
    self.connection = self.__connect()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 527, in     __connect
    connection = self.__pool._creator()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line     95, in connect
    connection_invalidated=invalidated
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 185, in     raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line     89, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 376,     in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (OperationalError) (1045, "Access denied for user     'root'@'localhost' (using password: NO)") None None



mysql> show grants;
+--------------------------------------------------------------------------------------    --------------------------------------------------+
| Grants for root@localhost                                                                                                                  |
+--------------------------------------------------------------------------------------    --------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD     '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                               |
+--------------------------------------------------------------------------------------    --------------------------------------------------+
2 rows in set (0.00 sec)

有什么提示吗?

附:我无法修改连接字符串,因为它在我正在使用的脚本中进行了硬编码。

【问题讨论】:

  • 好像有一些root密码,可能是in a config file。 (见相关here
  • 在 cli 版本中尝试-h 127.0.0.1:3306,强制建立 tcp 连接。如果您拒绝访问,那么这是 mysql 授权表中的 user@host-type 错误配置。
  • 用密码识别....所以有密码

标签: python mysql sqlalchemy


【解决方案1】:

你应该试试这个:

在 MySQL 控制台中:

use mysql;
update user set password=null where User='root' and Host='localhost';
flush privileges;

如果你想改变它:

use mysql;
update user set password=password('mynewpassword') where User='root';
flush privileges;

【讨论】:

  • 如果此答案有助于解决您的问题,请不要忘记将其标记为正确...干杯 ;)
猜你喜欢
  • 2011-12-07
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 2017-09-08
相关资源
最近更新 更多