Installing MySQL Python Module on Ubuntu

After tearing through several other Django books and tutorials using sqlite3 as the database, I’m starting to go through the book Beginning Django E-Commerce and it uses MySQL as the database. I use MySQL quite a lot so that side of things isn’t an issue, but I did run into a couple of wrinkles getting MySQL and Django playing nice so I thought I’d share.

Basically if after configuring your database in settings.py and running python manage.py dbshell you get a bunch of errors, you have a minor amount of installation work to do to get things rolling.

First thing I did was install pip, which is a better version of/replacement for easy_install:
sudo easy_install pip
Next I ran pip upgrade for good measure (probably not necessary but can’t hurt, and worth running if you already had pip installed):
sudo pip install pip –upgrade
On my machine (Ubuntu 12.04 64-bit) I also had to build the dependencies for the python-mysqldb libraries:
sudo apt-get build-dep python-mysqldb
And finally with that in place you can use pip to install the Python MySQL libraries:
sudo pip install MySQL-python
If everything worked you should now be able to run python manage.py dbshell from your Django project and have it load up the MySQL console.

Setting MySQL to Allow Multiple Statements In a Single Query In JDBC/OpenBD

This came up in a discussion on the OpenBD Google Group today so I figured it was worth a quick blog post.

Depending on what you’re doing with MySQL you may be in a situation where you need to run multiple SQL statements separated by a ; as part of a single query. By default this isn’t enabled in the JDBC connection with MySQL, but it’s easy to enable in your JDBC connection string.

When creating a MySQL datasource in the OpenBD admin, click on the “Advanced Settings” button, and in the “Connection String” box input the following text:
allowMultiQueries=true

If you’re creating the datasource for the first time this will be all you need to do, but if you’re adding this to an existing datasource you’ll want to bounce OpenBD to make sure the connection pool is cleared out.

To test that the setting change worked, simply write a CFQUERY that contains two statements:
<cfquery name="foo" datasource="foo">
SELECT * FROM foo;
SELECT * FROM bar;
</cfquery>

If that doesn’t throw an error, then you’re all set to run multiple statements in the same CFQUERY tag.
Note that you can enable this setting in bluedragon.xml as well, just make sure your <connectstring> node for your datasource looks like this:
<connectstring>allowMultiQueries=true</connectstring>

And your <hoststring> node should look something like this:
<hoststring>jdbc:mysql://server:port/database?cacheResultSetMetaData=false&amp;autoReconnect=true&amp;allowMultiQueries=true</hoststring>

Setting the Initial Root Password for MySQL on CentOS

I was setting up a new CentOS server tonight and installed MySQL via yum. If you’re familiar with installing MySQL via apt-get on Debian-based systems, you’ll know that during the install it prompts you to set the MySQL root password. Not so on CentOS.

When I’ve installed MySQL on CentOS in the past, I could have sworn the default root password was blank, and that right after the installation is complete you set it like so:

mysqladmin -u root password NEW_PASSWORD

Either my memory is foggy, or something’s changed with how this is done. No matter what I tried I kept getting “access denied” messages from MySQL and of course since I never set a root password, I have no idea what it wants from me.

Luckily there’s a solution, at least one that worked for me, though I still feel like I may be missing something so although this worked, I’m happy to be told there’s a simpler way.

First, make sure no MySQL processes are running:

killall -9 mysqld

Next, start MySQL in safe mode and have it skip grant tables:

/usr/bin/mysqld_safe --skip-grant-tables &

This will let you get in as root without a password. After the process starts, log in and use the mysql database:

mysql -u root mysql

Next, set the root user password:

update user set password=PASSWORD('new_password') where user='root';

It should say 2 or 3 rows affected. Finally, flush the privileges:

flush privileges;

Quit MySQL, and then you’ll want to kill the process you started with mysqld_safe and start the regular MySQL process. You can either bring the process you started earlier by typing ‘fg’, hitting enter, and then hitting ctrl-C to kill it, or you can do a ps -wef | grep mysqld to find the process ID and kill it as per usual.

Finally, restart MySQL and test your new root password:

/etc/init.d/mysqld start
mysql -u root -p

When it prompts you for the password, enter the password you set earlier and you should be logged in successfully.

Note this process also works if you’ve forgotten the MySQL root password.

Hope that save someone else a bit of time, and as I said earlier, I’ll be very happy if someone has a better explanation of why I wasn’t able to log in without a password on a fresh MySQL install on CentOS.

Writing Query Results to a File in MySQL

Blogging this mostly for my own reference, but I had to write query results to a file in MySQL today and hadn’t ever had to do that before. Luckily it’s darn simple. The following will write out a CSV with field data enclosed with quotation marks (which is important if the data itself may contain a comma).

SELECT whatever FROM whereever
INTO OUTFILE ‘/path/to/file.csv’
FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘n’

You can then open that up in your favorite spreadsheet program quite easily.

Comments

@Matt,

That’s awesome. I have never seen that before. Just one question – I assume the path there is relative to the machine housing the MySQL server, not the ColdFusion server (unless they are the same machine of course)?

Posted by Ben Nadel @ 6/3/08 6:38 AM

@Ben–yes, the path there is a server path, and I assume it would be relative to the MySQL executable, or to be safe it’s probably best to give it a full path (which is what the example really is since this is on a Unix server). Also I was running all this within MySQL directly so it didn’t involve CF, but if you executed this from CF the path would still be on the database server, not the CF server if they’re on different physical boxes.

Posted by Matt Woodward @ 6/3/08 7:08 AM

Ok, very cool.

Posted by Ben Nadel @ 6/3/08 7:09 AM

One more thing–the user MySQL is running as has to have write permissions on the directory to which you want to write the file. If you get an “errcode 13” that’s a file permission error meaning that the user MySQL is running as (either “nobody” or “mysql” is pretty common on Unix) doesn’t have permissions to the directory you’re trying to write to.

Posted by Matt Woodward @ 6/3/08 7:10 AM

That’s cool. I checked out to see how to accomplish this is T-SQL for MSSQL, and it looks like you can do it in MSSQL as well, although it isn’t nearly as clean as that MySQL syntax.

Posted by Joshua Curtiss @ 6/3/08 9:25 AM

Matt, This is indeed very helpful. I’m wondering if there’s a more direct way to create a table loaded with the results from the query? I’ve been scouring around the web for this, but haven’t found anyone with this particular need – perhaps it’s just me!

Posted by Maggie Cam @ 6/27/08 12:12 PM