简体   繁体   English

PyMySQL不是从Cron连接,而是从控制台连接

[英]PyMySQL does not connect from Cron but from Console

I have a script that connects to a MySQL DB via pyMySQL . 我有一个脚本,可以通过pyMySQL连接到MySQL数据库。

It works like a charm when I execute it manually from the console, but gives this output when I run this cronjob: 当我从控制台手动执行它时,它就像一个超级按钮,但是当我运行此cronjob时,它给出了以下输出:

@reboot sudo python3 /var/www/html/ls/src/AppBundle/Command/crawl.py true > /tmp/listener.log 2>&1

Result: 结果:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 890, in connect
(self.host, self.port), self.connect_timeout)
 File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

Why is that? 这是为什么? I followed all of this https://stackoverflow.com/a/15684341/1092632 w/out any success. 我没有成功就遵循了所有https://stackoverflow.com/a/15684341/1092632

Any hint appreciated! 任何提示表示赞赏!

Edit 编辑

Things I tried: 我尝试过的事情:

connect = pymysql.connect(host=constants.HOST,
                          user=constants.USERNAME,
                          passwd=constants.PASSWORD,
                          db=constants.DATABASE,
                          charset='utf8mb4',
                          port=constants.PORT,
                          unix_socket=constants.SOCKET,
                          cursorclass=pymysql.cursors.DictCursor)

constants.py constants.py

# MySQL #
HOST = '127.0.0.1' // Localhost, 127.0.0.1 and public IP of Server (having bind to 0.0.0.0
USERNAME = 'admin'
PASSWORD = 'XXX'
DATABASE = 'ls_base'
PORT = '3306' // With and without ''
SOCKET = '/var/run/mysqld/mysqld.sock' // File exists

This problem could be either caused by 1. server configuration or by your 2. python code. 此问题可能是由于1.服务器配置或您的2. python代码引起的。

for point 2, to be sure to exclude your code from the error, try this: 对于第2点,要确保从错误中排除代码,请尝试以下操作:

import MySQLdb
def dbconnect():
    try:
        db = MySQLdb.connect(
            host='localhost',
            user='root',
            passwd='XXX',
            db='myDB'
        )
    except Exception as e:
        sys.exit(e)
    return db
print dbconnect()

this code runs whit cron on a STOCK, RHEL server: 这段代码在STOCK RHEL服务器上运行cron cron:

* * * * * root /var/www/html/myApp/stopClock/stopClock.py

If this does not work, and you get the same error, the problem is your server cron config: 如果这不起作用,并且您收到相同的错误,则问题是服务器的cron配置:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/var/www/html/myApp

This is what I use. 这就是我用的。 and of course the Shebang in my first line of code: 当然还有我第一行代码中的Shebang:

#!/usr/bin/env python

I too had errno 111 when my python3 code connected to my localhost database from a cron @reboot execution. 当我的python3代码从cron @reboot执行连接到本地数据库时,我也有errno 111。 The python3 code would run fine when executing from shell. 从shell执行时,python3代码可以正常运行。 After much debugging I found out that the database connection was being made too soon. 经过大量调试后,我发现数据库连接建立得太早了。 I added this to my code: 我将此添加到我的代码中:

import subprocess

while subprocess.Popen('service mysqld status', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8').find('Active: active') == -1:
     sleep(1)
#once mysql is ready it will break out of while loop and continue to
connection = pymysql.connect(host='localhost'............the rest of your code

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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