2016-06-13 211 views
1

我试图从pytest-dbfixtures中使用mysql。我有一个例子测试文件从文件:pytest 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不会创建symlimks到/usr/bin,只能到/usr/local/bin。如果我手动创建的链接:

$ 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

如何使用mysql夹具从pytest dbfixtures ?有没有办法在OS X上使用MySQL 5.7?

+0

你能举一个例子来说明你如何引用'mysql' fixture? –

+1

更新了文章 –

+0

只是为了澄清,同样的例子在Ubuntu上运行在流浪机器上。 –

回答

0

我的设置一些信息:

  • 的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二进制文件(无需使用符号连接)。 当你安装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. 

使用ALTERhttp://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命令。虽然可能只是我。

让我知道你有多远。