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