2

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.

1 Answer 1

3

/cloudsql/<projectid>:<instancename> is a special socket that will only exist when running on App Engine.

To connect externally, you will need to connect by IP address:

mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>

Sign up to request clarification or add additional context in comments.

3 Comments

What will be the port over here? I only have the IP address of the instance.
The default mysql port is 3306, but you can leave it off specifying only mysql+mysqldb://<user>:<password>@<ip_address>/<dbname>
Your first problem looks like a python module installation issue that's unrelated to Cloud SQL. You might get a faster/better answer if you post a separate question with a title like "Problem installing mysqldb package on OS X for Python 3". (The error seems to indicate the module can't find the mysql client library.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.