Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Monday, September 8, 2014

Having frequent WiFi disconnection in your Linux?

I've been lately using Fedora 20 as my development environment and it's really fascinating. It is one of the best OS's for a developer with extensive support for development no matter what technology you are focused into (Of course if it's Windows or MAC specific, then it's not)

A major issue I have been experiencing in Fedora 20 in my laptop (Lenovo T530), is the frequent WiFi disconnection. It was very embarrassing, although the WiFi signal strength is good. At times the WiFi goes off and never gets connected back. The following was few useful commands I came to know that can force the WiFi adapter to initiate the connection (possibly). I hope this may be useful to some of you as well.
[shazni@shazniInWSO2 Disk1]$ nmcli -p con

=============================================================================================================
                                       List of configured connections
=============================================================================================================
NAME                 UUID                                  TYPE             TIMESTAMP-REAL                  
-------------------------------------------------------------------------------------------------------------
TriXNet              bd504b18-be5f-4d2d-9ea6-6bff40f29cca  802-11-wireless  Mon 08 Sep 2014 01:55:20 PM IST 
NAUMI LIORA HOTEL    dc5a4e03-69f0-4f0b-80a4-b5c6f2755ace  802-11-wireless  Thu 21 Aug 2014 10:20:16 PM IST 
TriXNet_Groud_Floor  6142855d-7845-4f6a-88fb-068b15cbd029  802-11-wireless  Thu 14 Aug 2014 11:30:43 PM IST 
em1                  32a2ab29-73af-477a-888c-ab9cf2a489b2  802-3-ethernet   never                           
Dialog 4G            3b96c186-012a-43eb-bdb8-72679f318472  802-11-wireless  Sun 31 Aug 2014 11:01:30 PM IST 
PROLiNK_PRN3001      cb54a35f-e459-44cf-a2b3-32071e77460b  802-11-wireless  Mon 07 Jul 2014 08:57:24 PM IST 
ZTE                  d57d20b1-c556-4706-8849-acafdee1fc88  802-11-wireless  Sat 09 Aug 2014 05:50:02 PM IST 
WSO2                 15da72d9-07f9-47bd-9a0a-f055399a6339  802-11-wireless  Fri 05 Sep 2014 06:20:15 PM IST 
AMB                  0a521533-22f4-4adc-89c3-8f1b9118743e  802-11-wireless  Fri 22 Aug 2014 05:01:37 PM IST 

[shazni@shazniInWSO2 Disk1]$ nmcli con up uuid bd504b18-be5f-4d2d-9ea6-6bff40f29cca
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
The first command shows the available connections together with device UUIDs. The second command initiates the connection given the UUID. This might result in prompting you for a password.

Well I have been doing this for sometime whenever my connection goes off and never re-connects. I found an easy solution ( or rather a workaround ) for frequent disconnection. The workaround was to disable IPv6 in my Linux box. I can't assure the solution would work if you are encountering the same issue. Anyway, that worked for me. Ever since disconnections have been rare.

To disable IPv6 in your machine, do the following.
[shazni@shazniInWSO2 shazni]$ sudo vi /etc/sysctl.conf
3) Add the following line at the end of the file (or where its convenient for you):
net.ipv6.conf.all.disable_ipv6 = 1
Restart your machine for the changes to take effect.

Saturday, February 8, 2014

The history of Unix and Linux

In this post I thought of sharing the history of Unix and Linux in a bullet list for curious people who want to know what has been the history of Unix/Linux in a brief list rather than reading a huge chapter of a classic book.
  • The first UNIX implementation was developed in 1969 (the same year that Linus Torvalds was born) by Ken Thompson at Bell Laboratories, a division of the telephone corporation, AT&T. It was written in assembler for a Digital PDP-7 mini-computer


Ken Thomson

PDP-7 (Which is under reconstruction)

  • In 1970, UNIX was rewritten in assembly language for a newly acquired Digital PDP-11 minicomputer


PDP-11

  • A short time later, Dennis Ritchie, one of Thompson’s colleagues at Bell Laboratories and an early collaborator on UNIX, designed and implemented the C programming language (The language that almost all the modern languages are derived from). C followed an earlier interpreted language, B. B was initially implemented by Thompson and drew many of its ideas from an even earlier programming language named BCPL


Dennis Richie

  • First edition of Unix - By 1971, UNIX was running on the PDP-11 and already had a FORTRAN compiler and versions of many programs still used today, including ar, cat, chmod, chown, cp, dc, ed, find, ln, ls, mail, mkdir, mv, rm, sh, su, and who.
  • Second Edition, June 1972: By this time, UNIX was installed on ten machines within AT&T.
  • Unix implemented in C by Ken Thomson and Dennis Richie in 1973
  • Third Edition, February 1973: This edition included a C compiler and the first implementation of pipes.
  • Fourth Edition, November 1973: This was the first version to be almost totally written in C.
  • Fifth Edition, June 1974: By this time, UNIX was installed on more than 50 systems.
  • Sixth Edition, May 1975: This was the first edition to be widely used outside AT&T.
  • Then, the Unix OS is distributed to Universities with source code.
  • January 1979 saw the release of Seventh Edition of UNIX. This release also contained a number of new tools, including awk, make, sed, tar, uucp, the Bourne shell and a FORTRAN 77 compiler. The release of Seventh Edition is also significant because, from this point, UNIX diverged into two important variants: BSD and System V
  • Bjarne Stroustrup developed a language which is an extension of C language, originally name as "C with Classes"


Bjarne Stroustrup

  • Many new tools and features were developed at Berkeley, including the C shell, the vi editor, an improved file system (the Berkeley Fast File System), sendmail, a Pascal compiler, and virtual memory management on the new Digital VAX architecture.
  • Under the name Berkeley Software Distribution (BSD), this version of UNIX, including its source code, came to be widely distributed. The first full distribution was 3BSD in December 1979. (Earlier releases from Berkeley BSD and 2BSD were distributions of new tools produced at Berkeley, rather than complete UNIX distributions.
  • In 1982, breakup of monopoly between AT&T and the US government on telephone system. This led AT&T to market and sell Unix. This resulted in the release of System III (three) in 1981. System III was produced by AT&T’s UNIX Support Group (USG)
  • In 1983, "C with Classes" was renamed as C++
  • In 1983, the Computer Systems Research Group at the University of California at Berkeley released 4.2BSD. This release was significant because it contained a complete TCP/IP implementation, including the sockets application programming interface (API) and a variety of networking tools. 4.2BSD and its predecessor 4.1BSD became widely distributed within universities around the world. They also formed the basis for SunOS (first released in 1983), the UNIX variant sold by Sun. Other significant BSD releases were 4.3BSD, in 1986, and the final release, 4.4BSD, in 1993.
  • The first release of System V (five) followed in 1983, and a series of releases led to the definitive System V Release 4 (SVR4) in 1989, by which time System V had incorporated many features from BSD, including networking facilities. System V was licensed to a variety of commercial vendors, who used it as the basis of their UNIX implementations.
  • In addition to the various BSD distributions spreading through academia, by the late 1980s, UNIX was available in a range of commercial implementations on various hardware. These implementations included Sun’s SunOS and later Solaris, Digital’s Ultrix and OSF/1 (nowadays, after a series of renaming and acquisitions, HP Tru64 UNIX), IBM’s AIX, Hewlett-Packard’s (HP’s) HP-UX, NeXT’s NeXTStep, A/UX for the Apple Macintosh, and Microsoft and SCO’s XENIX for the Intel x86-32 architecture.
  • In 1985, Richard Stallman founded the Free Software Foundation (FSF), a nonprofit organization to support the GNU project as well as the development of free software in general.
Richard Stallman
  • In 1987, Andrew Tanenbaum, a university professor in Holland created a Unix-like operating system named Minix. This was merely for academic purpose.


Andrew S. Tanenbaum

  • Well-known programs were produced by the GNU project such as Emacs text editor, GCC (originally the GNU C compiler, but now renamed as the GNU compiler collection, comprising compilers for C, C++, and other languages),, the bash shell, and glibc (the GNU C library).
  • POSIX.1 (or more specifically POSIX 1003.1) became an IEEE standard in 1988
  • Release of GNU General Public License (GPL) in 1989
  • FIPS 151-1 was published (Federal Information Processing Standard). This standard required certain optional features of POSIX.1
  • In 1989 (XPG3) X/Open Portability Guide Issue 3 was released. This standard was also based on POSIX.
  • ANSI C standardization led to C89
  • FIPS 151-2 aligned with the 1990 ISO edition of POSIX.1 (FIPS was withdrawn in 2000)
  • By 1990, a complete Unix system was ready; except the most important kernel. But, The GNU project had started work on an ambitious kernel design, known as the GNU/HURD, based on the Mach microkernel. But it was far from complete.
  • POSIX adaptation for ISO standard in 1990 (ISO/IEC 9945-1:1990)
  • Version 2 of the license, released in 1991
  • In 1991, Linus Torvalds, creating the Linux kernel, which is inspired by Minix.


Linus Torvalds

  • XPG4 standard was released in 1992
  • XPG4 version 2 in 1994 - This incorporated some important parts of AT&T’s System V Interface Definition Issue 3
  • By March 1994, the developers were able to release version Linux 1.0. Linux 1.2 appeared in March 1995, Linux 2.0 in June 1996, Linux 2.2 in January 1999, and Linux 2.4 in January 2001. Work on the 2.5 development kernel began in November 2001, and led to the release of Linux 2.6 in December 2003.
  • In 1995 Single UNIX Specification (SUSv1) was formulated by combining XPG4 version 2, X/Open Curses Issue 4 version 2 specification, and the X/Open Networking Services (XNS) Issue 4 specification. This was also called UNIX 95 standard.
  • In 1996, an Organization name 'The Open Group" was formed by merging X/Open and the Open Software Foundation (OSF). Nearly all key companies in the Unix domain are members of this organization
  • SUSv2 in 1997 (This is also occasionally referred to as UNIX 98 and XPG5)
  • The first C++ standard was ratified in 1998 and is known as ISO/IEC 14882:1998
  • Second C language standard led to C99
  • In 2001, POSIX 1003.1-2001 (Also known as SUSv3) in 2001. This then became an ISO standard namely ISO/IEC 9945:2002
  • C++ standard was amended with a Technical Corrigendum in 2003 an is known as ISO/IEC 14882:2003
  • SUSv4 and POSIX.1-2008 in 2008
  • The third C standard was published in 2011. This standard is know as C11
  • The next C++ standard was ratified in 2011. This standard is known as C++11

Friday, November 15, 2013

Installing and configuring phpMyAdmin in Linux

phpMyAdmin is a web based graphical administration tool for MySQL RDBMS, written in PHP. Using phpMyAdmin can ease your database administration tasks. Therefore, installing it in your machine is useful and this short guide will walk you through setting it up in your Linux. Installing this in Windows is just a matter of installing the WAMP server installer package which has everything, including MySQL server packaged into one bundle.

Even, in Linux you may choose to use a package called LAMP, which is easy to install and have everything bundled together. But for certain reason, may be you want to install MySQL separately in a remote server or you have MySQL already installed in your distribution, you would need to install phpMyAdmin separately as described below.

The phpMyAdmin needs a MySQL database already installed. If you haven't got it installed, see my previous post on 'Installing MySQL in Linux'.

Assuming you have installed MySQL, do the following to install phpMyAdmin, whichever is applicable to your distribution
$ sudo apt-get install phpMyAdmin
$ sudo yum install phpMyAdmin
During the installation, you will be prompted for the web server to serve the phpMyAdmin. Select 'Apache2' for that. You may also be asked to provide administration credentials, which you can provide on your wish and note it down as it's required while you login. You can also use an existing MySQL user credentials to login to phpMyAdmin.

The installation might start the apache web server and copy the phpmyadmin.conf into /etc/apache2/conf.d directory. In certain distribution like Fedora, the apache2 directory is named httpd. Replace the directory appropriately. You can check it by listing the directory content of /etc/apache2/conf.d. If you don't find it, you can copy it manually as follows and can reload the apache web server.
$ sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
$ sudo /etc/init.d/apache2 reload
or in RHEL/Fedora
$ sudo service httpd restart
Note: As per documentations, after Ubuntu 13.10 (Saucy Salamander), apache will load configuration files from /etc/apache2/conf-enabled/ directory instead of from /etc/apache2/conf.d/. Therefore if you are using Ubuntu 13.10 or later, do the above step for /etc/apache2/conf-enabled/ and reload the web server.

Ok that's it. Now you may start administering your MySQL database with phpMyAdmin. Open your favorite browser and navigate to http://<hostname>/phpmyadmin. If your apache web server is in local machine, replace <hostname> with localhost. Provide the username and the password that you configured in the installation or you may use an existing MySQL user credential. Then you would see the following phpMyAdmin page for administration. Good luck in using MySQL.


Monday, November 11, 2013

Enabling core dumps in Linux


It is usable to set the Linux systems to write core dumps when an application process crashes. It isn't a magic that badly written C/C++ application would silently crash and vanish. In such a case, to debug what has happened, we would like to view the stack trace of the application to the point of crash which may possibly give us a clue that what did go wrong.

If not in all Linux distributions, certain Linux distributions do not write the core dumps to the file system by default as it is set off. It's usually done to prevent applications to write huge core dumps of hundreds of GB, which would eat up valuable disk space. Although, enabling core dumps in production systems is not recommended. it would be beneficial for developers to have it enabled in the development machines.

The question is how do we turn it on. OK.. it's quite simple.

Before that we shall check the current core dump limits. Enter the following command.
$ ulimit -c
0
You might possibly get the value as zero. This means. the system doesn't write any core files when your application crashes. Let's test this with a sample program which is set to crash intentionally. Type in the following in a file names Crash.cpp.
#include <iostream>

using namespace std;

class Crash
{
public:
 Crash() {
     cout << "Constructing " << endl ;
     p = new int ;
 }
 ~Crash() {
     cout << "Destructing" << endl ;
     delete p ;
 }
private:
 int *p ;
};

int main()
{
 Crash *pCrash = new Crash ;
 delete pCrash ;
 delete pCrash ;
 return 0;
}

According to the program, dynamically allocated memory pointer pCrash is reallocated twice, which make the program crash at the second reallocation. Compile the program by entering the following.
$ g++ -o crash Crash.cpp
Now let's execute the program as below. You would see that the program crashed by looking at the terminal output of the stack trace. But the core file would be missing at the execution directory or in the configured directory (see below), since ulimit is 0.
 [shazni@wso2-ThinkPad-T530 Crash]$ ./crash
Constructing
Destructing
Destructing
*** Error in `./crash': free(): invalid pointer: 0x0000000000ddb020 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7fa5bd0bba46]
./crash[0x400b2f]
./crash[0x400a3a]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fa5bd05cea5]
./crash[0x400929]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:04 14552625                           /home/shazni/ProjectFiles/Test/NormalTest/Crash/crash
00600000-00601000 r--p 00000000 08:04 14552625                           /home/shazni/ProjectFiles/Test/NormalTest/Crash/crash
00601000-00602000 rw-p 00001000 08:04 14552625                           /home/shazni/ProjectFiles/Test/NormalTest/Crash/crash
00ddb000-00dfc000 rw-p 00000000 00:00 0                                  [heap]
7fa5bcd36000-7fa5bce39000 r-xp 00000000 08:03 4341456                    /lib/x86_64-linux-gnu/libm-2.17.so
7fa5bce39000-7fa5bd039000 ---p 00103000 08:03 4341456                    /lib/x86_64-linux-gnu/libm-2.17.so
7fa5bd039000-7fa5bd03a000 r--p 00103000 08:03 4341456                    /lib/x86_64-linux-gnu/libm-2.17.so
7fa5bd03a000-7fa5bd03b000 rw-p 00104000 08:03 4341456                    /lib/x86_64-linux-gnu/libm-2.17.so
7fa5bd03b000-7fa5bd1fa000 r-xp 00000000 08:03 4341459                    /lib/x86_64-linux-gnu/libc-2.17.so
7fa5bd1fa000-7fa5bd3f9000 ---p 001bf000 08:03 4341459                    /lib/x86_64-linux-gnu/libc-2.17.so
7fa5bd3f9000-7fa5bd3fd000 r--p 001be000 08:03 4341459                    /lib/x86_64-linux-gnu/libc-2.17.so
7fa5bd3fd000-7fa5bd3ff000 rw-p 001c2000 08:03 4341459                    /lib/x86_64-linux-gnu/libc-2.17.so
7fa5bd3ff000-7fa5bd404000 rw-p 00000000 00:00 0
7fa5bd404000-7fa5bd418000 r-xp 00000000 08:03 4329143                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa5bd418000-7fa5bd618000 ---p 00014000 08:03 4329143                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa5bd618000-7fa5bd619000 r--p 00014000 08:03 4329143                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa5bd619000-7fa5bd61a000 rw-p 00015000 08:03 4329143                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fa5bd61a000-7fa5bd6ff000 r-xp 00000000 08:03 2891772                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa5bd6ff000-7fa5bd8fe000 ---p 000e5000 08:03 2891772                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa5bd8fe000-7fa5bd906000 r--p 000e4000 08:03 2891772                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa5bd906000-7fa5bd908000 rw-p 000ec000 08:03 2891772                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa5bd908000-7fa5bd91d000 rw-p 00000000 00:00 0
7fa5bd91d000-7fa5bd940000 r-xp 00000000 08:03 4330760                    /lib/x86_64-linux-gnu/ld-2.17.so
7fa5bdb18000-7fa5bdb1d000 rw-p 00000000 00:00 0
7fa5bdb3b000-7fa5bdb3f000 rw-p 00000000 00:00 0
7fa5bdb3f000-7fa5bdb40000 r--p 00022000 08:03 4330760                    /lib/x86_64-linux-gnu/ld-2.17.so
7fa5bdb40000-7fa5bdb42000 rw-p 00023000 08:03 4330760                    /lib/x86_64-linux-gnu/ld-2.17.so
7fffcccd9000-7fffcccfa000 rw-p 00000000 00:00 0                          [stack]
7fffccd66000-7fffccd68000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted
If you see a value instead, that is the maximum size that's permitted to dump. If the dump is less than that size you would get the full dump. Otherwise the full dump may not be written, which would be useless. the core files may not be truncated to exact limit of ulimit. It may be different, since ulimit that we set is a soft limit.

To set a limit to the ulimit, issue the following command
$ ulimit -c 45  # sets the limit to 45 times 512 bytes of block
To set the limit to an unlimited amount, issue the following command
$ ulimit -c unlimited
Now, execute the above program. You would now see the same output, but there will be an additional file named 'core' in the current directory (If you still don't find, read ahead), which is the kernel dump of the process. Ok, have we done yet? You would have probably guessed 'No', because you see lot of stuff written below in the post. Good guess!!!
The problem here is that, the setting that you made is only temporary to the current terminal session. If you open up another terminal and check the ulimit, it would still be the initial ulimit (probably zero) that you saw earlier. We need our settings to be persistent throughout and between reboots. OK... I get profile files into my head now. Either .bashrc in home directory, if you want the setting to effect only the current user or /etc/profile if you need the settings to be effected to all users. I'll use my .bashrc for this.
$ vi ~/.bashrc
Add the following line to the end of your profile.
ulimit -c unlimited >/dev/null 2>&1
This set the ulimit to unlimited and throws whatever is output to screen to hell. You may need to source the .bashrc file as the below command and may need to close the terminal and start all over again. Now in all terminals you open up as your user account, ulimit should be shown as unlimited.
$ source ~/.bashrc
Ok, now even if you reboot the system and run the above program, you should get a core file created (If, you still don't find read ahead). Well, we can be little more fancy as well. We can have a pattern of our core file name. Currently, if you see the core dump is named 'core' unless someone has changed the pattern in the file /proc/sys/kernel/core_pattern. This is where you specify where the core dump is written and it's file name pattern. If you didn't see the core dump earlier in the directory where the program executable exist, look the path in this file. In my Ubuntu, it as 'core' as shown below.
$ cat /proc/sys/kernel/core_pattern
core
We can have certain information embedded in the core file name itself. You may want to edit this file directly or you can edit /etc/sysctl.conf. I'll edit the configuration file since I can add more system parameters if needed.
$ sudo vi /etc/sysctl.conf
And add the following to the end of the file
#core file settings
kernel.core_uses_pid=1
kernel.core_pattern=core.%e.%s.%p.%t`
fs.suid_dumpable=2
The first line will enable addition of the process id to the core file name.
The more interesting line is the second line. This line is the core file pattern. Each letter following a % sign as a special meaning. Some of the possible values and its meanings are as follows.
Unix/linux systems provide a facility to run a process under a different user id or group id than the user/group starting the process by changing the setuid and setgid bits. Such processes may be dealing with sensitive data which should not be accessible by the new invoking user/group. Since core dumps may provide some internal sensitive data, Unix/Linux systems by default disable core dumps to be written when such users execute the process. If we still want to enable core dumps to be written when such users execute the process, we need to set the third line.

%e - Application name
%s - Signal number that caused the crash
%p - Process ID of the application when it crashed
%t - time at which dump is written (This is in seconds from Unix epoch)
%u - real UID (User ID) of the dumped process
%g - real GID (Group ID) of the sumped process
%h - hostname
%% - will have a % sign itself

To make setting effective, enter the following command.
$ sudo sysctl -p
Now if you run the above application with above pattern, you would get something similar core file name as, core.crash.6.18056.1384147041

OK. Going a little further now. We can permit the core files to be written by all the applications in your system, including daemons. In Red-hat based distributions like RHEL or Fedora, this can be achieved by adding the following line in /etc/sysconfig/init
DAEMON_COREFILE_LIMIT='unlimited'
You may need to restart the system for this setting to take effect.

Instead if you just want to allow only certain daemons to write dumps, in Red-hat based distributions, edit add the following line /etc/init.d/functions unless it's already there
# make sure it doesn't core dump anywhere unless it's requested
corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
Now add the following line to the init script of your daemon in /etc/init.d/{your service}
DAEMON_COREFILE_LIMIT='unlimited'
Since is a RedHat way of setting core file limit, in Ubuntu you need to add the following lines instead to the services' init script in /etc/init.d
ulimit -c unlimited >/dev/null 2>&1
echo tmp/core.%e.%s.%p.%t > /proc/sys/kernel/core_pattern
If you now start your service by invoking following command
$ sudo service {your service script} start
Now find you services process id using
$ ps aux | grep {your service}
And let's kill it
$ sudo kill -s SIGSEGV {Your services' PID}
Now if go ahead and look in /tmp. you should find a core dump for your service.

Great!!! Here we are. now we have set our Linux box to write core dumps to the system and can debug any application crashes.

Thursday, October 17, 2013

Installing MySQL in Linux

MySQL is a very powerful open source relational database management system (RDBMS). In this short guide we'll get into installing MySQL on Linux.

You can check whether MySQL is already installed by issuing the following command.
$ mysql -V
You should see something similar output as follows if you have MySQL installed.
mysql  Ver 14.14 Distrib 5.5.32, for debian-linux-gnu (x86_64) using readline 6.2
If not, to install MySQL, run the following command from a terminal prompt in apt-get package manager based Linux distributions
$ sudo apt-get install mysql-server
In yum package manager based Linux distributions, issue the following command
$ sudo yum install mysql-server
You will be prompted to enter a password for the MySQL root user in the installation process. It's recommended to use a password for a root account. Now the MySQL server is installed on your Linux machine.

Alternatively, you may download the relevant MySQL package (e.g. rpm package) for your Linux from http://dev.mysql.com/downloads/mysql/#downloads and install according to your distribution. You may need to sign up for an Oracle Web account to download from this site. This article won't go into details of installing with distribution specific packages.

MySQL installation could also be done with the source. You can also download the source distribution from the above URL. Installing from source needs certain tools and libraries pre-installed (such as cmake and ncurses-devel). Other workaround may be required to build from source.

To start the MySQL database, issue the following command.
$ sudo /etc/init.d/mysql start
or in Fedora/RHEL
$ sudo systemctl start mysqld.service
To check whether MySQL is already running, you can issue the following command.
$ mysqladmin -u root -p status
You will be prompted for the password. When entered, you should see somewhat similar output as follows if MySQL is running
Uptime: 28581  Threads: 1  Questions: 111  Slow queries: 0  Opens: 343  Flush tables: 1  Open         tables: 84  Queries per second avg: 0.003
If not running, you would see an output as shown below.
   mysqladmin: connect to server at 'localhost' failed
   error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
   Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
You can edit the settings for the MySQL instance in /etc/mysql/my.cnf file to configure the basic settings like log file and port number.

That's it. You now should have MySQL installed on your system and ready to use it to develop database driven applications.

Saturday, October 5, 2013

Installing Developer Tools in Linux

The first step in developing applications in Linux based computers is mostly to have a setup of your developer tools to be installed properly. This short guide aims to provide you with installation steps required to get started with some of the popular programming languages to develop fascinating software.

This post will provide steps for installing the required tools and provide a classic "Hello World" example to test your installation of the tools. Before attempting to install any of the tools, make sure whether your system already contains the tools. Chances are high, that you, most probably have it installed already.

  • C

To compile a C program in Linux based systems, the most popular tool used is the gcc compiler. This is contained in the GNU Compiler Collection (GCC). To check whether gcc is already installed enter the following command
$ gcc --version
if you see an output like the following, gcc is already installed and you are all set to write C programs.
gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Instead if you see the following or something similar, you will have to install gcc.
bash: gcc: command not found...
Though gcc can be installed separately it's recommended to install a package called 'build-essential', which contains bunch of other useful and related packages. To install it issue the following commands in Debian based Linux.
$ sudo apt-get update
This will update the package list. Now issue the following command in Debian based Linux like Ubuntu.
$ sudo apt-get install build-essential
In Linux systems such as Red Hat Linux or Fedora that has yum based package manager, you can use the following command,
$ sudo yum install gcc
This will install gcc compiler and other development libraries like glibc. You can also use the following command to install whole bunch of development packages which also includes gcc and g++ (for C++)
$ sudo yum groupinstall "Development tools"
Now you can write a sample C program as below to check that everything is working fine. Create a file named HelloWorld.c and type in the following program text.
    #include <stdio.h>

    int main()
    {
        printf("Hello World!\n");
        return 0 ; 
    }
Compile the above program with the following command.
$ gcc -o HelloWorld HelloWorld.c
This will create an executable file named HelloWorld in your current directory. To execute the executable program type ;
$ ./HelloWorld
You should see the following output.
Hello World!
Congratulations!! You are a C guru now.

  • C++

The most popular C++ compiler in Linux is undoubtedly g++. This is also part of the GCC. If you have installed the build-essentials in the Debian or the "Development Tools" in Red hat Linux, you must already have g++. Otherwise, go ahead and install build-essentials or "Development Tools". To check whether g++ is already installed, enter the following command.
$ g++ --version
if you see an output like the following, g++ is already installed and you are all set to write C++ programs.
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Instead if you see the following or something similar, you will have to install g++ by installing build-essential package or the "Development Tools".
bash: g++: command not found...
If you didn't install "Development Tools", and want to install g++ separately using yum, use the following command,
$ sudo yum install gcc-c++ 

This should install g++ and other development files and libraries.

Let's now write a sample C++ program which prints Hello World! on the standard output. Type the following program in a file named HelloWorld.cpp
    
#include <iostream>     
using namespace std ;     

int main()     
{         
    cout << "Hello World!" << endl ;         
    return 0 ;      
} 
Compile the above program with the following command
$ g++ -o HelloWorld HelloWorld.cpp
This will create an executable file named HelloWorld in your current directory. To execute the executable program, type ;
$ ./HelloWorld
You should see the following output.
Hello World!
Quite similar to what we did for C, right? Voila!!! Now you are a C++ expert too.

  • Java

Next we shall look into setting up Java in Linux. Chances are that you already got Java installed. Most probably the OpenJDK version. If you are willing to work with OpenJDK, everything is fine, and you are ready to develop java applications. But you may have a valid reason to use Oracle java for developments. In that case we can remove OpenJDK and install Oracle Java or keep OpenJDK with Oracle Java. Though there are few ways to install and confugre Java in Linux, the best and easy way is to download the JDK zipped file from http://www.oracle.com/technetwork/java/javase/downloads/index.html. Make sure to download the version you want, which is applicable to your system architecture. Once you have downloaded the zip file, extract it to a location of your interest. Let's say the extracted location is /usr/local/java/jdk1.6.0_43. All you need to do is add the JAVA_HOME (your installation directory; e.g: /usr/local/java/jdk1.6.0_43) as a new environment variable and add the bin directory of JAVA_HOME to the PATH environment variable. This can easily be done by adding the following lines to your .bashrc file in your home directory.
JAVA_HOME=/usr/local/java/jdk1.6.0_43
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin     
export JAVA_HOME     
export PATH

If you add these lines to .bashrc, the changes will be effective only to the current user. If changes need to be applied to all the users, the same set of lines can be added to /etc/profile file. Once you added the line, source the respective file to make changes immediate on the same shell. To do that, issue the following command.
$ source ~/.bashrc
Now java is configured on your Linux box. Issue the following commands to verify your installation and environment variables. Expected outputs should be something similar to the following lines.
$ echo $JAVA_HOME      
/usr/local/java/jdk1.6.0_43     

$ echo $PATH      
/usr/local/java/jdk1.6.0_43    

$ java -version
If you see a similar output as follows you are all set to develop java applications
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
Let's write a simple java program, compile and execute it. Create a file named HelloWorld.java and type in the following text.
public class HelloWorld     
{         
 public static void main(String[] args)         
 {               
  System.out.println("Hello World!");         
 }       
}
Make sure the class name match your file name. To compile this program, type in the following.
$ javac HelloWorld.java
This must generate a class file in your current directory named HelloWorld.class. It contains the bytecode of your program. To execute your class file type
$ java HelloWorld
Make sure you do not type in the .class extension when executing. Following output should be shown in the standard output.
Hello World!
Congratulations!! you have written a simple java program. Ok.. Have we done with everything? Ideally yes. But wait a second. We often come across new java versions. Then the question comes, whether we can have two versions of java installed in the Linux side by side? The answer, as you may have guessed, is a big "YES". So how do we go about making that. Ok.. The process is simple. Download the zip file of your interested version of java. And extract it, say to the same location (e.g. /usr/local/java/jdk1.7.0_07). Next we got to inform the Linux that you have two versions of java. Let's collect a few more details.
$ java -version
This should still print the older java we configured earlier, like the following.
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
To find the path of the java that we currently use, issue the following command
$ which java
If you have only the java we set up earlier and followed the instructions above, you would get a similar output as follows.
/usr/local/java/jdk1.6.0_43/bin/java
Ok.. let's make the Linux aware about the two jdk versions we extracted to /usr/local/java. To do that we issue the following commands.
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.7.0_07/bin/java 2
ok... now the Linux knows about the new java, but has set the priority to 2. Issue the following command.
$ java -version
This would print the new java version.
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)     
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
    
Ooops!!. Where the hell my old java went? Ok don't panic. What has happened here is that you have created a symbolic link to /usr/local/java/jdk1.7.0_07/bin/java from /etc/alternatives/java and another symbolic link to /etc/alternatives/java to /usr/bin/java. When you issue the above java command, the system path /usr/bin/java overrides the path we earlier set. You can check this by issuing the following commands.
$ ls -l /usr/bin/java     
lrwxrwxrwx 1 root root 22 Oct  5 11:14 /usr/bin/java -> /etc/alternatives/java     

$ ls -l /etc/alternatives/java     
lrwxrwxrwx 1 root root 36 Oct  5 11:14 /etc/alternatives/java -> /usr/local/java/jdk1.7.0_07/bin/java
We shall now do the same to the old java we had. To do that, issue the following command
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.6.0_43/bin/java 1

If you issue the following command, we should still see the jdk7 is used.
$ java -version     

java version "1.7.0_07"     
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)     
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

Ok. We shall change to the earlier version by issuing following command and selecting 1.
$ sudo update-alternatives --config java

You will be prompted as shown below.
Selection    Path                                  Priority   Status     
------------------------------------------------------------     
* 0          /usr/local/java/jdk1.7.0_07/bin/java   2         auto mode       
  1          /usr/local/java/jdk1.6.0_43/bin/java   1         manual mode       
  2          /usr/local/java/jdk1.7.0_07/bin/java   2         manual mode     

Press enter to keep the current choice[*], or type selection number: 1
      
By selecting 1, you can switch back to the earlier version. To verify, again issue the following.
$ java -version     

java version "1.6.0_43"     
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)     
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
  
Further you have to do the same thing for javac and javaws commands for both the versions like the following.
$ sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.7.0_07/bin/javac 2     
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.7.0_07/bin/javaws 2     
$ sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.6.0_43/bin/javac 1     
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.6.0_43/bin/javaws 1
    
Now you can select your preferred javac version as below.
$ sudo update-alternatives --config javac

and select the appropriate number. That's it. We can now have as many java versions as we need in our system and switch back and forth easily to experiment with.

  • Python

Most probably python is installed on your Linux. You can check for python version by entering following command
$ python -V     

Python 2.7.4
If python is available you should see a similar output as above. If not install python with the following command, whichever is applicable,    $
sudo apt-get install python        or   
$ sudo yum install python
Ok.. Now let's start writing a sample program. Create a file called HelloWorld.py in your current directory. Enter the following text in it.
#!/usr/bin/python     
print("Hello World!")
Save the file and execute the code using python interpreter as follows
$ python HelloWorld.py     

Hello World!
Great.. Now you know how to program in Python too.

  • Perl

Most probably perl is also installed on your Linux. You can check for perl version by entering following command
$ perl -v
You should see a somewhat similar output as follows.
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi
(with 80 registered patches, see perl -V for more detail)

Copyright 1987-2011, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
    
If not, install perl using the following command, whichever applicable
$ sudo apt-get install perl        or     
$ sudo yum install perl
Done.. Now let's start writing a sample program. Create a file called HelloWorld.pl in your current directory. Enter the following text in it.
#!/usr/bin/perl     
print "Hello World!\n";
Save the file and execute the code using python interpreter as follows
$ perl HelloWorld.pl   
  
Hello World!
That's great... Now you are a perl expert as well.

  • Ruby

To check whether ruby is installed on you Linux, enter the following command.
$ ruby -v
If you have ruby installed, you should see somewhat similar output as below.    
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
If you can't see an output like this, you will have to install ruby by issuing the following command, whichever is applicable
$ sudo apt-get install ruby            or      
$ sudo yum install ruby
Ok. Let's write the same HelloWorld program in ruby. Create a file called HelloWorld.rb and enter the following.
#!/usr/bin/ruby      
puts "Hello World!"
Now you can execute the program by entering following.
$ ruby HelloWorld.rb

Hello World!

  • C#

We finally have come to our last section. C# is a Microsoft developed programming language. Hence you should typically be programming in Windows using the .Net framework. But if you still want to develop C# or any .Net application in Linux, you are not to be made worry. You can install the 'mono', which is an open source implementation of the Microsoft .Net framework. To install mono on your Linux type in the following commands. whichever is applicable.
$ sudo apt-get install mono-complete    or     
$ sudo yum install mono-core.x86_64
When this is done, you are ready to develop .Net applications in Linux. To see the mono version installed, type in the following.
$ mono -V
you should see something similar to the following.
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5ubuntu1)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
 TLS:           __thread
 SIGSEGV:       altstack
 Notifications: epoll
 Architecture:  amd64
 Disabled:      none
 Misc:          softdebug 
 LLVM:          supported, not enabled.
 GC:            Included Boehm (with typed GC and Parallel Mark)
Let's write a small C# program. Create a file called HelloWorld.cs and type in the following.
using System;     

public class HelloWorld     
{         
 static void Main()         
 {               
  Console.WriteLine("Hello World!");        
 }       
}
We can compile the program by issuing the following command.
$ gmcs HelloWorld.cs
This should create a file called HelloWorld.exe in you current directory. (Note : In Microsoft Visual C# in Windows, we use 'csc' command in the command prompt to compile a C# program). To execute the executable we can type in the following.
$ mono HelloWorld.exe     

Hello World!
Voila!!! Now we can develop any .Net application in Linux as well. Here we are. This post explained you to set up development tools (or rather to see whether it's installed) in your Linux box and walked you in creating sample programs to get started. I hope the article has been easy to follow and fun to read. Flourish in the development world!! Good luck.