SQLAlchemy, Google App Engine, Mysql, MAC OSX

andrea mucci
Pixel Heart
Published in
3 min readSep 17, 2015

--

In this tutorial i will show you how to install MySQL drivers for Google App Engine in your local MAC OSX.
After you can emulate Google Cloud SQL service in your personal MAC OSX and SQLAlchemy.
I suppose that you have installed correctly SQLAlchemy in your virtualenv.
First of all you have to donwload MySQL.
I have used the dmg image
When the installer complete the installation you have the mysql service installed and in you System Preferences you have a the MySQL Service Manager where is possible to start the demon and configure if you want to start Mysql during OSX Startup.
So, now is time to configure the correct user root password.
open you shell and type

mysql -u root -h localhost -p

Leave empty the password prompt
now you are in the mysql command.
Is time to change the password for root user. First you have to select the database that contain the users informations.

mysql> use mysql;

and

mysql> show tables;

and execute this query to return all the current installed users

mysql> select * from user;

the result is a very confusing list of signs and words.
But if you pay more attention you will see all the 4 root users with the different host related.
So we have to launch a query that update the password for all these users:

mysql> UPDATE mysql.user SET Password = PASSWORD(‘secret_root_password’) WHERE User = ‘root’;

Modify the secret_root_password with your password.
After

mysql> FLUSH PRIVILEGES;

Now if you exit and login again, when the prompt will ask you the password you have to use the password configured in the previous query.

Now you have your Mysql installed and configured.
Is time to configure the Python application and SQLAlchemy to connect with your Mysql.
Before all you have to modify your app.yaml file to add the Mysql driver support

application: your-app
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico

- url: .*
script: yourapp.main.app

libraries:
- name: MySQLdb
version: "latest"

So you have to add in the libraries the MySQLdb driver.
Now configure the connection url for SQLAlchemy

'mysql://root:secret-root-password@localhost/mytestapp'

For this example we use the root user but is recommended to use another user with restricted privileges.

Go to mysql shell

mysql -u root -h localhost -p

and create the database

mysql> CREATE DATABASE mytestapp;

Now you have to sync SQLAlchemy with your database engine.
If you don’t know how to sync a database with SQLAlchemy, please go to the documentation site.
Sometimes your obtain an error with the library libmysqlclient.18.dylib
You can solve the problem doing a link with this library, open your command line and type

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/

ReSync SQLAlchemy.
If all work well you can test your Google App Engine application with MySQL like Google Cloud SQL.

--

--

andrea mucci
Pixel Heart

Product of Italian Design from the summer of 75. In 2020, thanks to the pandemic lockdown i have released a python microservice framework calles MinOS.