In this latest release I’ve added a basic MySQL connection pool to the benchmarking script which improves the method in which connections to MySQL are handled and reused. In addition, there have been some optimizations made to the thread handler functions for better debug reporting. Download the latest release now and see how your MySQL server performs against the rest of the community! Download here: download page.
Here are some quick commands for installing the proper packages and requirements for the MySQL dbbenchmark program.
export PKG_PATH="ftp://openbsd.mirrors.tds.net/pub/OpenBSD/4.7/packages/amd64/" pkg_add -i -v wget wget http://dbbenchmark.googlecode.com/files/dbbenchmark-version-0.1.beta_rev26.tar.gz pkg_add -i -v python Ambiguous: choose package for python a 0: 1: python-2.4.6p2 2: python-2.5.4p3 3: python-2.6.3p1 Your choice: 2 pkg_add -i -v py-mysql pkg_add -i -v mysql pkg_add -i -v mysql-server ln -s /usr/local/bin/python2.5 /usr/bin/python gzip -d dbbenchmark-version-0.1.beta_rev26.tar.gz tar -xvf dbbenchmark-version-0.1.beta_rev26.tar cd dbbenchmark-version-0.1.beta_rev26 ./dbbenchmark.py --print-sql - login to mysql and execute sql commands ./dbbenchmark.py
Quick solution to an issue that the affected Debian Lenny release: the process used to collect the MEMORY_ACTIVE_BYTES variable has been modified to correct a situation where some systems report an array of memory information instead of the expected single integer value. The bug has been fixed in revision 21 and the current download (revision 22) is available for download or svn update. As usual, you can download the MySQL dbbenchmark script here: [downloads].
Thanks goes to Brian Vowell at Evernote.com for bringing this bug to my attention. The original bug report can be found here: [link]
There are many times when writing an application that single threaded database operations are simply too slow. In these cases it’s a matter of course that you’ll use multi-threading or forking to spawn secondary processes to handle the database actions. In this simple example for Python multi-threading you’ll see the how simple it is to improve the performance of your python app.
#!/usr/bin/python ## DATE: 2010-08-30 ## AUTHOR: Matt Reid ## WEBSITE: http://themattreid.com ## LICENSE: BSD http://www.opensource.org/licenses/bsd-license.php ## Copyright 2010-present Matt Reid from __future__ import division from socket import gethostname; import threading import sys import os import MySQLdb class threader(threading.Thread): def __init__(self,method): threading.Thread.__init__(self) self.tx = self.method = method def run(self): run_insert() def run_insert(): sql = "INSERT INTO table (`id`,`A`,`B`,`C`) VALUES …[Read more]
We had a very successful weekend of Planet.mysql users submitting their database statistics so I’ve pushed some code into a new release today so that everyone can benefit from some new features. The biggest change is to the threading logic. Previously the benchmarking script was serializing MySQL operations and only making use of a secondary thread (not the invoking thread) to query the database. Now you have the option of running with “–threads=x” to make use of your multi-core server. A good example of this improvement was on my Macbook Pro; before the threading change it was inserting ~700/sec, after the code change I tried –threads=4 and saw an improvement to ~900/sec. Rather significant.
Download the new script now and see how your server compares to the ones in the …
[Read more]MySQL Connector/Python is (or should be) compliant with the Python DB-API 2.0 specification. This means that you can use DBUtils' PooledDB module to implement database connection pooling.
Here below you'll find an example which will output the connection ID of each connection requested through the pooling mechanism.
from DBUtils.PooledDB import PooledDB import mysql.connector def main(): pool_size = 3 pool = PooledDB(mysql.connector, pool_size, database='test', user='root', host='127.0.0.1') cnx = [None,] * pool_size for i in xrange(0,pool_size): cnx[i] = pool.connection() cur = cnx[i].cursor() cur.execute("SELECT CONNECTION_ID()") print …[Read more]
MySQL Connector/Python is (or should be) compliant with the Python DB-API 2.0 specification. This means that you can use DBUtils’ PooledDB module to implement database connection pooling.
Here below you’ll find an example which will output the connection ID of each connection requested through the pooling mechanism.
from DBUtils.PooledDB import PooledDB import mysql.connector def main(): pool_size = 3 pool = PooledDB(mysql.connector, pool_size, database='test', user='root', host='127.0.0.1') cnx = [None,] * pool_size for i in xrange(0,pool_size): cnx[i] = pool.connection() cur = cnx[i].cursor() cur.execute("SELECT CONNECTION_ID()") print …[Read more]
MySQLdb is a Python wrapper around _mysql written by Andy
Dustman. This wrapper makes it possible to interact with a MySQL
Server performing all sorts of DDL and DML statements. I began my
Python journey recently and stumbled at the installation of the
MySQLdb module install. I was keen not to jump at an apt/yum
installation as we have servers that have no outbound connections
I decided I wanted to build the module from source.
You can download the MySQLdb files from SourceForge (70kb)
When downloaded you need to prep before your system is ready to
build the file. Here are some prerequisites that will
make life easier for you. I performed this particular install
using an Ubuntu 10.04 64bit OS.
Before you start ensure you have the following installed (MySQL
isn't actually required but for local Python development …
If you need to work with LVM in your scripts but haven’t found a good method to access details about Logical Volume Groups, here’s a simple Python script that will print the details about any volumes on your system. This could be useful for writing a partition check script for your MySQL data directory (if you’re not using a standard monitoring system like Nagios).
import sys import os import commands import subprocess import select def lvm(): print "" LVM_PATH = "/sbin" LVM_BIN = os.path.join(LVM_PATH, 'lvm') argv = list() argv.append(LVM_BIN) argv.append("lvs") argv.append("--nosuffix") argv.append("--noheadings") argv.append("--units") argv.append("b") argv.append("--separator") argv.append(";") argv.append("-o") argv.append("lv_name,vg_name,lv_size") process = subprocess.Popen(argv, stdout=subprocess.PIPE) output = "" out = process.stdout.readline() output += out lines = …[Read more]
If you’ve been looking for a simple python script to use with MySQL that you can use to expand upon for your next project, check this one out. It has error handling for the connection, error handling for the sql call, and loop iteration for the rows returned.
#!/usr/bin/python import sys import MySQLdb my_host = "localhost" my_user = "user" my_pass = "password" my_db = "test" try: db = MySQLdb.connect(host=my_host, user=my_user, passwd=my_pass, db=my_db) except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit (1) cursor = db.cursor() sql = "select column1, column2 from table"; cursor.execute(sql) results = cursor.fetchall() for row in results: column1 = row[0] column2 = row[1] print "column1: %s, column2: %s"%(column1,column2) db.close()