python - pytest mysql 数据库 fixture : cannot create database

标签 python mysql mysql-python pytest fixtures

我正在尝试使用来自 pytest-dbfixtures 的 mysql。我有一个测试文件,其中包含文档中的示例:

def test_using_mysql(mysql):
    mysql.query("SELECT CURRENT_USER()")

当我通过 $ py.test test_example.py 运行它时,我得到这个错误:

    E               subprocess.CalledProcessError: Command '/usr/bin/mysql_install_db --user=Fluffy --datadir=/tmp/mysqldata_3307' returned non-zero exit status 127

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py:711: CalledProcessError
    ------------------- Captured stderr setup ------
   [ERROR] /bin/sh: /usr/bin/mysql_install_db: No such file or directory

我使用的是 python 3.5 和 OS X 10.11.5。 MySQL 5.7.12 通过 homebrew 安装,运行良好。

homebrew 不会为 /usr/bin 创建符号链接(symbolic link),只会为 /usr/local/bin 创建符号链接(symbolic link)。如果我手动创建链接:

$ sudo ln -s /usr/local/Cellar/mysql/5.7.12/bin/mysql_install_db /usr/bin/mysql_install_db

我从 pytest 得到以下错误:

E               subprocess.CalledProcessError: Command '/usr/bin/mysql_install_db --user=Fluffy --datadir=/tmp/mysqldata_3307' returned non-zero exit status 1

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py:711: CalledProcessError
------------------------- Captured stderr setup ----------------------
 [ERROR]   Can't locate the language directory.

此外,脚本 mysql_install_db 已弃用:http://dev.mysql.com/doc/refman/5.7/en/mysql-install-db.html

如何使用 pytest dbfixtures 中的 mysql fixture?有没有办法在带有 MySQL 5.7 的 OS X 上使用它?

最佳答案

关于我的设置的一些信息:

  • Mac OS X 10.11.5
  • python 3.4.4
  • MySQL 社区版 5.7.13

我无法完全重现您遇到的问题,但我认为下面的一些信息应该有用。

需要的包:

pytest==2.9.2
pytest-dbfixtures==0.13.1
mysqlclient==1.3.7

我最初无法安装 mysqlclient(得到:“OSError: mysql_config not found”),因为 mysql 不在路径中. 将 /usr/local/mysql/bin 添加到 .bash_profile

中的路径

现在您需要配置 pytest-dbfixtures 以使用您的 mysql 连接。 对我来说,.conf 文件位于此处(我使用的是 virtualenv):

~/.virtualenvs/py3.4-PySA/lib/python3.4/site-packages/pytest_dbfixtures/conf/dbfixtures.conf

对于您来说,配置文件应该在您的 Python 3.5 安装目录中的某处(导航到那里并执行 find .-name dbfixtures.conf)。

pytest_dbfixtures/conf/dbfixtures.conf 中有一个部分用于设置您的mysql 用户、密码和本地 MySQL 二进制文件的路径(无需使用符号链接(symbolic link))。 安装 MySQL 时,您应该已经获得了 root 用户的密码(也就是说,您需要将其重置为新密码)。

您可能需要考虑为 MySQL 创建一个新用户并改用其凭据(也许查看您在评论中提到的 Ubuntu 服务器上的配置)。

如果您坚持使用 MySQL root 用户,这可能会有所帮助。我通过终端检查了 mysql 并且显然试图在 root MySQL 用户上执行 SELECT CURRENT_USER() 需要以下内容:

mysql> SELECT CURRENT_USER();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

使用 ALTER:http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

然后返回 dbfixtures.conf 并相应地更新密码。

不幸的是,我卡在这里了:

def stop_server_and_remove_directory():
        shutdown_server = (
            mysql_admin_exec,
            '--socket=%s' % unixsocket,
            '--user=%s' % config.mysql.user,
            'shutdown'
        )
>       subprocess.check_output(' '.join(shutdown_server), shell=True)

~/.virtualenvs/py3.4-PySA/lib/python3.4/site-packages/pytest_dbfixtures/factories/mysql.py:143: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

...
>               raise CalledProcessError(retcode, process.args, output=output)
E               subprocess.CalledProcessError: Command '/usr/local/mysql/bin/mysqladmin --socket=/tmp/mysql.3307.sock --user=root shutdown' returned non-zero exit status 1

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py:620: CalledProcessError

通过终端执行相同的 mysqladmin 命令会导致身份验证失败。不过可能只是我。

让我知道您的进展情况。

关于python - pytest mysql 数据库 fixture : cannot create database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790847/

相关文章:

python - 确定多记录html形式的记录

python - 使用 2 个 M2M 字段的相关名称过滤 Django 模型

Mysql查询将总天数转换为年月日

python - MySQLdb python 在列上插入行或增加计数(如果存在)

python-3.x - 如何使用 python 3.2 安装 MySQLdb

python - MySQLdb 游标不支持空迭代器吗?

python - Python 中的 Pandas 错误 : columns must be same length as key

python - 解码 7 位 GSM

php - 使用 php 从数据库填充 HTML 下拉框,但我缺少一条记录

php - 使用 PHP MySql 的标签系统