Tuesday, April 2, 2013

Installing MySQL on raspberry pi

Hello Lemmings!
Here's a small tutorial on how to install mysql on Raspberry PI

First upgrade and update your system by running the following command (it can take a while and download large ammounts of data)

sudo apt-get install update upgrade

then install mysql

sudo apt-get install mysql-server

during the installation you will be asked for root password (from now on reffered as [ROOTPWD]).


After this simple installation your MySQL instance will be running on your Raspberry PI. THe instance is already running and will start automatically on your raspberry after restart.

In case you want to restart it on the fly type:
/etc/init.d/mysql stop

and then to start it again

/etc/init.d/mysql start

Here are some tweeks:
1.  Prepare your mysql instance for production
Run following command to remove all testing databases and users which are installed by default:

mysql_secure_installation

in the upcoming dialogs you can set your root password, whether you can connect from outside to the mysql and so on. The same configuration can be also done in following configuration file

/etc/mysql/my.cnf

2. Create new schema
 
run mysql and login as root:
mysql -u root -p

enter [ROOTPWD] when required

when logged in successfully your currsor will looke like this
mysql>_

now create the new schema by following command
create database [DB_NAME];

 3. grant privileges to connect to your database from different machine

you will need an IP address of this machine to grant the privileges.
In windows you can try to rund command line (Widows+R, then type cmd and press enter)
In command line window enter following command:
ipconfig -all

search for entry like
IPv4 address . . . . . . . . . . : 192.168.1.[x](Preffered)

from now on let's reffer to this IP address as [REMOTE_IP_ADDRESS]

then go back to rasberry pi command line, run mysql and login as root as in step [2]
then exectute following command:

grant all on [DB_NAME].* to root@'[REMOTE_IP_ADDRESS]' identified by '[ROOTPWD]';

now you can try to launch your MySQL workbench and connect to raspberry pi from your win machine




Friday, February 15, 2013

Raspberry PI - hostname settings

Default hostname in raspbian
The default hostname set in raspbian is "raspberrypi". You can find and modify it in following file:

\etc\hostname


Out of the box, Raspberry PI is not recognized from windows when pinging via it's default hostname and PI is not able to ping windows machines.

Access Raspberry PI from windows
In this case, it is due to the fact that samba is not installed by default in raspbian.
To install samba type:

sudo apt-get install samba

you can try to ping PI from windows to check if it worked:

ping raspberrypi


Access windows machines from Raspberry PI
In this case installing winbind will do the trick:
To install winbind type:

sudo apt-get install winbind

then modify file /etc/samba/smb.conf


change:
workgroup = HOMENETWORK

modify file /etc/nsswitch.conf

change the line
"hosts: files dns"
to
"hosts: files dns wins"

after that you can try to ping your windows machine from raspberry terminal to check if everything worked fine:

ping [winmachine_host_name]

after that, you can access your machines by host name

My Raspberry PI arrived

Hello everyone!

Today my raspberry PI  Model B arrived!
I'm excited to see what this small machine can do!

What do i plan to do with it?

Perform an initial setup
Create as simple SVN server connected to a FTP as a repository.
Publish a simple page with some lightweight server.
Try RASPBMC as a media player

So checkout my blog in upcoming weeks to see what's the progress.

Friday, September 14, 2012

Cool java enums

In this tutorial  I'm going to show you how cool the enums in java are.

So let's start wit standard java enum:



package com.examples.coolEnums;

public enum EnumExample {
     ENUM1,
     ENUM2,
     ENUM3        
}


now let's say you need this enum to be represented as integer in some transport protocol, since java does not provide standard mechanism to assign integer values for enums (as in C# ENUM1=1, ...).
Many developers therefore fall back to using constants, something like this:

package com.examples.coolEnums;

public class Constants {
  public static final int CONSTANT_1 = 1;
  public static final int CONSTANT_2 = 2;
  public static final int CONSTANT_3 = 3;

}

this is in general one of the possibilities, but you loose the advantage of type safety, compiler can't ensure in build time that the value assigned to some int variable is present in constants.
Let's take a different approach and spice things a little.
The way to do this is to define a constructor for the enum:


package com.examples.coolEnums;

public enum EnumExample {
     ENUM1(1),
     ENUM2(2),
     ENUM3(3);
     
     private int index;
     EnumExample(int intValue)
     {
       this.index = intValue;
     }
     
     public int intValue()
     {
       return this.index;
     }
}



this way, every enumeration has it's index value. You can access it by the intValue() method.

But what about the transformation of int to enum? Let's add a static method to EnumExample class:


public static EnumExample valueOf(int intCode)
  throws InvalidParameterException
{
  for(EnumExample code: EnumExample.values())
  {
    if (code.intValue() == intCode) {
            return code;
        }
  }

  throw new InvalidParameterException("No such code defined!");
}


you can extend the enum any way you want in a similar fashion. You could add compare methods, utils and many others.

The benefit of this approach is also that you still have the type safety ensured!