|
Introduction - Array is one of the best feature of Bash shell and I am fan of this feature!!. Bash shell provides one-dimensional array. Any variable may be used as an array. There are many ways to declare array. Builtin “declare” command is used to explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based. A array is created automatically if any variable is assigned to using the syntax array_name[subscript]=value. The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. To explicitly declare an array, use “declare -a name“. “declare -a name[index]” is also accepted and the “index” is ignored here.
Arrays are assigned to using compound assignments of the form array_name=(value1,value2, … valueN), where each value is of the form [index]=string. Only string is required. In bash array indexing is same as C language which starts at zero. This syntax is also accepted by the declare builtin.
* How to assign/declare array variable in Bash shell -
declare -a myarr[0]=”Arun”
declare -a my_array
my_array=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
apna_array=([0]=arun [1]=bagul [4]=Santhosh [3]=ravi [5]=1024)
array[1]=”Bagul”
* How to refer the array and it’s element -
Suppose I want to print the above array “apna_array”, the syntax as shown below….
echo “${apna_array[*]}”
and
echo “${apna_array[@]}”
This syntax is very common in bash shell, ${variable_name} is same as $variable_name. While in array the “[*]” and “[@]” indicate the all element of array.
NOTE - Remember above two syntax are not same there is one difference. It’s an assignment for you guys/girls!
* How to access particular element of array -
name=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
here “name” is an array and I want to access the first and second element of the array “name”.
echo “My name is - ${name[0]} ${name[1]}”
* How to find the length of array or element of array -
${#my_array[index]} expands to the length of (element of array) ${my_array[index]} . If “index” is “*” or “#” then expansion is the number of elements in the array. Please note - referencing an array variable without a subscript/index is equivalent to referencing element zero.
Suppose “student” is array then, ${student} is same as ${student[0]}
#declare and assign elements to array “student_record” -
student_record=(10 “Ravi Bhure” “1st year of BE” “SSVPS College” Dhule)
echo ${student_record}
echo ${student_record[0]}
echo “Total no of element in array is - ${#student_record[*]}”
echo “Total no of element in array is - ${#student_record[@]}”
echo “Length of element is - ${#student_record[1]}”
* How to delete element of array -
“unset“ builtin command is used to delete element of array.
unset array_name[index] - delete the array element at given “index”. If “index” is “*” or “@” then, it will removes the entire array.
big_city=(Bangalore Pune Nasik Hydrabad Surat)
Here “big_city” is the array of big cities in India. Suppose I want to delete “Nasik” from above array -
unset big_city[2]
* Example - Look below script and it’s output
root@arun:~# cat how_to_array.sh
#!/bin/bash
#declare and assign element
declare -a myarr[0]=”Arun”
declare -a metro_array=(Mumbai Delhi Kolkata Chennai)
declare -a my_array
my_array=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
name_arr[1]=”Arun Bagul”
echo “————————–”
echo “myarr is - ${myarr[0]}”
echo “Metro cities in India - ${metro_array[*]}”
echo “My name is - ${my_array[0]} ${my_array[1]}”
echo “Name - ${name_arr[1]}”
echo “Name - ${name_arr[*]}”
echo “————————–”
#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is - ${myarr[*]}”
apna_array=([0]=arun [1]=bagul [3]=Santhosh [4]=ravi [5]=1024)
echo “${apna_array[*]}”
echo “${apna_array[@]}”
student_record=(10 “Ravi Bhure” “1st year of BE” “SSVPS College” Dhule)
echo ${student_record}
echo ${student_record[0]}
echo “Total no of element in array is - ${#student_record[*]}”
echo “Total no of element in array is - ${#student_record[@]}”
echo “Length of element is - ${#student_record[1]}”
#how to delete array -
del_arr=(Arun Bagul)
echo “del_arr before ‘delete’ is - ${del_arr[*]}”
#delete
unset del_arr
echo “del_arr after ‘delete’ is - ${del_arr[*]}”
echo “————————–”
#how to delete one element in array -
big_city=(Bangalore Pune Nasik Hydrabad Surat)
#suppose - want to delete “Nasik” from above array -
echo “Big City before ‘delete’ is - ${big_city[*]}”
#delete
unset big_city[2]
echo “Big city after ‘delete’ is - ${big_city[*]}”
root@arun:~#
*** Run above script -
root@arun:~# ./how_to_array.sh
————————–
myarr is - Arun
Metro cities in India - Mumbai Delhi Kolkata Chennai
My name is - Arun Bagul
Name - Arun Bagul
Name - Arun Bagul
————————–
myarr is - Arun - System Engineer!
arun bagul Santhosh ravi 1024
arun bagul Santhosh ravi 1024
10
10
Total no of element in array is - 5
Total no of element in array is - 5
Length of element is - 10
del_arr before ‘delete’ is - Arun Bagul
del_arr after ‘delete’ is -
————————–
Big City before ‘delete’ is - Bangalore Pune Nasik Hydrabad Surat
Big city after ‘delete’ is - Bangalore Pune Hydrabad Surat
root@arun:~#
Thank you,
Arun Bagul
What is Tomcat -
Tomcat is the servlet container, which implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystem. Tomcat is pure HTTP web server for Java based appicatio.
How to install -
1) Download java -
Go to http://java.sun.com/javase/downloads/?intcmp=1281 choose your platform and follow the steps on the site.
2) Installing the binaries
The installation of the Java Developer’s Kit is pretty straightforward if if you retrieve the RPM for it. If you have to download the RPM from Sun’s site, it isn’t acutally in RPM format yet. It’ll be called something like j2sdk-xxx-linux-rpm.bin. chmod 700 it and execute it. This will run the Sun EULA and after you agree to it, generate the actual RPM file.
Execute the binary - chmod +x jdk-xxx-linux-i586-rpm.bin
After the RPM is produced, install it simply by running rpm -ivh jdk-xxx-linux-i586-rpm. This will install the JDK in /usr/java/jdk.x.x. You need to modify the user’s .bash_profile to include /usr/java/jdk/bin in the path so the executables will run. What I usually do is make a symbolic link called /usr/java/jdk that points to this /usr/java/jdk.x.x. That way I don’t have to update my path in the .bash_profile every time I install a new version of the JDK.
You should also set your JAVA_HOME in the .bash_profile with something like export JAVA_HOME=/usr/java/jdk
or on bash promt of user
[root@localhost]# JAVA_HOME=/usr/java/jdk ; export JAVA_HOME
Download the latest stable release of tomcat from http://mirrors.24-7-solutions.net/pub/apache/tomcat/tomcat-5/v5.5.26/bin/apache-tomcat-5.5.26.tar.gz
after downloading apache-tomcat-5.x.x.tar.gz extract it into /usr/local/
Installing the binaries
cd /usr/local
tar -xvzf apache-tomcat-5.x.x.tar.gz
cd apache-tomcat-5.x.x
cd bin
rm *.bat
To enable the Tomcat manager, you need to modify /usr/local/apache-tomcat-5.x.x/conf/tomcat-users.xml add a user »admin« or with the role »manager«. The result should look like this:
<?xml version=’1.0′ encoding=’utf-8′?>
<tomcat-users>
<role rolename=”manager”/>
<role rolename=”tomcat”/>
<role rolename=”role1″/>
<user username=”both” password=”tomcat” roles=”tomcat,role1″/>
<user username=”tomcat” password=”tomcat” roles=”tomcat”/>
<user username=”admin” password=”password” roles=”manager”/>
<user username=”role1″ password=”tomcat” roles=”role1″/>
</tomcat-users>
Now you should be able to startup tomcat -
/bin/sh /usr/local/apache-tomcat-5.x.x/bin/startup.sh
You should now be able to connect to: http://localhost:8080/index.jsp
![]()
Thanks
Ravi Bhure
There is more to Red Hat Enterprise Linux 5 (RHEL5) than Xen. I, for one, think people will develop a real taste for YUM (Yellow dog Updater Modified), an automatic update and package installer/remover for RPM systems.
YUM has already been used in the last few Fedora Core releases, but RHEL4 uses the up2date package manager. RHEL5 will use YUM 3.0. Up2date is used as a wrapper around YUM in RHEL5. Third-party code repositories, prepared directories or websites that contain software packages and index files, will also make use of the Anaconda-YUM combination.
Essentially, YUM automatically computes dependencies and figures out what actions need to happen in order to successfully install packages. The Yellowdog Update Modified package manager is actually a variant of the Yellowdog Update Package (YUP), which is used by the Yellowdog Linux project to manage its applications. Yum is a version of YUP that is compatible with RPMs.
Using YUM makes it much easier to maintain groups of machines without having to manually update each one using RPM. Some of its features include:
Multiple repositories
Simple config file
Correct dependency calculation
Fast operation
RPM-consistent behavior
comps.xml group support, including multiple repository groups
Simple interface
RHEL5 moves the entire stack of tools which install and update software to YUM. This includes everything from the initial install (through Anaconda) to host-based software management tools, like system-config-packages, to even the updating of your system via Red Hat Network (RHN). New functionality will include the ability to use a YUM repository to supplement the packages provided with your in-house software, as well as plugins to provide additional behavior tweaks.
YUM automatically locates and obtains the correct RPM packages from repositories. It frees you from having to manually find and install new applications or updates. You can use one single command to update all system software, or search for new software by specifying criteria.
Keep in mind that it is always useful to keep your packages in a local YUM repository. The advantage of this is that when you install a package, YUM will automatically resolve any dependencies, not only by downloading the necessary packages from the other repositories you might have in you list, but also by using your local repository as a resource for potential dependencies. When installing a package with YUM, you must have already created RPM packages for all your dependences. That way, YUM can resolve all the dependencies. You won’t be able to install your package if the dependencies do not exist in the repositories on your list.
Creating your own repository in RHEL5
To install the RPM, you’ll need to type this command:
# yum install createrepo
What this will do is put all your customer RPM packages in a directory, where you can then create the necessary metadata that is needed for your local repository. You would do that by running this command:
# createrepo /mnt/fc_local_repo/
Your local YUM repository has been created. Whenever you put in any new RPMs, you’ll have to run this command, so that the new repository metadata gets updated. To install an RPM package and all the other packages that it depends on, you only need to run:
# yum install my_package.RPM
To install the package group MySQL Database, enter the command:
# yum groupinstall “MySQL Database”
If you need to upgrade the packages for MYSQL: # yum groupupdate “MySQL Database”
To search for packages which provide for Mail Transfer Agents (MTAs), or that have MTA in the name:
# yum provides MTA
Let’s say we want to update our entire system. It’s as simple as typing:
# yum update
To activate automatic daily updates:
/sbin/chkconfig –level 345 yum on; /sbin/service yum start
Configuring access to repositories in RHEL5
To add an extra repository, place a definition file in the /etc/yum.repos.d/ directory on your system. Package providers make the definition files for their repositories available on their websites. You must have root access to add a file to the definitions directory. To copy the definition file example.repo, type this command:
# cp example.repo /etc/yum.repos.d/
The configuration file for each repository should include a gpgkey setting. This setting specifies the location of a public key that verifies the packages provided by that repository. This public key is automatically imported the first time that you install software from the repository.
In conclusion, if you have used YUM before, you should have no problem getting used to this change in RHEL5. If you have not used YUM, once you get passed the initial learning curve, I’m certain that you will love it.
for Yum for RHE4 just checkout official centos wiki pages on centos.org
http://wiki.centos.org/HowTos/PackageManagement/YumOnRHEL
Thanks
Ravi
Introduction - apt-get is the APT package handling utility. I am using apt-get command for my day to day activities, which is very handy tool on Debian/Ubuntu Linux. Which can be used for installing/updating or removing Debian package (.deb), updating system etc. So I was thinking about using apt-get on Redhat (RPM) based Linux. is there any way to use it? the answer is Yes!! there is way to use apt-get on Redhat (RPM) based Linux.
root@localhost:~#rpm -ivh http://apt.sw.be/redhat/el4/en/x86_64/dag/RPMS/apt-0.5.15lorg3.2-1.el4.rf.x86_64.rpm
Add following mirror -
root@localhost:~# vi /etc/apt/sorces.list.d/os.list
repomd http://apt.sw.be redhat/el4/en/x86_64/dag/
root@localhost:~#
How to use apt-get -
root@localhost:~# apt-get upgrade
root@localhost:~# apt-get update
root@localhost:~#apt-cache search pkg_name
root@localhost:~#apt-get install pkg_name
root@localhost:~#apt-get remove pkg_name
Thanks
Ravi Bhure
Introduction - To install Wordpress, you should have Apache, MySQL, and PHP installed on your Linux server(typical LAMP server). If you don’t have LAMP server installed yet, then there are plenty of tutorials out there that will help you install them. Note that this walk through will probably also work for recent versions of Debian/Ubuntu Linux.
How to install the wordpress package using apt-get -
root@ravi:~# apt-get install wordpress
If you get an error message that the package wordpress cannot be found, it is most likely because you do not have multiverse servers enabled for apt-get. Here’s how to add them. First, edit /etc/apt/sources.list
root@ravi:~# vi /etc/apt/sources.list
Add the following two lines (it doesn’t particularly matter where you add them, or even if you have duplicate lines).
deb http://us.archive.ubuntu.com/ubuntu/ gusty main restricted universe multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ gusty main restricted universe multiverse
Now tell apt-get to update its package list and you should be able to find wordpress.
root@ravi:~# apt-get update
Now try to install Wordpress as shown above. Next we have to configure Apache for WordPress -
root@ravi:~# vi /etc/apache2/apache2.conf
At the bottom of the file add the following lines:
Alias /blog /usr/share/wordpress
<Directory /usr/share/wordpress>
Options FollowSymLinks
AllowOverride Limit Options FileInfo
DirectoryIndex index.php
</Directory>
You can access WordPress with URL http://yourdomain.com/blog/. If you want it to go somewhere else, just adjust the path accordingly.
Now we have to tell Apache to reload its configuration files.
root@ravi:~# /etc/init.d/apache2 reload
Next, we are going to create a database in MySQL for WordPress to use. First, login to MySQL Server -
root@ravi:~# mysql -u root -p
If you have never used MySQL before, the default root password is blank. Now would be a good time to set one.
Next we are going to create a database for WordPress to use. I’m going to be very imaginative and name the database “wordpress”.
mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)
Now we have to create a database user for wordpress. This part is important; you could just use the same user for all of your applications, but if something went wrong, you could lose all of your data. By creating a separate MySQL user for each application, you are limiting your damage. I’m going to be imaginative again and choose the name “wordpress_user” for the WordPress database user account. You might want to pick a better password than fluffy; that’s simply what I’m using for this demonstration.
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO ‘wordpress_db_user’@'localhost’ IDENTIFIED BY ‘password’ ;
Query OK, 0 rows affected (0.00 sec)
Now let’s make sure that these changes took:
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
We are done with MySQL now, so let’s quit.
mysql> quit;
Bye
Next we are going to modify WordPress’s configuration file to tell it about the database and user we just created for it. But first, we have to deal with a little idiosyncrasy. The Ubuntu WordPress package creates a symbolic link to /etc/wordpress/ for its configuration file, but we aren’t going to be using anything in /etc/ for our installation. So first, get rid of the symbolic link. Don’t worry, this is just deleting a link, it’s not actually deleting any files. The default wp-config.php will remain in /etc/wordpress/ if you really want it.
root@ravi:~# rm /usr/share/wordpress/wp-config.php
Now copy the sample configuration over to the main configuration location.
root@ravi:~# cp /usr/share/wordpress/wp-config-sample.php /usr/share/wordpress/wp-config.php
Now let’s edit the configuration.
root@ravi:~# vi /usr/share/wordpress/wp-config.php
Remember the database name, user account, and password that we just set in MySQL? We are going to input these into the configuration file and save it.
define(’DB_NAME’, ‘wordpress’); // The name of the database
define(’DB_USER’, ‘wordpress_DB_user’); // Your MySQL username
define(’DB_PASSWORD’, ‘password’); // …and password
define(’DB_HOST’, ‘localhost’); // 99% chance you won’t need to change this value
We’re almost done. Now we just use WordPress’s built-in installation script. Go to http://yourdomain.com/blog/wp-admin/install.php and follow the directions. Write down the auto-generated password it gives you.
Now we’re going to login to WordPress. Go to http://yourdomain.com/blog/wp-login.php and login with the account ‘admin’ and the password you just wrote down. Now everything is working! The first thing you should do is change the password to the admin account. This option can be accessed from the “Users” tab along the top of the screen.
We have one more thing to do. We’re going to install a plugin for WordPress called “Spam Karma”. This plugin will defend your site from comment spam, which is a much bigger problem than you’d realize, especially if your site starts getting really popular. Luckily, it’s very easy to install the plugin. Go to the plugins directory, download the plugin using wget, and unzip it.
root@ravi:~#cd /usr/share/wordpress/wp-content/plugins/
root@ravi:~#wget http://wp-plugins.net/sk2/sk2_final.zip
root@ravi:~#unzip sk2_final.zip
Now, go back into the admin console of your blog, go to the Plugins tab, and click on the Activate button next to Spam Karma.
That’s it; you’re done! Try writing your first post on WordPress, or configure your site’s look and feel.
Thanks
Ravi Bhure
Introduction - Zabbix is a solution for monitoring applications, networks, and servers. With Zabbix you can monitor multiple servers at a time, using a Zabbix server that comes with a web interface (that is used to configure Zabbix and holds the graphs of your systems) and Zabbix agents that are installed on the systems to be monitored. The Zabbix agents deliver the desired data to the Zabbix server. This tutorial shows how you can install the Zabbix server and agent on a Ubuntu Gusty 7.10.
I will use the system SFPAZABBIX with the IP address 192.168.3.180 as the Zabbix server, and I’ll install a Zabbix agent on the same system -
The Zabbix server can store its information in a MySQL or PostgreSQL database. We use MySQL here, so we install the MySQL server and client first using apt-get -
root@ravi:~# apt-get install mysql-server mysql-client
Create a password for the MySQL user root (replace yourrootsqlpassword with the password you want to use) -
root@ravi:~# mysqladmin -u root password yourrootsqlpassword
Installing apache2 php5 for web interface -
root@ravi:~# apt-get install apache2 php5 php5-gd
Afterwards, we can install the Zabbix server, Zabbix agent, and the Zabbix web interface with a single command -
root@ravi:~# apt-get install zabbix-server-mysql zabbix-frontend-php zabbix-agent
Apt installation will be asked a few questions-
Like mysql root user password and Zabbix database password, give mysql root password there that we created already.
This should create a MySQL database called Zabbix.
Next we must edit the Zabbix agent configuration in /etc/zabbix/zabbix_agentd.conf.
Replace Server=localhost with Server=127.0.0.1 (to avoid lookup problems of localhost) and specify the hostname of the current system in the Hostname line.
root@ravi:~# vi /etc/zabbix/zabbix_agentd.conf
[...]
Server=127.0.0.1
[...]
Hostname=SFPAZABBIX
[...]
root@ravi:~#
Then we restart the Zabbix agent -
root@ravi:~# /etc/init.d/zabbix-agent restart
Finally, we must specify the password of our zabbix MySQL user in the Zabbix web interface configuration -
root@ravi:~# vi /etc/zabbix/dbconfig.php
<?php
$DB_TYPE=’MYSQL’;
$DB_SERVER=’localhost’;
$DB_DATABASE=’zabbix’;
$DB_USER=’root’;
$DB_PASSWORD=’mysqlrootpassword’;
?>
root@ravi:~#
Restart services for zabbix server & agent -
/etc/init.d/zabbix-server restart
/etc/init.d/zabbix-agent restart
That’s it. you can access Zabbix webbased admin panel check URL http://SFPAZABBIX/zabbix or http://192.168.3.180/zabbix
>
Afterwards, go to Configuration TAB and configure Zabbix!
If you have problems with Zabbix, please check the Zabbix logs - * /var/log/zabbix-agent/zabbix_agentd.log
* /var/log/zabbix-server/zabbix_server.logThe Zabbix configuration files for the server, agent, and web interface are as follows -
* /etc/zabbix/apache.conf
* /etc/zabbix/dbconfig.php
* /etc/zabbix/zabbix_agentd.conf
* /etc/zabbix/zabbix_server.conf
Taking backup of Zabbix server database using below script -
#!/bin/bash
# script for dumping the contents of a zabbix MySQL database
# this script will create a compressed mysqldump of the specified database
savePath=/zabbix/
fileName=”ZabbixDBbackup” # filename for the backup note the
dateVar=$(date +%Y-%m-%d) # date variable to append to filename
mysqldump -u root -p(mysqlpassword) zabbix | gzip > $savePath$fileName-$dateVar.gz
Schedule cronjob for Zabbix is as follows -
@daily /bin/sh /zabbix/backupforzabbix.sh #zabbix db backup daily midnight
Append existing zabbix db backup on zabbix db -
Go to where is the zabbix db backup path ( i.e. /zabbix), check date & go for newer date when zabbix was running in good condition.
(ls -l commands output give you the newer date)
Unzip the compressed file and rename it to dbfilename.sql (with sql extension)
Now go to mysql CLI prompt -
root@ravi:~# mysql -u root -p
mysql> use zabbix;
mysql> \. filename.sql
above command append the tables of zabbix database.
Thanks
Ravi Bhure
Introduction -
Introduction -
This is the simple way to change password of user on Unix/Linux system non interactively! This script is really helpful for updating password in bulk or adding new users and setting there password in bulk. Another method is to expect tool please refer article on expect!!
Let’s see the script now -
root@arun:~# cat /script/mypasswd.sh
#! /bin/bash
if [ $# -eq 2 ]; then
echo “Script to change password non-interactively”
echo “——————————————-”
else
echo ” * Usage: mypasswd.sh username password”
exit 1
fi
passwd $1<<ARUN
$2
$2
<<ARUN
root@arun:~#
How to use it ?
root@arun:~# ./mypasswd.sh
* Usage: mypasswd.sh username password
root@arun:~#
root@arun:~# ./mypasswd.sh myuser mypwd
Script to change password non-interactively
——————————————-
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
root@arun:~#
Thank you,
Arun Bagul
Introduction - I was looking for a PHP CLI (command line) script through which I can hide the user input details like password etc on windows platform. I didn’t find any script in php, which hide the password details while user giving input through command prompt. Then I got an idea to hide the password script using batch command. I tried to find out the batch script through which I can take the inputs and then pass the php script. Finally I found a wonderful batch script, which takes the input user name and password (hidden).
** Batch Script => input.bat
@echo off
cls
SET /P uname=Enter Username:
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>in.com
set /p password=Enter password :<nul
for /f “tokens=*” %%i in (’in.com’) do set password=%%i
del in.com
echo.
c:\php\php.exe d:\php\test.php %uname% “%password%”
Pause
Execute the batch file on window system (DOS prompt) will take the inputs and give the inputs to php script. To execute batch file just double click on input.bat file…
Thank you,
Arun Bagul and Santhosh Tirunahari
Introduction - MySQL Clustering is simple, easy to setup and reliable solution. That enables clustering of in-memory databases in a shared-nothing system. MySQL supports only NDB storage or table type system in clustering environment. MySQL Cluster integrates the standard MySQL server with an in-memory clustered storage engine called NDB. A MySQL Cluster consists of a set of computers, each running a one or more processes which may include a MySQL Server (API), a Data (Storage) node, a Management server.
In MySQL Cluster the tables are stored in the Data (Storage) nodes. Due to this tables are directly accessible from all other MySQL servers in the cluster, provided that the Database which contains the table is available (if not need to create database) on all MySQL nodes. The Data stored in the Data nodes for MySQL Cluster can be mirrored. The NDB storage engine can be configured with a range of failover and load balancing options. NDB is an in-memory storage engine offering high-availability and data-persistence features.

There are three types of cluster nodes, in MySQL Cluster configuration. There will be at least three nodes, one of each of types is require to configure MySQL clustering successfully.
1) Management (MGM) Node - Manages the other nodes within the MySQL Cluster, performing such functions as providing configuration data, starting and stopping nodes, running backup, and so forth. Because this node type manages the configuration of the other nodes, a node of this type should be started first, before any other node. An MGM node is started with the command ndb_mgmd.
2) Data node - This node stores cluster data. There are as many data nodes as there are replicas, times the number of fragments. For example, with two replicas, each having two fragments, you will need four data nodes. It is not necessary to have more than one replica. A data node is started with the command ndbd.
3) SQL node - use to accesses the cluster data. SQL node is a traditional MySQL server that uses the NDB Cluster storage engine. An SQL node is typically started with the command mysqld –ndbcluster or by using mysqld with the ndbcluster option added to my.cnf.
An SQL node is actually just a specialised type of API node, which designates any application which accesses Cluster data. One example of an API node is the ndb_restore utility that is used to restore a cluster backup. It is possible to write such applications using the NDB API.
** Let’s implement MySQL cluster practically -
In my case there are three(3) Storage Node ie (192.168.0.2 ,192.168.0.3 and 192.168.0.4), four(4) MySQL API server (192.168.0.1, 192.168.0.2, 192.168.0.3 and 192.168.0.4 ) and one Management Node (192.168.0.5)…
* Please follow these steps carefully you can easily set MySQL clustering.
1] Login to Management node here IP of MGM node is 192.168.0.5
* Create directory where data for Mgmd (Management node) is stored -
root@fileserver:~# mkdir /var/lib/mysql-cluster_MGM
root@fileserver:~#
root@fileserver:~# ll -d /var/lib/mysql-cluster_MGM
drwxr-xr-x 2 root root 4096 2008-01-05 11:22 /var/lib/mysql-cluster_MGM
root@fileserver:~#
* Make sure that the owner of data directory is MySQL user ie “mysql” -
root@fileserver:~# chown mysql:mysql -R /var/lib/mysql-cluster_MGM/
root@fileserver:~#
root@fileserver:~# ll -d /var/lib/mysql-cluster_MGM/
drwxr-xr-x 2 mysql mysql 4096 2008-01-05 11:22 /var/lib/mysql-cluster_MGM/
root@fileserver:~#
2] How to start Management Node
root@fileserver:/var/lib/mysql-cluster_MGM# ndb_mgmd –help
Usage: ndb_mgmd [OPTIONS]
MySQL distrib 5.0.21, for pc-linux-gnu (i486)
-?, –usage Display this help and exit.
-?, –help Display this help and exit.
-V, –version Output version information and exit.
-c, –ndb-connectstring=name
Set connect string for connecting to ndb_mgmd. Syntax:
“[nodeid=<id>;][host=]<hostname>[:<port>]“. Overides
specifying entries in NDB_CONNECTSTRING and Ndb.cfg
–ndb-mgmd-host=name
Set host and port for connecting to ndb_mgmd. Syntax:
<hostname>[:<port>].
–ndb-nodeid=# Set node id for this node.
–ndb-shm Allow optimizing using shared memory connections when
available
–ndb-optimized-node-selection
Select nodes for transactions in a more optimal way
-c, –connect-string=name
same as –ndb-connectstring
–core-file Write core on errors.
–character-sets-dir=name
Directory where character sets are.
-f, –config-file=name
Specify cluster configuration file
-P, –print-full-config
Print full config and exit
-d, –daemon Run ndb_mgmd in daemon mode (default)
–interactive Run interactive. Not supported but provided for testing
purposes
–no-nodeid-checks Do not provide any node id checks
–nodaemon Don’t run as daemon, but don’t read from stdin
–mycnf Read cluster config from my.cnf
Variables (–variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
——————————— —————————–
ndb-connectstring (No default value)
ndb-mgmd-host (No default value)
ndb-nodeid 0
ndb-shm FALSE
ndb-optimized-node-selection TRUE
connect-string (No default value)
core-file FALSE
character-sets-dir (No default value)
config-file (No default value)
print-full-config FALSE
daemon TRUE
interactive FALSE
no-nodeid-checks FALSE
nodaemon FALSE
mycnf FALSE
root@fileserver:/var/lib/mysql-cluster_MGM#
root@fileserver:/var/lib/mysql-cluster_MGM# ndb_mgmd -f /var/lib/mysql-cluster_MGM/config.conf
root@fileserver:/var/lib/mysql-cluster_MGM#
root@fileserver:/var/lib/mysql-cluster_MGM# netstat -nlp | grep ndb
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 24291/ndb_mgmd
root@fileserver:/var/lib/mysql-cluster_MGM#
root@fileserver:/var/lib/mysql-cluster_MGM# /etc/init.d/mysql-ndb
mysql-ndb mysql-ndb-mgm
root@fileserver:/var/lib/mysql-cluster_MGM# /etc/init.d/mysql-ndb-mgm
3] Managment Node command line utility -
root@fileserver:/var/lib/mysql-cluster_MGM# ndb_mgm
ndb_mgm ndb_mgmd
root@fileserver:/var/lib/mysql-cluster_MGM# ndb_mgm
– NDB Cluster — Management Client –
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
———————
[ndbd(NDB)] 3 node(s)
id=2 (not connected, accepting connect from 192.168.0.2)
id=3 (not connected, accepting connect from 192.168.0.3)
id=4 (not connected, accepting connect from 192.168.0.4)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.5 (Version: 5.0.21)
[mysqld(API)] 4 node(s)
id=5 (not connected, accepting connect from 192.168.0.1)
id=6 (not connected, accepting connect from 192.168.0.2)
id=7 (not connected, accepting connect from 192.168.0.3)
id=8 (not connected, accepting connect from 192.168.0.4)
ndb_mgm>
4] Login to Storage Node 192.168.0.2 and edit my.cnf file -
* Make changes to my.cnf file as shown below -
root@asterisk:/etc/mysql# tail /etc/mysql/my.cnf
# The following configuration is read by the ndbd storage daemons,
# not from the ndb_mgmd management daemon.
#
#################################
#Use this MySQL server as NDB storage/Data node
#MGM node IP is - 192.168.0.5
[MYSQL_CLUSTER]
ndb-connectstring=192.168.0.5
root@asterisk:/etc/mysql#
root@asterisk:/etc/mysql# /etc/init.d/mysql restart
Stopping MySQL database server: mysqld.
Starting MySQL database server: mysqld.
root@asterisk:/etc/mysql# netstat -nlp | grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 19518/mysqld
root@asterisk:/etc/mysql#
root@asterisk:/etc/mysql# ndbd –help
Usage: ndbd [OPTIONS]
MySQL distrib 5.0.22, for pc-linux-gnu (i486)
-?, –usage Display this help and exit.
-?, –help Display this help and exit.
-V, –version Output version information and exit.
-c, –ndb-connectstring=name
Set connect string for connecting to ndb_mgmd. Syntax:
“[nodeid=<id>;][host=]<hostname>[:<port>]“. Overides
specifying entries in NDB_CONNECTSTRING and Ndb.cfg
–ndb-mgmd-host=name
Set host and port for connecting to ndb_mgmd. Syntax:
<hostname>[:<port>].
–ndb-nodeid=# Set node id for this node.
–ndb-shm Allow optimizing using shared memory connections when
available
–ndb-optimized-node-selection
Select nodes for transactions in a more optimal way
-c, –connect-string=name
same as –ndb-connectstring
–core-file Write core on errors.
–character-sets-dir=name
Directory where character sets are.
–initial Perform initial start of ndbd, including cleaning the
file system. Consult documentation before using this
-n, –nostart Don’t start ndbd immediately. Ndbd will await command
from ndb_mgmd
-d, –daemon Start ndbd as daemon (default)
–nodaemon Do not start ndbd as daemon, provided for testing
purposes
–foreground Run real ndbd in foreground, provided for debugging
purposes (implies –nodaemon)
Variables (–variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
——————————— —————————–
ndb-connectstring 192.168.0.5
ndb-mgmd-host (No default value)
ndb-nodeid 0
ndb-shm FALSE
ndb-optimized-node-selection TRUE
connect-string 192.168.0.5
core-file FALSE
character-sets-dir (No default value)
initial FALSE
nostart FALSE
daemon TRUE
nodaemon FALSE
foreground FALSE
root@asterisk:/etc/mysql#
* Data on Storage node is stored in ” /var/lib/mysql-cluster” directory. The MySQL user ie “mysql” need to be owner of this directory.
root@asterisk:/etc/mysql# ll -d /var/lib/mysql-cluster/
drwxr-xr-x 2 root root 4096 2006-06-16 17:38 /var/lib/mysql-cluster/
root@asterisk:/etc/mysql#
root@asterisk:/etc/mysql# chown mysql:mysql -R /var/lib/mysql-cluster/
root@asterisk:/etc/mysql# ll -d /var/lib/mysql-cluster/
drwxr-xr-x 2 mysql mysql 4096 2006-06-16 17:38 /var/lib/mysql-cluster/
root@asterisk:/etc/mysql#
** How to start Storage or Data node first time -
root@asterisk:/etc/mysql# ndbd –initial
…
….
root@asterisk:/etc/mysql#
Note - please use “–initial” while starting storage or data node first time
root@asterisk:/etc/mysql# netstat -nlp | grep ndb
tcp 0 0 192.168.0.2:38321 0.0.0.0:* LISTEN 19686/ndbd
tcp 0 0 192.168.0.2:43001 0.0.0.0:* LISTEN 19686/ndbd
tcp 0 0 192.168.0.2:41115 0.0.0.0:* LISTEN 19686/ndbd
tcp 0 0 192.168.0.2:54651 0.0.0.0:* LISTEN 19686/ndbd
tcp 0 0 192.168.0.2:55003 0.0.0.0:* LISTEN 19686/ndbd
tcp 0 0 192.168.0.2:59453 0.0.0.0:* LISTEN 19686/ndbd
root@asterisk:/etc/mysql#
NOTE :: Please follow the same steps as mention here for other storage node ie 192.168.0.3 and 192.168.0.4.
5] Goto Managment node and check whether 192.168.0.2 Storage node is activated or not
root@fileserver:/var/lib/mysql-cluster_MGM# ndb_mgm
– NDB Cluster — Management Client –
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
———————
[ndbd(NDB)] 3 node(s)
id=2 @192.168.0.2 (Version: 5.0.22, starting, Nodegroup: 0)
id=3 (not connected, accepting connect from 192.168.0.3)
id=4 (not connected, accepting connect from 192.168.0.4)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.5 (Version: 5.0.21)
[mysqld(API)] 4 node(s)
id=5 (not connected, accepting connect from 192.168.0.1)
id=6 (not connected, accepting connect from 192.168.0.2)
id=7 (not connected, accepting connect from 192.168.0.3)
id=8 (not connected, accepting connect from 192.168.0.4)
ndb_mgm>
6] Now login to server 192.168.0.4 and configure mysql server as MySQL Node (API) -
root@linserver:~# vi /etc/mysql/my.cnf
[mysqld]
#
#setting for SQL-node of MySQL cluser
#
###########################################
#Use this MySQL server as NDB storage/Data node
#MGM node IP is - 192.168.0.5
#set Table/engine type to NDB
ndbcluster
#set MGM IP address
ndb-connectstring=192.168.0.5
root@linserver:~#
7] Goto Managment node and check whether 192.168.0.4 as MySQL node is activated or not -
root@fileserver:/var/lib/mysql-cluster_MGM# ndb_mgm
– NDB Cluster — Management Client –
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
———————
[ndbd(NDB)] 3 node(s)
id=2 @192.168.0.2 (Version: 5.0.22, starting, Nodegroup: 0)
id=3 @192.168.0.3 (Version: 5.0.21, starting, Nodegroup: 0)
id=4 (not connected, accepting connect from 192.168.0.4)
[ndb_mgmd(MGM)] 1 node(s)
id=1 (Version: 5.0.21)
[mysqld(API)] 4 node(s)
id=5 (not connected, accepting connect from 192.168.0.1)
id=6 (not connected, accepting connect from 192.168.0.2)
id=7 (not connected, accepting connect from 192.168.0.3)
id=8 (not connected, accepting connect from 192.168.0.4)
ndb_mgm>
** Now check that MySQL API node 192.168.0.4 is activated
ndb_mgm> show
Cluster Configuration
———————
[ndbd(NDB)] 3 node(s)
id=2 @192.168.0.2 (Version: 5.0.22, Nodegroup: 0, Master)
id=3 @192.168.0.3 (Version: 5.0.21, Nodegroup: 0)
id=4 @192.168.0.4 (Version: 5.0.22, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.5 (Version: 5.0.21)
[mysqld(API)] 4 node(s)
id=5 (not connected, accepting connect from 192.168.0.1)
id=6 (not connected, accepting connect from 192.168.0.2)
id=7 (not connected, accepting connect from 192.168.0.3)
id=8 @192.168.0.4 (Version: 5.0.22)
ndb_mgm>
NOTE:: Please follow the same steps as mention here for other MySQL API node ie 192.168.0.1, 192.168.0.2 and 192.168.0.3
** How to check/use MySQL Clustering -
8] Login to MySQL server 192.168.0.4 and follow the process
root@linserver:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 708 to server version: 5.0.22-Debian_0ubuntu6.06.3-log
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> create database arunbagul;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| arunbagul |
+——————–+
2 rows in set (0.00 sec)
mysql>
mysql> use arunbagul;
Database changed
mysql> CREATE TABLE cluster_tab (rollno INT NOT NULL , name varchar(255)) ENGINE=’NDBCLUSTER’;
Query OK, 0 rows affected (0.38 sec)
mysql>
mysql> SHOW TABLES;
+———————+
| Tables_in_arunbagul |
+———————+
| cluster_tab |
+———————+
1 row in set (0.00 sec)
mysql>
mysql> INSERT INTO cluster_tab VALUES ( 1 , ‘Arun Bagul’ ) ;
Query OK, 1 row affected (0.10 sec)
mysql> INSERT INTO cluster_tab VALUES ( 2 , ‘Yogesh Nikam’ );
Query OK, 1 row affected (0.07 sec)
mysql> INSERT INTO cluster_tab VALUES ( 3 , ‘Ketan Mayangar’ );
Query OK, 1 row affected (0.01 sec)
mysql> select * from cluster_tab;
+——–+—————-+
| rollno | name |
+——–+—————-+
| 2 | Yogesh Nikam |
| 3 | Ketan Mayangar |
| 1 | Arun Bagul |
+——–+—————-+
3 rows in set (0.16 sec)
mysql>
9] Now login to MySQL server of 192.168.0.3 which is acting as MySQL node (API).
root@proxy:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12 to server version: 5.0.21-Debian_3ubuntu1-log
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql>
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| auth |
| mysql |
+——————–+
7 rows in set (0.00 sec)
mysql>
NOTE :: See out put of above query. It’s not showning the “arunbagul” DB which is clustered environment. Please Remember that you need to create DB on each MySQL API node, for DB which is in MySQL cluster! Then after the Clustered DB can be accessed from all MySQL API. All the tables etc are also available….
mysql> create database arunbagul;
Query OK, 1 row affected (0.07 sec)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| arunbagul |
| auth |
| mysql |
+——————–+
8 rows in set (0.00 sec)
mysql> use arunbagul ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show tables;
+———————+
| Tables_in_arunbagul |
+———————+
| cluster_tab |
+———————+
1 row in set (0.01 sec)
mysql> select * from cluster_tab;
+——–+—————-+
| rollno | name |
+——–+—————-+
| 1 | Arun Bagul |
| 3 | Ketan Mayangar |
| 2 | Yogesh Nikam |
+——–+—————-+
3 rows in set (0.04 sec)
mysql> INSERT INTO cluster_tab VALUES ( 4 , ‘Nishit Shah’ );
Query OK, 1 row affected (0.10 sec)
mysql> quit
Bye
root@proxy:~#
10] Now login to MySQL server 192.168.0.4 which is MySQL API node.
root@linserver:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 800 to server version: 5.0.22-Debian_0ubuntu6.06.3-log
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> select * from cluster_tab;
+——–+—————-+
| rollno | name |
+——–+—————-+
| 2 | Yogesh Nikam |
| 3 | Ketan Mayangar |
| 1 | Arun Bagul |
| 4 | Nishit Shah |
+——–+—————-+
4 rows in set (0.12 sec)
mysql>
11] Now login to MySQL Managment node.. and check the MySQL Cluster;
root@fileserver:/home# ndb_mgm
– NDB Cluster — Management Client –
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
———————
[ndbd(NDB)] 3 node(s)
id=2 @192.168.0.2 (Version: 5.0.22, Nodegroup: 0, Master)
id=3 @192.168.0.3 (Version: 5.0.21, Nodegroup: 0)
id=4 @192.168.0.4 (Version: 5.0.22, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.5 (Version: 5.0.21)
[mysqld(API)] 4 node(s)
id=5 (not connected, accepting connect from 192.168.0.1)
id=6 (not connected, accepting connect from 192.168.0.2)
id=7 @192.168.0.3 (Version: 5.0.21)
id=8 @192.168.0.4 (Version: 5.0.22)
ndb_mgm>
12] How to start/stop, restart MySQL Cluster -
** Login to MGM node and then start MGMD node as…
root@fileserver:~# ndb_mgmd -f /var/lib/mysql-cluster_MGM/config.conf
root@fileserver:~# ndb_mgm
– NDB Cluster — Management Client –
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
———————
[ndbd(NDB)] 3 node(s)
id=2 (not connected, accepting connect from 192.168.0.2)
id=3 (not connected, accepting connect from 192.168.0.3)
id=4 (not connected, accepting connect from 192.168.0.4)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.5 (Version: 5.0.21)
[mysqld(API)] 4 node(s)
id=5 (not connected, accepting connect from 192.168.0.1)
id=6 (not connected, accepting connect from 192.168.0.2)
id=7 (not connected, accepting connect from 192.168.0.3)
id=8 (not connected, accepting connect from 192.168.0.4)
ndb_mgm>
*** How to start NDBD Node from MGM interface..
ndb_mgm> 2 STATUS
Node 2: not connected
ndb_mgm> 2 START
Start failed.
* 22: Error
* No contact with the process (dead ?).
ndb_mgm>
That’s it!! MySQL clustering is working…
Thank you,
Arun Bagul