问题
I'm setting up an application on OpenShift. It requires a Python script to run every hour and extract data from an online server into a MySQL database on OpenShift. I've been filling this database for some time now by running the Python script locally on my computer and using the port-forward technique. This works like a charm. Underneath is the port-forwarding info that's displayed while doing this.
Service Local OpenShift
------- -------------- ---- -------------------
httpd 127.0.0.1:8080 => 127.12.248.131:8080
mysql 127.0.0.1:3306 => 127.12.248.130:3306
node 127.0.0.1:8081 => 127.12.248.129:8080
Press CTRL-C to terminate port forwarding
And the variables I use in my local script...
host='127.0.0.1'
user='user_placeholder'
passwd='password_placeholder'
db='3v3'
port=3306
Of course I'd like the script to run automatically on the server of OpenShift so I don't have to do so myself. After a quick search I stumbled upon the cron method. It's possible to just put a python script in a map to make it run every hour. I set up some environment variables to access my database, just like I did in the local script. However, the script can't seem to connect to the MySQL database when I tail it. I've even printed the environment variables out and they're exactly the same as the ones I used to succesfully port-forward.
host=os.environ['OPENSHIFT_EXTMYSQL_DB_HOST'] # prints '127.12.248.130'
user=os.environ['OPENSHIFT_EXTMYSQL_DB_USERNAME'] # prints 'user_placeholder'
passwd=os.environ['OPENSHIFT_EXTMYSQL_DB_PASSWORD'] # prints 'password_placeholder'
db=os.environ['OPENSHIFT_EXTMYSQL_DB_NAME'] # prints '3v3'
port=int(os.environ['OPENSHIFT_EXTMYSQL_DB_PORT']) # prints 3306
The error message:
File "/var/lib/openshift/57c57f1889f5cfd9bb00006b/app-root/runtime/repo/.openshift/cron/minutely/test.py",
line 36, in db = MySQLdb.connect(host=os.environ['OPENSHIFT_EXTMYSQL_DB_HOST'], user=os.environ['OPENSHIFT_EXTMYSQL_DB_USERNAME'], passwd=os.environ['OPENSHIFT_EXTMYSQL_DB_PASSWORD'], db=os.environ['OPENSHIFT_EXTMYSQL_DB_NAME'], port=int(os.environ['OPENSHIFT_EXTMYSQL_DB_PORT']))
File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py",
line 81, in Connect return Connection(*args, **kwargs)
File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/connections.py",
line 187, in init super(Connection, self).init(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.12.248.130' (113)")
I honestly don't know where this could go wrong. I know that it's not much of a lead but if anyone thinks to know what the problem could be, I'd be glad to hear.
来源:https://stackoverflow.com/questions/39681552/openshift-cant-connect-to-mysql-db-with-a-cron-python-script