I would like to interface between Python and Google SQL Cloud using SQLAlchemy. I have tried to run the following code:
from sqlalchemy import *
engine = create_engine("mysql+mysqldb://root@/<database>?unix_socket=/cloudsql/<projectid>:<instancename>")
df.to_sql('jobsbank', engine, flavor='mysql', if_exists='append',index=True)
create_engine is an SQLAlchemy method, and df is a Pandas dataframe.
I am using a Mac OS 10.11.1 (El Capitan), and MySQL version is 5.7.10 for OS X 10.9.
Problem 1: In a Python 3.4 environment, I get "image not found".
I use the Python 3.4 environment, and install the following MySQLdb package which is meant to support Python 3: https://github.com/PyMySQL/mysqlclient-python/.
However, when I run my script, it throws out the following error:
ImportError Traceback (most recent call last)
<ipython-input-7-dd22983d5391> in <module>()
----> 1 import MySQLdb
/Users/---/anaconda/envs/scrape/lib/python3.4/site-packages/mysqlclient-1.3.7-py3.4-macosx-10.5-x86_64.egg/MySQLdb/__init__.py in <module>()
17 from MySQLdb.release import __version__, version_info, __author__
18
---> 19 import _mysql
20
21 if version_info != _mysql.version_info:
ImportError: dlopen(/Users/---/anaconda/envs/scrape/lib/python3.4/site-packages/mysqlclient-1.3.7-py3.4-macosx-10.5-x86_64.egg/_mysql.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
Referenced from: /Users/---/anaconda/envs/scrape/lib/python3.4/site-packages/mysqlclient-1.3.7-py3.4-macosx-10.5-x86_64.egg/_mysql.so
Reason: image not found
Problem 2: In Python 2.7, I get the error can't connect to local MySQL server through socket ...
When I tried the same code in a Python 2.7 environment, with pip install python-mysql instead of the MySQLdb package above, I get the error message:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/cloudsql/myprojectid:myinstancename' (2)")
I have already taken the necessary steps to authorise my IP address and can access the google cloud database via the SQL client on my command line, so am not sure why I am getting this?
Side problem: For some reason, I cannot import SQLalchemy in the iPython Notebook console when working in the Python 2.7 environment
On a different note, I am having a strange problem whereby I can import SQLAlchemy in the Python 2.7 environment from the command line, but not from the iPython notebook started up in that same environment. When I try to import sqlalchemy using the notebook, it tells me that the module does not exist:
ImportError: No module named sqlalchemy
This occurs even though conda list in that environment shows me that sqlalchemy is installed.
I am terribly sorry for the expansive messiness of this post. There seems to be a spanner in every route I've tried. I have trawled through every post on StackOverflow related to this and tried all the suggestions, but nothing seems to work. Any help at all will be appreciated.
If this is too problematic, other suggestions on how I can upload my data via Python onto a cloud SQL database (does not have to be google's), will be useful as well.