简体   繁体   中英

Python CRON not find the MySQL Driver

I don't know PYTHON but I have to run a script as cronjob on a linux (CentOS6) server and I'm getting some issue.

The script works well if I run it from a browser ( http://xx.xx/xx.py ) or from bash. In both cases it load modules and update the MySql DB.

The issue is when it is running by CRON :

/usr/local/bin/python2.7 /home/itsme/cron/demo.py >> /home/itsme/cron/logs/cron_log.log

on the error log I have:

Failed to connect to MySql DB using connect string- DRIVER={MySQL};SERVER=localhost;DATABASE=dbname;UID=admin;PWD=*******# .Reconnect Attempt# 1

<class 'pyodbc.Error'> ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')

Which means the MySql driver is not found. So I try: # odbcinst -j

unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

VI /etc/odbcinst.ini

# Example driver definitions

# Driver from the postgresql-odbc package
# Setup from the unixODBC package
[PostgreSQL]
Description     = ODBC for PostgreSQL
Driver          = /usr/lib/psqlodbc.so
Setup           = /usr/lib/libodbcpsqlS.so
Driver64        = /usr/lib64/psqlodbc.so
Setup64         = /usr/lib64/libodbcpsqlS.so
FileUsage       = 1


# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description     = ODBC for MySQL
Driver          = /usr/lib/libmyodbc5.so
Setup           = /usr/lib/libodbcmyS.so
Driver64        = /usr/lib64/libmyodbc5.so
Setup64         = /usr/lib64/libodbcmyS.so
FileUsage       = 1

If I print the sys.path from the script I have:

       ['/home/itsme/cron/', '/usr/local/lib/python2.7/site-packages/setuptools-7.0-py2.7.egg',
     '/usr/local/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg',
'/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', 
'/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', 
'/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/site-packages', '/home/itsme/cron/src', '/usr/lib']

Why CRON is unable to find the MySQL driver ?

Thanks for any help.

I think you are missing some environment variable in your cron environment which is causing this issue.

In your terminal, from where this script works, run the following command

printenv > env.normal.text

Now, run a dummy cron job which outputs the environment variables like below

* * * * * * printenv > env.cron.text

Now just compare the two for the relevant missing variable. Set it in your cron.

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