简体   繁体   中英

pytest mysql db fixture: cannot create database

I'm trying to use mysql from pytest-dbfixtures. I have a test file with example from documentation:

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

and when I run it via $ py.test test_example.py , I get this error:

    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

I am using python 3.5 and OS X 10.11.5. MySQL 5.7.12 is installed via homebrew , and works well.

homebrew doesn't create symlimks to /usr/bin , only to /usr/local/bin . If I create the link manually:

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

I get the following error from 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.

Also, script mysql_install_db is deprecated: http://dev.mysql.com/doc/refman/5.7/en/mysql-install-db.html

How do I use mysql fixture from pytest dbfixtures? Is there a way to use it on OS X with MySQL 5.7?

Some info about my set up:

  • Mac OS X 10.11.5
  • Python 3.4.4
  • MySQL Community Edition 5.7.13

I wasn't exactly able to reproduce the issue you are running into, but I think some of the information below should be useful.

Packages required:

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

I couldn't install mysqlclient initially (got: "OSError: mysql_config not found" ), because mysql was not in the path. Added /usr/local/mysql/bin to the path in .bash_profile

Now you need to configure pytest-dbfixtures to use your mysql connection. For me, the .conf file was located here (I am using virtualenv):

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

For you, the config file should be somewhere in your Python 3.5 installation directory (navigate there and do find . -name dbfixtures.conf ).

There's a section in pytest_dbfixtures/conf/dbfixtures.conf to set your mysql user, password, and the paths to local MySQL binaries (no need to use symlinks) . You should have gotten a password for the root user when you installed MySQL (that said, you'll need to reset it to something new).

You might want to consider creating a new user for MySQL and use its credentials instead (perhaps look at the configuration on the Ubuntu server you referred to in the comments).

If you insist on using the MySQL root user, this might help. I went to check into mysql via the terminal and apparently trying to do SELECT CURRENT_USER() on the root MySQL user requires the following:

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

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

Then go back to dbfixtures.conf and update the password accordingly.

Unfortunately, I get stuck here:

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

Executing the same mysqladmin command via the terminal results in an auth failure. Might just be me though.

Let me know how far you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM