Category Archive: MySQL

Subcategories: No categories

Posts about MySQL

Database Abstraction v0.8

When we began developing C# web applications, we found ourselves in the position of determining what the best way of accessing the database is.  We evaluated several technologies…

  • NHibernate – May be very good, but it was overkill for what we were trying to do.
  • LINQ to SQL – This brings C#’s LINQ (Language-Integrated Query) to SQL databases.  You create database-aware classes and use LINQ to select from collections, which LINQ to SQL converts to database access.  This is a good abstraction, but it relies on SQL Server; as we typically deploy to PostgreSQL, this didn’t work.  (We also couldn’t get DBLinq, a database-agnostic implementation, to work.)
  • ADO.NET – This is the tried-and-true database access methodology, released as part of the initial release of the .NET framework.  The downside to this is that it encourages SQL in the code at the point of data retrieval; it does not provide a clean separation of data access from data processing.
  • EF Code First – This didn’t exist; it’s also very SQL Server-centric.  Not faulting Microsoft for that, especially since they release a free version now; but, as we deploy on Linux, until they release a Linux version, SQL Server is not an option.

With our PHP applications, we had written a database service that read queries from XML files.  Then, queries were accessed by name, with parameters passed via arrays.  The one thing that ADO.NET has that was useful was the fact that it is based on interfaces.  This means that if we wrote something that exposed, manipulated, and depended on IDataConnection (instead of SqlConnection, the SQL Server implementation of that interface), we could support any implementation of database.  The SqlDataReader implements IDataReader as well.  Our solution was becoming apparent.

Over time, we developed what is now the Database Abstraction project hosted on CodePlex.  On Thursday, we released the first public release (although the DLLs are in the repository, and are usually current at every commit).  If you are looking for a way to separate your data access from the rest of your code, or want a solution that’s database-agnostic, check it out.  It supports SQL Server, MySQL, PostgreSQL, SQLite, and ODBC connections *, using the data provider name to derive the proper connection to implement.  There is also a Mock implementation to support unit tests; this mock can provide data, providing a useful way to test methods.  Finally, there is a membership and role provider based on Database Abstraction; simply configure the connection string, create the database tables, and away you go! **

A pre-released version is already in production use in our PrayerTracker application, and others are being built around it.  If this sounds like something that could help your project, certainly feel free to check it out!

* Oracle is omitted from this list, as their DLL had redistribution restrictions; this meant that the source code repository, upon check-out, would have build errors.  There may be an Oracle implementation in the future (it would be trivial), but there is not one now.

** The membership and role providers are untested; they will be tested and tweaked by version 0.9.

Tagged , , , , ,

Oracle SQL Developer Debian Package

Oracle SQL Developer is a Java-based tool that provides a graphical interface to a database. While it’s main focus is Oracle (of course), it can be hooked up, via JDBC, to many other databases, such as MySQL, PostgreSQL, and SQL Server. It’s similar to Toad, but is provided by Oracle at no cost.

Oracle provides SQL Developer in either an RPM, or a generic binary install. I like the ability to manage packages, but I’ve never had much luck at getting RPM to run on Ubuntu. I downloaded the RPM file, and, using alien, I converted the package to a .deb package (Debian package format) and installed it. It worked like a charm!

I haven’t tested it with gcj, but using Sun’s Java 6 update 7 from the Ubuntu repositories, it ran just fine. After you install the package, do a directory list on /usr/lib/jvm. You’re looking for the Sun JDK – if it’s installed, you’ll have a symlink java-6-sun that points to java-6-sun-1.6.0.07. Once you’ve determined the location of the JDK, run “sqldeveloper” from the command line – the program will prompt you for the path to your JDK. Enter it (probably “/usr/lib/jvm/java-6-sun”) and you’re good to go. (You have to install the package as root – but, for the rest of these steps, use your normal user, not root, as this puts settings in a .sqldeveloper directory off your home directory.) The package installs an icon in the “Programming” or “Development” group. Once you’ve told it where the JDK is, you can use this to launch it.

Download SQL Developer 1.5.1 Debian Package

Tagged , , , , ,

A Handy PHP Backup Script

I found a script over on the Lunarpages Forums about using PHP to back up your site. I have taken it, modified it a little, beefed up the documentation a lot, and am now posting it here. You can download the source code for it, and it is also displayed below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
 * Generic Backup Script.
 * 
 * To configure this script for your purposes, just edit the parameters below.
 * Once you have the parameters set properly, when the script executes, it will
 * create an archive file, gzip it, and e-mail it to the address specified.  It
 * can be executed through cron with the command
 * 
 * php -q [name of script]
 * 
 * You are free to use this, modify it, copy it, etc.  However, neither DJS
 * Consulting nor Daniel J. Summers assume any responsibility for good or bad
 * things that happen when modifications of this script are run.
 * 
 * @author Daniel J. Summers &lt;daniel@djs-consulting.com&gt;
 */
 
// --- SCRIPT PARAMETERS ---
 
/*  -- File Name --
	This is the name of the file that you're backing up, and should contain no
	slashes.  For example, if you're backing up a database, this might look
	something like...
$sFilename = "backup-my_database_name-" . date("Y-m-d") . ".sql"; */
$sFilename = "backup-[whatever-it-is]-" . date("Y-m-d") . ".[extension]";
 
/*  -- E-mail Address --
	This is the e-mail address to which the message will be sent. */
$sEmailAddress = "[your e-mail address]";
 
/*  -- E-mail Subject --
	This is the subject that will be on the e-mail you receive. */
$sEmailSubject = "[something meaningful]";
 
/*  -- E-mail Message --
	This is the text of the message that will be sent. */
$sMessage = "Compressed database backup file $sFilename.gz attached.";
 
/*  -- Backup Command --
	This is the command that does the work.
 
	A note on the database commands - your setup likely requires a password
	for these commands, and they each allow you to pass a password on the
	command line.  However, this is very insecure, as anyone who runs "ps" can
	see your password!  For MySQL, you can create a ~/.my.cnf file - it is
	detailed at http://dev.mysql.com/doc/refman/4.1/en/password-security.html .
	For PostgreSQL, the file is ~/.pgpass, and it is detailed at
	http://www.postgresql.org/docs/8.0/interactive/libpq-pgpass.html .  Both of
	these files should be chmod-ded to 600, so that they can only be viewed by
	you, the creator.
 
	That being said, some common commands are...
 
- Backing Up a MySQL Database
$sBackupCommand = "mysqldump -u [user_name] [db_name] &gt; $sFilename";
 
- Backing Up a PostgreSQL Database
$sBackupCommand = "pg_dump [db_name] -h localhost -U [user_name] -d -O &gt; $sFilename";
 
- Backing Up a set of files (tar and gzip)
$sBackupCommand = "tar cvf $sFilename [directory]/*";
 
Whatever command you use, this script appends .gz to the filename after the command is executed.  */
$sBackupCommand = "[a backup command]";
 
// --- END OF SCRIPT PARAMETERS ---
//
// Edit below at your own risk.  :)
 
// Do the backup.
$sResult = passthru($sBackupCommand . "; gzip $sFilename");
$sFilename .= ".gz";
 
// Create the message.
$sMessage = "Compressed database backup file $sFilename attached.";
$sMimeBoundary = "&lt;&lt;&lt;:" . md5(time());
$sData = chunk_split(base64_encode(implode("", file($sFilename))));
 
$sHeaders = "From: $sEmailAddress\r\n"
		. "MIME-Version: 1.0\r\n"
		. "Content-type: multipart/mixed;\r\n"
		. " boundary=\"$sMimeBoundary\"\r\n";
 
$sContent = "This is a multi-part message in MIME format.\r\n\r\n"
		. "--$sMimeBoundary\r\n"
		. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
		. "Content-Transfer-Encoding: 7bit\r\n\r\n"
		. $sMessage."\r\n"
		. "--$sMimeBoundary\r\n"
		. "Content-Disposition: attachment;\r\n"
		. "Content-Type: Application/Octet-Stream; name=\"$sFilename\"\r\n"
		. "Content-Transfer-Encoding: base64\r\n\r\n"
		. $sData."\r\n"
		. "--$sMimeBoundary\r\n";
 
// Send the message.
mail($sEmailAddress, $sEmailSubject, $sContent, $sHeaders);
 
// Delete the file - we don't need it any more.
unlink($sFilename);
Tagged , , ,

Apache and MySQL Are Back

I was finally able to resolve my problems with Apache and MySQL. When I decided to mount my FAT32 drive under /home/summersd, I inadvertently caused myself some problems. From talking to a Linux guy at work, I found that no processes that weren’t running under my user ID could access those files. The reason is that Linux looks up the entire diretory tree, back to /, to determine if you can access the file. So, although I had “-rwxrwxrwx summersd summersd” on every file, /home/summersd was “drwx—— summersd summersd”, and /home was “drwxr-xr-x”. The permissions on /home/summersd was keeping Apache from seeing /home/summersd/drive_d/wwwroot, and MySQL from seeing or writing to /home/summersd/drive_d/mysql/data. I moved the drive to /mnt/drive_d, with the mount point being owned by “root”, still mounting the drive with my user name, and everything worked.

In the process of reconfiguring Thunderbird, I believe I may have found out how to share the address book across operating systems. The file ~/.thunderbird/default.[something]/prefs.js has a listing of all the preferences and settings. I modified this file to change the location of my mail files, and there is a setting there for an address book (which isn’t shown in the configuration dialog – after all, it is 0.7.3…) I’ll play with that later – right now I’m just elated to have Apache and MySQL working again.

Success with Wine & Diagnostics

At work, we use an editor called Visual SlickEdit (VSlick). It’s got a lot of features, and supports color-coding for many different languages. I decided that I’d give wine another shot, as we only have the Windows version of this program. I installed wine and winesetuptk, used winesetuptk to configure the installation, then ran the installation program. Everything installed, and the program ran up to a point, when it started complaining about a missing DLL. I booted to WXP, found the DLL, copied it to the FAT32 drive, rebooted to Linux, and copied the DLL into the “fake windows” system directory. Soon, it was working great! I can’t believe it – success with wine!

I also have made little headway towards getting Apache and MySQL to working. I changed the process that Apache uses to run as “summersd”, and I was able to see pages (although any pages that relied on a database didn’t work). I still haven’t figured this one out yet…

I’m still getting kernel panics from time to time, and it seems to be whenever I access networking. A suggestion from one of the folks on the WBEL users list was to download the Ultimate Boot CD, filled with diagnostic programs. I downloaded it, burned it, and ran some memory checks. Those checked out, so I’m going to run a “CPU Burn-In” program to see if it can detect errors from the CPU. It runs for up to 7 days, but I think I’ll just run it overnight – folding@home didn’t take nearly that long to crash it before.

Back to WBEL

Today, I reinstalled WBEL 3.0.  I was able to compile ndiswrapper (as I kept that on my FAT32 drive), and get the network card working smoothly very quickly.  (In fact, it seems to be more reliable under Linux than WXP!)  With the network up, it was easy to download Firefox, Thunderbird, and OpenOffice, and installing them was a breeze.  (I decided to put them under /opt this time, trying to stick with the FHS.)  I decided to mount my FAT32 drive under my home directory, as /home/summersd/drive_d.  E-mail works fine, but Apache gives me a 403 (Permission Denied) error.  MySQL doesn’t seem to be working either – I’ll have to play with that later.

A Month in Summary

Well, the last month has been interesting. I was able to get my Windows and Linux installations synchronized by creating a mount point for my second drive under /mnt/drive_d . Under that, I created a directory called /thunderbird for my e-mail, and moved my e-mail and newsgroup folders over there. (The first time, I missed the “newsrc” file, which is important – it tells what newsgroups you’ve subscribed to and which messages you’ve read.) Under Windows, I pointed it to “D:\thunderbird\pop3.knology.net”, and under Linux, it was configured to “/mnt/drive_d/thunderbird/pop3.knology.net”. I then moved the “wwwroot” directory from “C:\Inetpub” to drive D:, and pointed IIS to the new location. Under Linux, I did something a little different. As “root”, I deleted the directory /var/www/html, and instead created /var/www/html as a symbolic link to /mnt/drive_d/wwwroot (the actual command is “ln -s /mnt/drive_d/wwwroot /var/www/html”). That worked great as well.

MySQL was more complicated, but I was eventually able to get it working as well. I created the directory “D:\mysql\data” for the data, then configured /etc/my.cnf under Linux to look at “/mnt/drive_d/mysql/data”. I kept getting “Could not connect to server using socket /var/lib/mysql/mysql.sock”. After some digging, it appeared to be a permissions problem. All the documentation said that the default socket was /tmp/mysql.sock, so I changed my.cnf to point there instead, restarted mysqld, and it worked! So, I have no idea what a Unix socket it, but I know that now I have one! :)

I was also able to get DVDs playing using xine, compiling it myself, and using libdvdcss, I can even watch commercial DVDs. I’m really impressed with xine – it handles all kinds of media out of the box, including DivX and up to version 8 of WMV files. You can add codecs to it as well, to support almost anything you want to do from an audio or video perspective. Compiling the player took around 20 minutes, and compiling the front end took another 5. And, it was simple – download the .tar.gz file, do “tar xvfz [name].tar.gz”, “cd [name]“, “./configure”, “make install”. The “./configure” script is the key in the whole process – it looks at what you have installed, and creates make files that will work with your compiler.

Everything started going south, though, when I started having freezes. Eventually, I got to where I could not boot without a kernel panic, and then boot errors (which I detailed in this e-mail to the WBEL user’s list. Encouraged by my success over the past month, I decided to return to WBEL – it’s supposed to be more stable than FC2, and I bet that I can get ndiswrapper, the dual-booting web server, the common e-mail, and maybe even some other stuff working again.