Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.api.rdbms_mysqldb
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Relational database API stub that uses the MySQLdb DB-API library.
Also see the rdbms module.
"""
import logging
import os
_POTENTIAL_SOCKET_LOCATIONS = (
'/tmp/mysql.sock',
'/var/run/mysqld/mysqld.sock',
'/var/lib/mysql/mysql.sock',
'/var/run/mysql/mysql.sock',
'/var/mysql/mysql.sock',
)
_connect_kwargs = {}
_OS_NAME = os.name
[docs]def SetConnectKwargs(**kwargs):
"""Sets the keyword args (host, user, etc) to pass to MySQLdb.connect()."""
global _connect_kwargs
_connect_kwargs = dict(kwargs)
[docs]def FindUnixSocket():
"""Find the Unix socket for MySQL by scanning some known locations.
Returns:
If found, the path to the Unix socket, otherwise, None.
"""
for path in _POTENTIAL_SOCKET_LOCATIONS:
if os.path.exists(path):
return path
try:
import google
import MySQLdb
from MySQLdb import *
__import__('MySQLdb.constants', globals(), locals(), ['*'])
except ImportError:
def connect(instance=None, database=None):
logging.error('The rdbms API (Google Cloud SQL) is not available because '
'the MySQLdb library could not be loaded. Please see the SDK '
'documentation for installation instructions.')
raise NotImplementedError('Unable to find the MySQLdb library')
else:
[docs] def connect(instance=None, database=None, **kwargs):
merged_kwargs = _connect_kwargs.copy()
if database:
merged_kwargs['db'] = database
merged_kwargs.update(kwargs)
if 'password' in merged_kwargs:
merged_kwargs['passwd'] = merged_kwargs.pop('password')
host = merged_kwargs.get('host')
if ((not host or host == 'localhost') and
not merged_kwargs.get('unix_socket') and
_OS_NAME == 'posix'):
socket = FindUnixSocket()
if socket:
merged_kwargs['unix_socket'] = socket
else:
logging.warning(
'Unable to find MySQL socket file. Use --mysql_socket to '
'specify its location manually.')
logging.info('Connecting to MySQL with kwargs %r', merged_kwargs)
try:
return MySQLdb.connect(**merged_kwargs)
except MySQLdb.Error:
logging.critical(
'MySQL connection failed! Ensure that you have provided correct '
'values for the --mysql_* flags when running dev_appserver.py')
raise
[docs]def set_instance(instance):
logging.info('set_instance() is a noop in dev_appserver.')
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["This code provides an API stub for relational databases using the MySQLdb library within the Google App Engine environment."],["The `SetConnectKwargs` function allows users to define connection parameters like host and user to be used by `MySQLdb.connect()`."],["The `FindUnixSocket` function searches for the Unix socket for MySQL in common locations, returning the path if found or `None` if not."],["The `connect` function handles the establishment of a connection to a MySQL database, merging user-provided parameters with default connection settings."],["The code logs warnings and errors, providing guidance in cases where MySQLdb is not found or if the connection to the database fails."]]],[]]