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.
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:
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;
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:
And your <hoststring> node should look something like this:
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:
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:
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.
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.
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)?
@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.
Ok, very cool.
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.
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.
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!