Thursday, August 30, 2012

Install MySQL and reset root password

Install MySQL 

Using yum to install MySQL on CentOS is very easy:
yum install mysql mysql-server mysql-devel
To use mysql with PHP, you should install :
yum install php-mysql
Make mysql as a service and start when you boot your system:
# chkconfig mysqld on
# service mysqld start

To reset your root password
1. Stop mysql using command:
# sudo service mysqld stop
2. Start up the mysql daemon and skip the grant tables which store the passwords.
# mysql safe --skip-grant-tables
3. Connect to mysql without a password
# mysql --user=root mysql
4. Reset root password
# update user set Password=PASSWORD('new_password') where user='root';
# flush privileges;
# exit;
Now you can login mysql using password.

Wednesday, August 29, 2012

Backup and restore MySQL

Backup and restore MySQL 

backup:
mysqldump -uroot -pXXXXXX db_name > db_name.mysql

restore:
mysql  -uroot -pXXXXXX db_name < db_name.mysql

Configure Redis Master-Slave Relationship

Configure Redis Master-Slave Relationship

Before configure master slave relationship, make sure your redis version on both servers are  the same, the first time I tried to install M-S relationship, redis on my server1 is version 2.2.12 and on my server2 is version 2.4.4, and it failed , it failed because of 
(1)
"Can't handle RDB format version 2"
(2)
[19171] 05 May 18:05:50 * SLAVE OF 127.0.0.1:6379 enabled (user request)
[19171] 05 May 18:05:51 * Connecting to MASTER...
[19171] 05 May 18:05:51 * MASTER <-> SLAVE sync started: SYNC sent
[19171] 05 May 18:05:51 - Accepted 127.0.0.1:45202
[19171] 05 May 18:05:51 # MASTER aborted replication with an error: ERR Can't SYNC while not connected with my master


so in order to make life easier, please make sure redis version is >= version 2.4.4(because it works on my servers)

You can find Redis Installation instruction on my previous tutorial, we will skip it this time.


1. Locate your redis configuration file.
Redis configuration file is located in "/etc/redis/redis.conf", but the location of redis.conf is depend on how you specify it during redis installation.

2. In Slave server, add 3 lines to redis.conf
slaveof 6379
masterauth password_you_want 
requirepass same_password

3. In Master host, add 2 lines to redis.conf
masterauth password_you_want
requirepass same_password

4. Restart redis servers for both Master and Slave 
# /sbin/service redis-server restart

Now, master-slave should work and the databases is synchronized.

To remove the m-s relationship comment out the above modified lines and restart redis on both server.

Redis Installation

Redis Installation
Redis is an integral part of SMG Blackbird architecture. Here's how to install Redis:
Redis is a high performance key-value store that can be easily integrated with a variety of applications. Redis can handle many inserts (sets) by keeping newly added values in memory and writing them to disk over time. To install Redis on CentOS 5.6, run the following commands as root.
Installation
# sudo su
# yum install make gcc wget telnet
# wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz
# tar -xf redis-2.4.4.tar.gz
# cd redis-2.4.4
# make && make install


We're going to change the default redis.conf file to daemonize it, change the location of the database, change the log notices to a production acceptable level, and change the logfile location.
# mkdir /etc/redis /var/lib/redis
# sed -e "s/^daemonize no$/daemonize yes/" -e "s/^dir \.\//dir \/var\/lib\/redis\//" -e "s/^loglevel debug$/loglevel notice/" -e "s/^logfile stdout$/logfile \/var\/log\/redis.log/" redis.conf > /etc/redis/redis.conf
To make management easy, we're going to use an init script that I found here on [1]. The install location on this script doesn't match where the /usr/local/bin/redis-server location we're using, so I'm using sed to update it.
# wget https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
sed -i "s/usr\/local\/sbin\/redis/usr\/local\/bin\/redis/" redis-server
# chmod u+x redis-server
# mv redis-server /etc/init.d
# /sbin/chkconfig --add redis-server
# /sbin/chkconfig --level 345 redis-server on
# /sbin/service redis-server start


Tuesday, August 28, 2012

User is not in the sudoers file. This incident will be reported

User is not in the sudoers file.  This incident will be reported

Sometimes we use sudo command and see the above ERROR.

It means that the user as whom we have logged in and are trying to run the command "sudo" does not have the permission to do so.
Only the users listed in /etc/sudoers have the permission to use the command "sudo".
To give the sudo permission to a user we need to add the user to the file /etc/sudoers file.
Open the file /etc/sudoers as root.
# sudo vim /etc/sudoers
And then add the following line into the sudoers file:
# username ALL = (ALL) ALL
Save and quit:
# wq!
Now, sudo should work properly.

Wednesday, August 22, 2012

Build Two-node cluster with Heartbeat

I was asked to build two-node cluster with heartbeat on two remote servers.
I don't event know what is cluster,heartbeat before I install it on remote servers. I am very new to Linux.
Here's the problems I encountered when I try to install, this article is first to solve those 4 problems, and secondly to show all steps to setup two-node cluster with heartbeat[Second part is not finished yet]
1. What is cluster and heartbeat, why we use it?
2.  How to setup IP Aliasing(virtual IP)?
3.  How to install EPEL Repo on CentOS5.X or CentOS6.X?
Now, I will solve those problems one by one.
1. What is cluster and heartbeat, why we use it?
This problems is quit easy. 
A computer cluster consists of a set of loosely connected computers that work together so that in many respects they can be viewed as a single system.
The components of a cluster are usually connected to each other through fast local area networks, each node (computer used as a server) running its own instance of an operating system. Computer clusters emerged as a result of convergence of a number of computing trends including the availability of low cost microprocessors, high speed networks, and software for high performance distributed computing.
Clusters are usually deployed to improve performance and availability over that of a single computer, while typically being much more cost-effective than single computers of comparable speed or availability.[resources from Wikipedia]

In computer clusters, heartbeat network is a private network which is shared only by the cluster nodes, and is not accessible from outside the cluster. It is used by cluster nodes in order to monitor each node's status and communicate with each other.[resources from Wikipedia]
For more information, check here.

2.  How to setup IP Aliasing(virtual IP)?
IP aliasing is associating more than one IP address to a network interface. With this, one node on a network can have multiple connections to a network, each serving a different purpose.[resources from Wikipedia]
My system is CentOS, now we start to setup ip aliasing:
# cd /etc/sysconfig/network-scripts/
now create a file named "ifcfg-eth0:0". The reason why you create this file is that in order to create an alias for the interface, you need to create a file in the format ifcfg-ethX:Y - where X is the interface number and Y represents the alias number. The first alias would be numbered 0, the next alias 1, and so forth. For the first alias on the first interface, you would create a file called ifcfg-eth0:0.
Add the following to ifcfg-eth0:0
DEVICE=eth0:0
BOOTPROTO=dhcp
ONBOOT=yes
IPADDR="
192.168.111.111"
NETMASK="255.255.255.0"

IPADDR is your virtual ip address, for example: 192.168.111.111
In order to apply the new ip address, you can restart the full network or use ifup to bring up the ip alias interface:
# service network restart   
OR
#ifup eth0:0

To verify everything is okay, use the following command
# ifconfig
to list all active interfaces, it will look like this:
eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          inet addr:XXX.XXX.XXX.XX  Bcast:XXX.XXX.XXX.255  Mask:255.255.255.0
          inet6 addr: XXX::XXX:XXXX:XXXX:XXXX/XX Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1672376185 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1830099250 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1022777289 (975.3 MiB)  TX bytes:111054671 (105.9 MiB)

eth0:0    Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          inet addr:192.168.111.111  Bcast:XXX.XXX.XXX.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
You can also use "ping" to check the new ip adderss works or not.
Lets go to question 3.

3.  How to install EPEL Repo on CentOS5.X or CentOS6.X?

This took me several hours to install heartbeat on CentOS6.X, I tried "yum install heartbeat", it failed and return Error, Nothing to do. This is because I did not download EPEL Repo. Once EPEL Repo was downloaded, all problems would be solved.Here is the instructions:
Centos 5.x
wget http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
sudo rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm
Centos 6.x
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
Once installed you should see some additional repo definitions under the /etc/yum.repos.d directory.
$ ls -1 /etc/yum.repos.d/epel* /etc/yum.repos.d/remi.repo
/etc/yum.repos.d/epel.repo
/etc/yum.repos.d/epel-testing.repo
/etc/yum.repos.d/remi.repo
Enable the remi repository

The remi repository provides a variety of up-to-date packages that are useful or are a requirement for many popular web-based services.  That means it generally is not a bad idea to enable the remi repositories by default.
First, open the /etc/yum.repos.d/remi.repo repository file using a text editor of your choice:
#sudo vim /etc/yum.repos.d/remi.repo
Edit the [remi] portion of the file so that the enabled option is set to 1.  This will enable the remi repository.
name=Les RPM de remi pour Enterprise Linux $releasever - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/$releasever/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
failovermethod=priority
Okay, so far question 3 is solved.
For more information, check here .

---------------------------------------------------------------------------------------------------------------------------------
Configuration of Two-Node Cluster with HeartBeat on CentOS

Pre-configuration requirements:
     1. hostnames for your two nodes.
      setup hostname  in the file "hosts", it is under /etc/hosts
      for example, I have two remote servers cat and dog. configure the hosts file for both servers.
       then use command "uname -n" for each of them.
       if server1 returns "cat",  use cat, if it returns "mycat", use mycat; the same for dog.
     2. ip aliasing
       we only need to setup ip aliasing for your primary node(master), don't do anything about ip aliasing on your secondary node(slave). [we've talked about how to setup IP aliasing at the beginning of this tutorial].

Configuration:
     1. Download and install heartbeat packages .In my case I use CentOS so install heartbeat with yum:
      # yum install heartbeat
      [if  an ERROR occurred and said Nothing to do, see How to install EPEL Repo at the beginning of this tutorial]
     2. Before configure heartbeat files , we need to move them( ha.cf, authkeys and haresources) to /etc/ha.d
     #cp /usr/share/doc/heartbeat-3.0.4/ha.cf /etc/ha.d/
     #cp /usr/share/doc/heartbeat-3.0.4/authkeys /etc/ha.d/
     #cp /usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d/
     3. Now, lets start to configure them one by one:
        (1)Lets configure ha.cf first:
            #sudo vim /etc/ha.d/ha.cf
            Add the following lines to ha.cf
            logfile /var/log/ha-log
            logfacility local0
            keepalive 2
            deadtime 10
            initdead 120
            bcast eth0
            udpport 694
            auto_failback on
            node name_of_node1
            node name_of_node2
        (2) configure authkeys:
            #sudo vim /etc/ha.d/authkeys
            Add the following lines to it:
             auth 2
             2 sha1 test-ha
           then, change the permission for authkeys:
           # sudo chmod 600 /etc/ha.d/authkeys
        (3) configure haresources:
           #sudo vim /etc/ha.d/haresources
           Add the following lines to it:
           cat 192.168.111.111/24/eth0 mysqld
           cat is my primary server name(name of node 1), 192.168.111.111 is my IP Aliasing(virtual ip).
           24 is the port number we use, mysqld is the service we want to run.
Configuration is done so far, make sure the content of authkeys and haresources files are the same on both servers.

Testing  whether two-node cluster with heartbeat works properly or not.
      1.  Turn off your firewall on both servers of just modify iptables and let it allow 649 go through[Usually the former way is better].
      2.  Monitor the log files of heartbeat using the following command, we setup the log files in our ha.cf file.
         # tail -f /var/log/ha-log
      3.  Start primary server heartbeat and then start secondary server heartbeat using
         # sudo service heartbeat start
      Then, it may show something looks like this :Local status now set to: 'up'
       Now wait several seconds [at most 3 minutes], it will show the service is started. 
     4. Stop heartbeat on primary server using following command
        # sudo service heartbeat stop
         Now the service on secondary server will be activated.
    5 . Check the service/process is starting  using:
        # ps -ef | grep mysqld
         mysqld is the service/process name


I get more problems about heartbeat:
1. Message hist queue is filling up.
2. Both machines own our resources.
Those two problems occurred due to the firewall in most cases, so make sure firewall is closed on both
servers using:
# sudo service iptables stop




Tuesday, August 21, 2012

ifconfig NOT working on CentOS

When I try command "ifconfig" on CentOS, it responded "Command not known". I realized that it is not in my profile file.
To check up whether it is in your profile, just type in:
> echo $PATH
you can use /sbin/ifconfig is working or not. To add it to your user profile, you need to add the following to your /etc/profile:
export PATH=$PATH/sbin
Save and quit:
> :wq
and then activate your changes:
> source /etc/profile

Wednesday, August 15, 2012

Jetty/Tutorial/Embedding Jetty

All sources are from:http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
The only goal of this post is to help learning jetty.

To embed a Jetty server, the following steps are typical:
  • Create the server
  • Add/Configure Connectors
  • Add/Configure Handlers 
  • Add/Configure Servlets/Webapps to Handlers
  • Start the server
  • Wait (join the server to prevent main exiting) 
Creating a server:
public class SimplestServer
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.start();
        server.join();
    }
}
Wrting Handlers:
To produce a response to a request, Jetty requires a Handler to be set on the server. A handler may:
  • Call another Handler (see HandlerWrapper).
  • Examine/modify the HTTP request.
  • Generate the complete HTTP response.
  • Select one or many Handlers to call (see HandlerCollection).
a simple hello world handler:
public class HelloHandler extends AbstractHandler
{
    public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }
}
The parameters passed to the handle method are:
  • target–the target of the request,which is either a URI or a name from a named dispatcher.
  • baseRequest–the Jetty mutable request object,which is always unwrapped.
  • request–the immutable request object, which might have been wrapped.
  • response–the response, which might have been wrapped.
The handler sets the response status, content-type and marks the request as handled before it generates the body of the response using a writer. The following code from OneHandler.java shows how a Jetty server can use this handler:
public static void main(String[] args) throws Exception
{
    Server server = new Server(8080);
    server.setHandler(new HelloHandler());
 
    server.start();
    server.join();
}

Configure Connectors:
To configure the HTTP connectors that the server uses, you can set one or more connectors on the server. You can configure each connector with details such as interface, port, buffer sizes, timeouts, etc.
public class ManyConnectors
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server();
 
        SelectChannelConnector connector0 = new SelectChannelConnector();
        connector0.setPort(8080);
        connector0.setMaxIdleTime(30000);
        connector0.setRequestHeaderSize(8192);
 
        SelectChannelConnector connector1 = new SelectChannelConnector();
        connector1.setHost("127.0.0.1");
        connector1.setPort(8888);
        connector1.setThreadPool(new QueuedThreadPool(20));
        connector1.setName("admin");
 
        SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
        String jetty_home = 
          System.getProperty("jetty.home","../jetty-distribution/target/distribution");
        System.setProperty("jetty.home",jetty_home);
        ssl_connector.setPort(8443);
        SslContextFactory cf = ssl_connector.getSslContextFactory();
        cf.setKeyStore(jetty_home + "/etc/keystore");
        cf.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
        cf.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
 
        server.setConnectors(new Connector[]{ connector0, connector1, ssl_connector });
 
        server.setHandler(new HelloHandler());
 
        server.start();
        server.join();
    }
}




























Tuesday, August 14, 2012

How to set and use different JDK versions for Netbeans on CentOS

 How to set and use different JDK versions for Netbeans on CentOS

1.Find the configuration file netbeans.conf
# locate netbeans.conf

2.The path would look like this:
# /usr/local/netbeans-6.9.1/etc/netbeans.conf

3.Open the configure file:
# sudo vim  /usr/local/netbeans-6.9.1/etc/netbeans.conf

4.Find the following statements in your netbeans.conf file:
# Default location of JDK, can be overridden by using --jdkhome :
netbeans_jdkhome="/usr/java/latest"


5. Change the version you like.
it should work now.

Friday, August 10, 2012

Jenkins

How to start with jenkins?
1. Installation on CentOS:
# sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
# sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
# sudo yum install jenkins

2. Start Jenkins
The easiest way to execute Jenkins is through the built in Winstone servlet container. You can execute Jenkins like this:
# java -jar jenkins.war
To see Jenkins UI, open a web browser and go to URL http://myServer:8080 where myServer is the name of the system running Jenkins, for example: localhost or 127.0.0.1

3.  Configure Jenkins
Go to >Manage Jenkins >Manage Plugins.
Choose github plugin and git plugin to be installed.
Go to >Manage Jenkins >Configure System
    Security Realm:
    choose Jenkins's own user database, and then choose Allow users to sign up.
    Authorization:
    choose Matrix-based security, and add user or group.
    JDK:
    if JDK is already installed, just need to fill in JAVA_HOME, or you can choose install  automatically.
    Git:
    if git is already installed, just leave the name default and fiil "git " in the box of path to git executable, or install it automatically.
    Maven:
    if Maven is already installed, just fill in MAVEN_HOME, or install automatically.
After all those are finished, press save, the configuration step is done.


How to Create Jenkins User?
1. Create jenkins user.
#sudo /usr/sbin/useradd -m jenkins -d /home/jenkins
2. Checkout jenkins user information.
# id jenkins
it looks like this:
uid=506(jenkins) gid=506(jenkins) groups=506(jenkins);
3. Work as a jenkins user.
# su jenkins OR su-jenkins
4. Set jenkins user password.
# sudo /usr/bin/passwd jenkins


HOWTO connect Jenkins with PRIVATE GitHub Repo?


1. su as a jenkins user to create the SSH key pair.
if you don't know your jenkins user password, you can reset it as following :
#sudo /usr/bin/passwd jenkins

2. Create SSH Key:

ssh-keygen is used to generate that key pair for you.
Here is a session where your own personal private/public key pair is created:
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/cantin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/cantin/.ssh/id_rsa.
Your public key has been saved in /home/cantin/.ssh/id_rsa.pub.
The key fingerprint is:
f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 cantin@sodium

Explanation: 
The command ssh-keygen -t rsa initiated the creation of the key pair.
No passphrase was entered (Enter key was pressed instead).
The private key was saved in .ssh/id_rsa. This file is read-only and only for you. No one else must see the content of that file, as it is used to decrypt all correspondence encrypted with the public key.
The public key is save in .ssh/id_rsa.pub.

3. Paste the content of id_rsa.pub to your Github account. 

# cat id_rsa.pub  [show the public key onscreen]
(1) Copy the public key, go to GitHub, you'll see Account setting on the upper right, press it and you'll see SSH Keys, then paster the key here!
(2) An alternative way is Deploy key to a specific repo.  You don't paste the key to account this time, go to specific repo, press Admin, and choose Deploy key.
Now, it should work.

"localhost" is not known on CentOS

localhost is not known
My linux system could not recognize its own localhost.
Here is several ways to check "localhost" work or not:
1. Enter following command in terminal, and localhost is the system's hostname:
# ping localhost
It will show you something looks like this:
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.029 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.086 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.121 ms
if it does not show you the above thing, it does not work, or you can check ping directly with your local ip address:
#ping 127.0.0.1   
2. Check localhost using ssh
#ssh localhost
It will show you something looks like this:
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is a3:0a:4f:d4:f1:73:6f:25:eb:92:70:9d:93:b8:45:4f.
Are you sure you want to continue connecting (yes/no)?
If not so, it means your hostname does not work properly.

HOWTO solve it?
1. Modify /etc/hosts
It looks like the following:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       localhost.localdomain localhost mycentos
I added localhost and mycentos as my host name.
2.Try to flush DNS cache :
#/etc/init.d/nscd restart
It works for my system.












Thursday, August 9, 2012

[Solved] File not found when running maven pom.xml in sub-directory

Run a maven project on jenkins.
If the data structure looks like this(pom.xml is under root directory):
my-app
 | - - pom.xml
 | - - src
          | - - main
          |          | - - java
          |                  | - - com
          |                           | - - mycompany
          |                                              | - - app
          |                                                       | - - App.java
          |- - test
                    | - - java
                            | - - com
                                     | - - mycompany
                                                        | - - app
                                                                 | - - AppTest.java

It will work properly, but if my root folder has more than one modules(projects), if will fail and say "no pom.xml"

root-dir
 | - - project1
 |       | - - pom.xml
 |       | - - src
 |- - project2
         | - - pom.xml
         | - - src
For the above structure, each module has its own pom.xml, but there is NO pom.xmll in the root directory, so when you run maven, it will not find any pom.xml in root directory. In order to find out the pom.xml in subdirectory, we need add a pom.xml in root folder:
What you need to do is add modules to your root pom.xml


after you create this pom.xml, paste it into your root directory, data structure will look like:


root-dir
 | - - project1
 |       | - - pom.xml
 |       | - - src

 |- - project2
 |       | - - pom.xml
 |       | - - src
 | - - pom.xml

Hope it helps:)