Pragmatism in the real world

Counting the rows in a SQL Group By query

I recently needed count the number of rows in an SQL query that had a Group By clause. It looked something like this: SELECT account_name FROM events WHERE created_at >= CURDATE() – INTERVAL 3 MONTH GROUP BY account_id This provides a list of account names (28 in my case), but if you try to count them using: SELECT COUNT(account_name) as c FROM events WHERE created_at >= CURDATE() – INTERVAL 3 MONTH GROUP BY account_id You… continue reading.

Turn off foreign key checks when restoring a mysql file dump

I recently received a MySQL dump file where the various tables in it had foreign keys to each other. I usually restore with this command: mysql –login-path=rob < dump.sql but this generated the error: ERROR 1217 (23000) at line 288805: Cannot delete or update a parent row: a foreign key constraint fails It turns out that the easiest way to solve this is to use the –init-command switch to set foreign keys off for this… continue reading.

Using Docker to create a MySQL server

When working on test code on my computer, I usually use the built-in PHP server (php -S) which works nicely. Every so often, I need access to MySQL and I use Docker to temporarily create a MySQL server for me. This is how I do it. The magic command is: $ docker run –name mysql \ -e MYSQL_USER=rob -e MYSQL_PASSWORD=123456 -e MYSQL_DATABASE=bookshelf \ -p 3306:3306 -d mysql/mysql-server:5.7 This creates a Docker container called "mysql" on… continue reading.

Using PostgreSQL with PHP in Cloud Foundry

Having successfully deployed a PHP application to Cloud Foundry, I needed a PostgreSQL database for persistent storage. I found Lorna Mitchell's Connecting PHP to MySQL on Bluemix helpful and this article expands on that information. I want to create a cloud-based PostgreSQL database and connect it to Laravel's Eloquent in a Cloud Foundry application. This is how to do it. Create the database instance As I'm using Bluemix for my Cloud Foundry hosting, I'm using… continue reading.

Converting databases between MySQL and SQL Server

I regularly deal projects that target SQL Server, but mostly develop against MySQL to avoid having to run a full Windows stack locally all the time. One of the nice things about PHP with DBAL and Migrations is that the database is pretty well abstracted from my code. Of course, this means that I don't target any of the specialist features, but for these projects, this hasn't been an issue. To convert data from SQL… continue reading.

Password less command line scripts with MySQL 5.6

I have a number of command line scripts that copy MySQL databases down from staging servers and store them locally. These scripts set the password on the command line using the -p option. With MySQL 5.6, a new warning is displayed every time my scripts run: Warning: Using a password on the command line interface can be insecure. This is annoying! The way to solve this is to use the new command line tool mysql_config_editor… continue reading.

Uninstalling MySQL on Mac OS X Leopard

To uninstall MySQL and completely remove it (including all databases) from your Mac do the following: Use mysqldump to backup your databases to text files! Stop the database server sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems/MySQLCOM sudo rm -rf /Library/PreferencePanes/My* edit /etc/hostconfig and remove the line MYSQLCOM=-YES- rm -rf ~/Library/PreferencePanes/My* sudo rm -rf /Library/Receipts/mysql* sudo rm -rf /Library/Receipts/MySQL* sudo rm -rf /private/var/db/receipts/*mysql* The last three lines are particularly important as otherwise,… continue reading.

Notes on using Oracle with my tutorial

William Graham has been playing with the Zend Framework and Oracle using my tutorial. His notes are very useful if you want to get my admittedly MySQL-centric tutorial working with Oracle. I've come across that uppercase field name thing before in an application I wrote at work that needed to transfer some data from Oracle to SQL Server. Took me a while to work out what was going on. I'm a little confused about the… continue reading.