Zephyr Dev

A place for problems

Exporting a MySQL database via the command line

No Comments »

Problem

You want to dump data from a database into a single file via the command line.

Solution

You could use the export tools in phpMyAdmin, but if that is not an option or you want a clean dump, then you will use the commandline.

Prerequisite:

You must be able to log into your server and find location of MySQL. I prefer a shell connection, but there are other ways. Here is instruction on accessing this stuff.

mysqldump -u [username] -p[password] [dbname] [tables] > /tmp/filename.sql

Below is the script example to backup mysql database in command line:-

mysqldump -h localhost -u username -p database_name > backup_db.sql

If your mysql database is very big, you might want to compress your sql file.
Just use the mysql backup command below and pipe the output to gzip,
then you will get the output as gzip file.

mysqldump -u username -h localhost -p database_name | gzip -9 > backup_db.sql.gz

If you want to extract the .gz file, use the command below:-

gunzip backup_db.sql.gz

Exporting all databases from server

mysqldump -u wildlife -p -A > big_database.sql

You may have to identify the location of mysql to run these commands. For example, I’ve added the is a sample

/hosts/willywonka/mysql/bin/mysqldump -u dbusername -h localhost -p databaseName 
| gzip -9 > backup_file_jan2012.sql.gz

By the way, make sure the above command is entered all on one line. If you miss the option (everything after the vertical bar) you will just get a mysql dump on your screen!

Preface to learning Java web technologies

No Comments »

For the next little while, I will be posting about Java development for the web. There will be several different conversations, but my ultimate goal is to write a tutorial on everything I have learned in the last four months. This should include Java and JSP basics, Struts 2, connecting to a database (JDBC, JNDI, Hibernate), building a project with Maven (or ANT WTF?), and a whole lot of other ridiculous things.

Herding pronghorn

The reason I am writing this is because everything currently written on the web—every tutorial and example—is complete trash. I learned PHP, Python, CSS, MySQL, MySQL stored procedures, Oracle SQL (and advanced topics), Javascript and Google Maps Javascript API all from free web tutorials. My knowledge in these topics is not superficial either; PHP 5+ has many OOP features and should not be discounted as a crappy scripting language. Yet, trying to learn Java and all its frameworks is as easy as building a Turing machine out of toothpicks, blindfolded, and on the moon. It is also as fun as herding cats with border collies and kicking walls with toothpicks under your toenails. Yeah, I’m pissed. Needless to say, it is hard learning Java web technologies.

The problem is several fold:

  1. There are many different ways to use Java (robotics, appliances, web, etc.)This makes it awesome because the world is your playground! However, it also means there are many, many different libraries/frameworks and ways of doing the same thing. The simplest things on the web seem to have many very different approaches.
  2. Java is used and dominated by CS nerds. Nothing wrong with knowing your craft. Consequently though, the web system and programmers seem to be overly consumed with technical details and logistics. This seems to stifle creativity. The programming community generally don’t know how to dumb-down their explanations and tutorials which makes learning an already difficult system even more difficult. Even frameworks, which are supposed to simplify things, have high learning curves. For example, Struts 2 is awesome, but it took me a long while before I could even figure out how to make a “hello world” work properly. Take a look at this Struts request flow diagram. It makes sense to me now, but it unnecessarily took a lot of time to understand.
  3. Object-oriented to the extreme
  4. The community lacks freshness. I’ll be following along with one tutorial and will come across a diagram with a Netscape logo. I then have to question the whole thing because I’m not sure if it is still relevant. Maybe there are newer technologies?

Ultimately what I mean to say is there is so much potential with Java, but it is not accessible by the general web developer. Part of this is due to the nature of being an advanced and full-featured language, but the other part is due to a lack of easy-to-follow documentation. I seek to remedy this with my own tutorials. These will get you up and running quickly. Even if you miss something, you can always update later…but at least you’ve started.

Wish me luck!

P.S. A shoutout to the Head First series on Java development. Your books are actually fun and easy to follow. If only you had one on the web technologies I am trying to learn (Struts, Hibernate, Spring, Maven)!

How to uncompress a .WAR file from the command line (UNIX)

1 Comment »

Problem:

You want to un-archive (unpackage) a WAR file to reveal the contained files and directory structure.

Solution:

Use the command line to unarchive the file. It is very simple and will only take a few moments. I originally used Eclipse to import the WAR file. It would expand the file out, but it seemed to add directories and/or move things around to fit its folder structure. I wanted to see the folder structure as it was packaged in the WAR file, this solution does that. Follow these steps:

  1. Create a folder to store the contents of the WAR file (I am using temp)
  2. Place the WAR file in this folder
  3. Locate this folder via the command line (see basic directory navigation help if you do not know how to do this). Example:
    cd Desktop/temp
  4. Use JAR to open the file up:
    jar -xvf struts2-blank.war
  5. Check that it worked:
    ls
    # My results:
    META-INF		error.jsp		index.html
    WEB-INF			example			struts2-blank.war

How to add Tomcat to MAMP

No Comments »

Problem:

You want to utilize Java (to run JSP, JavaServer Pages) on your local web server

Solution:

If you are a Java/JSP developer, then most likely you are running an Integrated Development Environment (IDE). Examples are NetBeans and Eclipse. These have their own application servers for running code. I’m pretty sure they have Tomcat installed since they seem to run JSP pages just fine. So, you could just run your code with the IDE application server and you would be fine.

If you still want to use MAMP then I will show you how to install Tomcat. It is actually really simple:

  1. Download the latest version of Tomcat
  2. Unzip/extract the file to /Applications/MAMP/
  3. For the sake of simplicity and operational smoothness, name the directory “tomcat” So, this folder hierarchy will look like /Applications/MAMP/tomcat
  4. Open the file /Applcations/MAMP/bin/start.sh with any text editor (I use TextMate).
  5. Add the line:
    /Applications/MAMP/tomcat/bin/startup.sh

    Save this file

  6. Open the file /Applcations/MAMP/bin/stop.sh with any text editor
  7. Add the line:
    /Applications/MAMP/tomcat/bin/shutdown.sh

    Save this file

Close these files and restart MAMP servers. Call up the local server (“http://localhost/”) and make sure everything is running. If you have troubles, try adding the startup and shutdown lines to /Applcations/MAMP/bin/startApache.sh and /Applcations/MAMP/bin/stopApache.sh respectively. Let me know if you run into problems.

Google maps API v3 not showing up in Internet Explorer 7 or 6

6 Comments »

Problem:

Embedding Google maps into a webpage (using Google Maps Javascript API version 3) works in Firefox, Chrome, Safari, IE 8 and 9, but does not work in IE 6 and 7. Basically, nothing shows up in place of the map. Here are two examples:

Example (not working):

IE 7 screenshot google maps version 3 error

Example (correct):

Screenshot of Google maps Javascript API working properly

Solution:

The solution for Internet Explorer 6 is to disregard this browser. IE6 is no longer supported by Google Maps. To add insult to injury, it isn’t even supported by Microsoft…so, get with the times. Even IE7 is a bit old.

There are many possible errors in Javascript that could account for the missing Google map. However, my issue was with trailing commas. IE7 (and sometime IE8) does not like trailing commas because it expects an identifier—hence the warning: “expected identifier.”

Here is a sample of the API with the problem commas:

<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(39.8165, -109.549293);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
  }
</script>

Developers may leave these commas in with no recourse from other browsers. This doesn’t fly in IE7, so remove the extra comma. If you need a way to do this in bulk, you could try regex. The corrected version looks like:

<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(39.8165, -109.549293);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
  }
</script>

MySQL error: SQL state S1009 java.sql.SQLException: Value ”0000-00-00” cannot be represented as java.sql.Date

3 Comments »

Error:


SQL state S1009 java.sql.SQLException: Value ''0000-00-00'' cannot be represented as java.sql.Date

I received this error while using NetBeans SQL browser. I searched high and wide for the answer, but could not fix the problem. The problem resides in the Java app rejecting a blank value for timestamp. The solution involves setting a parameter that tells Java to interpret as NULL. People kept suggesting the same
solution, but I couldn’t get it to work at first. So, here is the definitive solution for those using NetBeans.

Solution:

The parameter zeroDateTimeBehavior will need to be set to “convertToNull”

  1. Open NetBeans
  2. Open Services tab or (control-5/command-5)
  3. Right-click the database you are having troubles with. IMPORTANT: make sure to click disconnect. Once it has been disconnected, then click “properties.”
  4. click the “…” button to the right of the field for Database URL. Add
    ?zeroDateTimeBehavior=convertToNull
    to the URL.

    Image of the error message

    Joomla is the name of the database.

  5. Now connect again and it should work.
  6. Let me know if you run into any troubles

How to add multiple options/flags to PHP filter_var()

1 Comment »

First, I must say that I am a fan of filter_var(). It makes sense that, if you are building a dynamic application, you would require user input. Anywhere there is user input and database retrieval, there will be a chance of injection. Thus, all good code properly sanitizes user-derived variables. Sanitization was previously achieved by hand-built functions. If you were sanitizing an email address or something else specific, you might use regex to validate. This was all fine and dandy, but seemed odd that there was no solution native to PHP.

Well, as of PHP 5.2, there is! The solution came in the way of PHP variable filters. There are two main types: sanitizing filters and validating filters (see http://www.php.net/manual/en/filter.filters.php). Each filter has a set of useful options. This brings us to the point of this post— to show you how to use multiple options on a filter:

<?php
$ugly_var = "¶¢•§¢§ Hello Hello =x' or 1 =1";
echo filter_var($ugly_var ,FILTER_SANITIZE_STRING, 
FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
?>

RESULT:
Hello Hello =x' or 1 =1
This applied both the FILTER_FLAG_STRIP_LOW and FILTER_FLAG_STRIP_HIGH flags. Simple enough!

Read more about this at PHP->filter_var

How to make files/folders visible on Mac OS X using terminal (command line)

No Comments »
  1. open terminal
  2. enter command: defaults write com.apple.finder AppleShowAllFiles -bool true
  3. enter command: killall Finder

You can now open a new finder window and you will see files with a dot in front of the name

Thanks to http://ss64.com/osx/ for this.

Importing a MySQL backup into a database via command line

No Comments »

I kept running into problems when trying to import a dump file into my database. Here is how I solved it:

  1. Log into MySQL via command line
  2. Enter:
    • USE whateverdatabase
  3. Enter:
    • SOURCE backupfile.sql

Simple enough!
Since my fields were huge, I had to adjust max_allowed_packet. I had to first log in with the max_allowed_packet change (see post) and then run these lines.