Installing and using MySQL (on Fedora 15)

I had some trouble getting basic installation of MySQL to work, so here’s a ground-up tutorial.

  1. Install mysql and mysql-server from the repositories. On Fedora, that’s ‘sudo yum install mysql’ and the same with ‘mysql-server’.
  2. To run the MySQL server, you have to run the command ‘/etc/rc.d/init.d/mysqld start’. (You may need to run this as root by prefacing with ‘sudo’.)
  3. The problem I ran into was, it was trying to use the innodb database, which is not installed by default. So you need to edit the file ‘/etc/my.cnf’. Under the section [mysqld], add the lines:
    skip-innodb
    default-storage-engine=myisam
  4. Now run  ‘/etc/rc.d/init.d/mysqld start’ and it should say things are going ok. If not, you’ll have to check the error log file ‘/var/log/mysqld.log’ to see what went wrong.
  5. Now, from a terminal, run ‘mysql’. It should connect to the database you have running.  This should open up in interactive MySQL interpreter where you can enter commands like “SHOW DATABASES;”.

Installing Mercurial on Linux

Update: I’ve updated this post to focus on the steps to take in order to get mercurial working on your system, and some rough guidelines.

To install, use your installation/package manager. In Ubuntu, run:

sudo apt-get install mercurial

Now you need to set permissions. Inside your home directory, ~/, you can set up your “hgrc” (mercurial configuration file) with your username and default email address. Note that you must create and edit this file as the owner of ~/, not as root, because mercurial will only trust files that you created.  So open or create the file ~/.hgrc. Then enter the following lines:

[ui]

username = My Name <example@emailaddress.com>

with your name and email. Now you should be able to check out and use repositories.

Note that if you get errors about untrusted users (for example, mercurial ignores anything from “untrusted user root”, the only fix may be to edit your systems hgrc file to trust those users. The file is called /etc/mercurial/hgrc and you should add these lines to it:

[trusted]

users = root

Or any other users you wish to trust.

Using Mercurial

I won’t cover how to set up a Mercurial repository, because I didn’t — I just used bitbucket. To start out with a repository located at some url, use cd to navigate to the directory you want to use, then use hg clone to get a copy of a project. For example:

cd ~/my-documents/my-projects/
hg clone https://hostofproject.com/my-project-1

Or wherever it is. Now you’re ready to start developing!  If you create a new file or folder, you must add it to the project:

hg add *

Sending and receiving updates is a two-step process. To finalize all the changes and additions you’ve made, you must commit the changes. To send that final version to the repository, you must push it.

hg commit -m 'Made some changes'
hg push

Conversely, to get the newest version from the repository, you must pull it. To change your working copy to incorporate this new version, you must updateit.

hg pull
hg update

That’s the gist of it.

Dual-Boot Windows 7 and Linux (Ubuntu)

For today, a quick (but hopefully thorough) walkthrough on how to dual-boot Windows 7 and a Linux distribution. Whether you’re starting from a clean machine or an existing Windows 7 computer, desktop or laptop, these general instructions should work for you. Note that this is just one way to do it. Also, others may recommend some changes or tweaks to this approach. But it should be useful if you’d like an example of how to set up a dual-boot system. (Disclaimer: I’m not responsible for anything at all, ever. Try this at your own risk. BACK UP YOUR DATA FIRST!)

I’ve done this on a home-built, clean desktop, and on a Lenovo x220 laptop where I kept the existing Windows 7 install. So I will draw on these as examples. Both times, I installed Ubuntu, which is commonly known as a user-friendly distribution of Linux that is good for first-timers. These instructions, however, should work in general.

What you’ll need: A computer, a way to back up your data, some disks/USB drives for installation. You’ll also need to be willing to Google some things if you haven’t done them before — in the interests of brevity, I won’t explain how to click through each menu or every step in detail. If you’re stuck on a part, leave a comment and I’ll try to explain; there are also lots of resources out there on the web! Finally, expect to do a little troubleshooting after installation to get Windows and Linux to work together with your Data partition, etc. It may take a bit of setup work, but should be worth it!

Before we head out onto the field, here’s the general game plan. We’re going to create three main partitions: one with our Windows 7 installation, programs, etc; one with our Linux installation, applications, etc; and one for all of our data. The two operating systems will both be able to access the data partition, so they can both read and write to the same folders and files.

Ready? Let’s go.

Step 1: Back Up Your Data/Computer

If you have any data stored on your computer, you need to back it up first. You should hopefully already have an external hard drive or some other backup method; if not, get one!

You’ll also want a way to reinstall Windows 7 if it’s pre-installed. (You may not end up using it, if you are able to simply “shrink” the Windows 7 install, but you will want it down the road if your computer crashes.) My laptop, for example, had an option of creating backup disks which could be used to return it to its factory state if something went wrong. I used this feature to create a backup on a 16 GB USB drive.

Step 2: Get Install Disks/USB

If you already have Windows 7 installed, you may be able to shrink the install to fit its partition without needing to reinstall Windows. If you want to reinstall Windows or have not yet installed it, you’ll need a Windows install CD or some other bootable device, like a USB drive, that allows you to install Windows 7.

You’ll also need a bootable CD or USB drive for your Linux distribution (I created mine using Ubuntu.com). Ideally, it would come with the application GParted, which we will use to partition our hard drive. If not, you’ll need a bootable drive that does. (Worst-case scenario: create a bootable Ubuntu CD, use it in Step 3 to use GParted, then get rid of it and use your favorite Linux distro when it’s time to install.)

Step 3: Partition

Now we’re going to partition the hard drive. First, let’s figure out how many gigs each partition will need. Remember, all of our data (documents, music, everything) will go in the Data partition. But programs and applications will go in the partitions of the respective hard drives. Modern video games, for example, take up a lot of space.

  • Windows partition. Windows 7 alone is over 20 GB, so 30 GB is probably a bare minimum. I recommend at least 40 GB, possibly much more if you expect to install large programs.
  • Linux partition. The memory used varies by distribution but is very likely under 5 gigs. Add in the programs you’re running and 10 GB is probably a bare minimum, but I’ll recommend at least 20. If you have a large hard drive, it probably can’t hurt to leave extra space on these.
  • Data partition. This will be all the space that you have left.

Now, it’s time to actually make the partitions. First, a quick primer on partitions. Partitions are ways to divide up your hard drive. You can split a hard drive into partitions of two types: primary partitions and extended partitions. A primary partition is a chunk of the hard drive, plain and simple. And extended partition is also a chunk of the hard drive space, but an extended partition must itself be partitioned up into one or more separate chunks. These sub-chunks are called logical partitions.

You may only have up to four total primary/extended partitions on your hard drive. However, you can subdivide an extended partition into a number of logical partitions, enabling you to sidestep this limit.

Now, insert your Linux boot media (CD or USB drive) that has GParted on it. Shut down the computer. We will need to boot from the media. To do so, you may have to go into your BIOS menu and change the boot order. For my laptop, I was able to press F12 on startup, giving me an option of which device I wanted to boot from.

Once it’s booted up, find and run the program GParted. (Note: GParted asks you to select sizes in terms of megabytes, but here we’ll mainly be talking in terms of gigabytes. So be aware of the conversion!)

If You Already Have Windows Installed

Your disk will have a small partition at the very beginning (created by Windows for booting and/or system recovery; we don’t have to worry about it), then the rest of the drive is your Windows installation. (You may also have another partition at the end; my laptop, for instance, has a ~10GB partition at the end of the hard drive containing system recovery tools. If so, leave that one alone as well.)

The large Windows install is the partition we want to shrink. (It is a primary partition.) Click on it and select “Shrink Partition.” Hopefully, you will be able to shrink it to the size you want. If not, you may have too much data or it may be too fragmented. You can try defragmenting the drive and coming back to this step, or you can format this partition, erasing Windows. If you end up formatting the partition, follow the instructions for not having Windows installed. If you did shrink the Windows install successfully, you can skip to the section “Create Linux and Data Partitions”.

If You Don’t Already Have Windows Installed

It’s always nice to start from a clean slate. If there are any partitions on your hard drive, use GParted to delete them. Then create a primary partition at the beginning of the drive for Windows 7. Make it the size you decided on above and format it as ntfs. Then move on to the next step.

Create Linux and Data Partitions

Now, we’ll take the remaining space (located after our Windows partition) and allocate it as one big extended partition; then, we’ll divide it up into logical partitions for Linux and for our data.

In GParted, select the space after the Windows partition and choose to create an extended partition. Now we’re ready to create logical partitions within that big extended partition.

First, we’ll create what’s called the “swap”. Basically, this is space on the hard drive that Linux uses as extra RAM if your RAM is used up. If you don’t know about swap space, I recommend making your swap partition the same size as your RAM (if you have 4 GB of RAM, you can make the swap partition 4 GB). To create it, select the extended partition and create a new logical partition of the appropriate size. Format this partition as “swap” or “linux-swap”.

After that (but still within our extended partition), we’ll create a logical partition to hold our Linux operating system. Do so in the area after the swap space, within the extended partition. Format this as ext4, which is a standard format for Linux.

Use the remaining space in the extended partition to create a logical partition for Data. We’ll format this as ntfs, so that Windows and Linux can both read/write to it. Once that’s done, we’re done creating partitions and ready to install!

Step 4: Install Windows (if not already installed)

If you don’t have Windows installed, we need to install Windows first. Boot from the Windows CD (or USB drive if you have it on that disk image) and do a fresh install. Make sure to install it onto the partition you created specifically for Windows.

Step 5: Install Linux Distro

To install Linux, boot from your Linux installation media. You’ll want to do an advanced install (in Ubuntu, select the “Manually specify partitions” option). You will choose which partitions to use for installation. There are two partitions to worry about in this step. Set your swap area as “swap”. (Duh.) Set your Linux area (located right after your swap) as /. This is where the Linux install will go. For mount point, choose the entire device (probably called “/dev/sda”).

Now you should be able to go ahead with the installation!

Interlude: Working Machine

You should now have a fully functioning dual-boot system! When you start up the computer, your Linux boot manager screen should come up, allowing you to manually select which operating system to boot, or selecting Linux after a few seconds of inactivity. You can boot each OS to make sure they were installed correctly. Then, let’s finish the job with Step 6.

Step 6: Configure Each OS to use Data Partition

We’ll start with Linux, since it can be a bit trickier. What we want is for Linux to automatically mount the Data partition upon startup, and we want to be able to read and write the partition. In Ubuntu, I was able to successfully do so by following instructions from ubuntuguide.org. I will list the steps below; note that your mileage may vary on other versions of Linux.

First, you need to know the name of the Data partition. For example, mine was called /dev/sda7. You can figure it out with GParted, or by opening a Terminal and running “sudo fdisk -l”.

Now, we’ll make a folder to be the mount point for the partition. (“Mounting” the partition just means loading it up so the system can access the partition.) When the partition is mounted, it will appear in this folder. For this step, I created a folder named “Data” in the /media folder, since /media is where drives are usually mounted. To do this from the Terminal, you can run “sudo mkdir /media/Data”.

Now we need to change how the drive is loaded on startup. The file that controls this is called fstab and is located in the /etc folder. First, you should create a backup copy of fstab in case you mess something up. You can do that on the command line by running “sudo cp /etc/fstab /etc/fstab-backup.bak”. This copies fstab into a file named fstab-backup.bak.

To edit fstab, we can open it up in Gedit (a text editor): “sudo gedit /etc/fstab”.

There will be one or two lines describing how to load up your Data partition. For example, if it is called /dev/sda7, you might have a line that looks like:

/dev/sda7  auto nouser,atime,noauto,rw,nodev,noexec,nosuid 0 0

Comment it out by placing a # at the beginning of the line (if there are two, comment both out):

#/dev/sda7  auto nouser,atime,noauto,rw,nodev,noexec,nosuid 0 0

Now, we will add a line that mounts the partition to our chosen folder with read/write permissions. Add the line:

/dev/sda7 /media/Data ntfs-3g quiet,defaults,rw 0 0

where /dev/sda7 is the name of your Data partition and /media/Data is the folder where you’d like it to mount.

Now, reboot your system and boot back into Linux. The Data partition should be mounted and you should be able to read and write to it. To test it out, you can navigate to /media/Data (or wherever your mount point is) and create some folders. I personally put the following folders in my Data space: Documents, Media, Other, Temp, and Trash.

Last thing on Linux. On a normal Linux install, your own personal files are stored in “/home/username/”, where username is, well, your user name. To make things easier, we can make links (shortcuts) from inside this folder to our Data. I created a link to each of the above folders: Documents, Media, Other, Temp, and Trash.

Now, reboot into Windows. Windows should automatically mount the Data partition as a drive (on my system, it is D:\). You should be able to open it and see any folders and files you created on Linux. To finish things off, I recommend making similar shortcuts on Windows: In your C:\Users\username folder, delete the “Documents”-style folders and replace them with shortcuts pointing to D:\Documents and so on. You can also drag these folders into your Favorites and Library bars in Windows Explorer (on the left side) for easy access.

Done!

I hope these instructions were clear and worked well. If not, let me know!

Easily Open a File in Java

Say you wanted to open a file in Java, either for reading or writing. But it’s not clear how to do it, and whether you need a GUI, and blah blah blah, wouldn’t it be nice to just find a minimal example?  Well then, here you go.

import javax.swing.*;   // for JFileChooser
import java.io.*;       // for File
import java.util.*;     // for Scanner (if you want to read it in)

class FileOpener {

    public static void main(String[] args) {
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();
            // it's in your hands now...
        }
    }
}

Solid. Now say you want to read from the file:

            try {
                // read in the file
                Scanner s = new Scanner(f);
                String str = s.nextLine();
                // ...
            } catch(FileNotFoundException e) {
                // Couldn't find the file for some reason
                e.printStackTrace();
            }

Or writing to the file:

            try {
                FileWriter fw = new FileWriter(f);
                fw.write("Hello, World!");
                // ...
            } catch(IOException e) {
                // didn't work
                e.printStackTrace();
            }

Ta-da! Protip: If you’re using this as part of a GUI with a Component “comp” showing your main GUI screen, set “comp” to be the parent of the filechooser dialog by calling fc.showOpenDialog(comp);.

Other protip: if you really want, you can subject yourself to the official tutorial here.

Build Your Own Computer Part 4: Put It Together

Putting together a computer should be easy. In fact, it is easy. So let’s do it.

If you’re interested in the entire process of building your own computer, starting from scratch, then I encourage you to check out Part 1, Part 2, and Part 3. They detail the steps of understanding and choosing your components. But if you’ve got all the components and are ready to install, then it’s time for…

Continue reading

Build Your Own Computer Part 3: Choosing the Parts

So you’re interested in building your own computer. And you’ve already read through Part 1 (The Basics) and Part 2 (Understanding the Options).

Those posts covered these topics: Timestamp (this article is up to date as of July 2011), Am I Qualified (Yes), Reasons to Build It Yourself, List the Parts, and Understand the Parts.

So we know, generally speaking, what’s going into our computer. More than that, we know what our options are at each step and about what they’ll cost us. Now’s the time to start making decisions. Continue reading

Build Your Own Computer Part 2: Understanding the Options

You want to build your own computer, right? Of course you do. By now, I’m certain that you’ve read Part 1: The Basics. If you know what the parts of a computer are, you probably skipped it. The main point I want to recapitulate is that this article was written in March 2011. [UPDATED: current as of July 2011.]  In this business, you’ve got to use recent sources.

Previously, we looked at what components go into building a PC. Now we’re going to go over that list another time, but in detail. Our goal here is to understand the options available to us when it comes time to pick out parts and make a budget.

Continue reading