Popular Posts

Thursday, 4 April 2013

MySQL for Beigners

This is a list of handy MySQL commands that I use time and time again. At the bottom are statements, clauses, and functions you can use in MySQL. Below that are PHP and Perl API functions you can use to interface with MySQL. To use those you will need to build PHP with MySQL functionality. To use MySQL with Perl you will need to use the Perl modules DBI and DBD::mysql.
Below when you see # it means from the unix shell. When you see mysql> it means from a MySQL prompt after logging into MySQL.

To login (from unix shell) use -h only if needed.

# [mysql dir]/bin/mysql -h hostname -u root -p

Create a database on the sql server.

mysql> create database [databasename];

List all databases on the sql server.

mysql> show databases;

Switch to a database.

mysql> use [db name];

To see all the tables in the db.

mysql> show tables;

To see database's field formats.

mysql> describe [table name];

To delete a db.

mysql> drop database [database name];

To delete a table.

mysql> drop table [table name];

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];

Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Show all records containing the name "Bob" AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

Show all records starting with the letters 'bob' AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

Tuesday, 7 August 2012

Basic Command While using Postfix Mail Server

Explain the working of local mail submission for postfix?
When a local email message enters the postfix system. Local messages are deposited into the maildrop directory of the Postfix queue by the postdrop command, usually through the sendmail compatibility program. The pickup daemon reads the message from the queue and feeds it to the cleanup daemon. The cleanup daemon processes all inbound mail and notifies the queue manager after it has placed the cleaned-up message into the incoming queue. The queue manager then invokes the appropriate delivery agent to send the message to its next hop or ultimate destination.

What are the important files for postfix server ?

/etc/postfix/main.cf
/etc/postfix/access
/etc/postfix/aliases

Which command checks for configuration problems?
# postfix check

How you will see the queue of postfix server?
#postqueue -p 
or mailq

How can I clear postfix mail server queue?

# postsuper -d ALL

How you will reload the postfix queue?
# postsuper -r ALL

which command is used to find out that postfix is complied with mysql or not?

# postconf -m
nis
regexp
environ
mysql
btree
unix
hash


Explain smtpd_recipient_limit parameter? And what is the default value for this parameter?
The smtpd_recipient_limit parameter can limit the number of recipients allowed in a single incoming message.
The default value for this parameter is 1000.

Explain smtpd_timeout Parameter?

The smtpd_timeout parameter limits the amount of time Postfix waits for an SMTP client request after sending a response. This allows the Postfix administrator to quickly disconnect SMTP servers that "camp out" on the SMTP connection, utilizing system resources for the SMTP connection without actually sending a message.
smtpd_timeout = value
By default, Postfix will assume the value is in seconds.

Explain maximal_queue_lifetime Parameter?
The maximal_queue_lifetime parameter sets the amount of time (in days) that a message remains in the deferred message queue before being returned as undeliverable. The default value is 5 days. Once this value is reached, Postfix returns the message to the sender.

Explain queue_run_delay Parameter?
The queue_run_delay parameter sets the time interval (in seconds) that Postfix scans the deferred message queue for messages to be delivered. The default value for this is 1,000 seconds.

Explain default_destination_concurrency_limit Parameter? The default_destination_concurrency_limit parameter defines the maximum number of concurrent SMTP sessions that can be established with any remote host. This parameter is related to the SMTP maxprocess parameter in the master.cf configuration file. The maximum number of concurrent SMTP sessions cannot exceed the maxprocess value set for the maximum number of SMTP client processes. Thus, if the default maxprocess value of 50 is used, setting the default_destination_concurrency_limit greater than 50 has no effect.

How to scan newly added LUN using rescan-scsi-bus.sh ?

ENV : RHEL 5.4 and later

I suggest you NOT to scan the existing LUNs since I/O operations are still in use and if you scan them it will/may corrupt the file system. So, I always suggest you to scan the new added device or storage. Once you add it, HBA will detect the device and then you can scan this non-existent LUNs to the HBA. As an example you can execute the command like :

---
#rescan-scsi-bus.sh --hosts=1 --luns=2
---

Note : I assume that on host 1/or on HBA 1, lun 2 doesn't exist.

For more details please get help from :

---
#rescan-scsi-bus.s --help

http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/Online_Storage_Reconfiguration_Guide/rescan-scsi-bus.html
---

Try :)

How to list out number of files inside each directory?

I developed following script and used it :

---
[root@vijay log]# cat ./find_large_small_files.sh
#!/bin/bash
find . -type d -exec ls -ld {} \; &> ./tmpfile
let i=0
for i in `cat ./tmpfile`
do
echo "Directory $i has following no of files : `ls -al $i|wc -l`"
done

Try and modify as per your need. 

what does it mean by "cman expected_votes="1" two_node="1" in cluster.conf ?

For two node clusters ordinarily, the loss of quorum after one out of two nodes fails will prevent the remaining node from continuing (if both nodes have one vote.) Special configuration options can be set to allow the one remaining node to continue operating if the other fails. To do this only two nodes, each with one vote, can be defined in cluster.conf. The two_node and expected_votes values must then be set to 1 in the cman section as follows.

---
cman two_node="1" expected_votes="1"

Basic Idea about GFS file system!

Global File System (GFS) is a shared disk file system for Linux computer clusters. It can maximize the benefits of clustering and minimize the costs.

It does following :

# Greatly simplify your data infrastructure
# Install and patch applications once, for the entire cluster
# Reduce the need for redundant copies of data
# Simplify back-up and disaster recovery tasks
# Maximize use of storage resources and minimize your storage costs
# Manage your storage capacity as a whole vs. by partition
# Decrease your overall storage needs by reducing data duplication
# Scale clusters seamlessly, adding storage or servers on the fly
# No more partitioning storage with complicated techniques
# Add servers simply by mounting them to a common file system
# Achieve maximum application uptime

While a GFS file system may be used outside of LVM, Red Hat supports only GFS file systems that are created on a CLVM logical volume. CLVM is a cluster-wide implementation of LVM, enabled by the CLVM daemon clvmd, which manages LVM logical volumes in a Red Hat Cluster Suite cluster. The daemon makes it possible to use LVM2 to manage logical volumes across a cluster, allowing all nodes in the cluster to share the logical volumes. Red Hat supports it if gfs is deployed on LVM

GULM (Grand Unified Lock Manager) is not supported in Red Hat Enterprise Linux 5. If your GFS file systems use the GULM lock manager, you must convert the file systems to use the DLM lock manager. This is a two-part process.

* While running Red Hat Enterprise Linux 4, convert your GFS file systems to use the DLM lock manager.
* Upgrade your operating system to Red Hat Enterprise Linux 5, converting the lock manager to DLM when you do.

“GFS with a SAN” provides superior file performance for shared files and file systems. Linux applications run directly on GFS nodes. Without file protocols or storage servers to slow data access, performance is similar to individual Linux servers with directly connected storage; yet, each GFS application node has equal access to all data files. GFS supports up to 125 GFS nodes.

GFS Software Components :

gfs.ko : Kernel module that implements the GFS file system and is loaded on GFS cluster nodes.
lock_dlm.ko : A lock module that implements DLM locking for GFS. It plugs into the lock harness, lock_harness.ko and communicates with the DLM lock manager in Red Hat Cluster Suite.
lock_nolock.ko : A lock module for use when GFS is used as a local file system only. It plugs into the lock harness, lock_harness.ko and provides local locking.

The system clocks in GFS nodes must be within a few minutes of each other to prevent unnecessary inode time-stamp updating. Unnecessary inode time-stamp updating severely impacts cluster performance. Need to use ntpd for accurate time with time server.

A) (On shared disk ) : Create GFS file systems on logical volumes created in Step 1. Choose a unique name for each file system. For more information about creating a GFS file system, refer to Section 3.1, “Creating a File System”.
You can use either of the following formats to create a clustered GFS file system:
Initial Setup Tasks:

1. Setting up logical volumes
2. Making a GFS files system
3. Mounting file systems

#gfs_mkfs -p lock_dlm -t ClusterName:FSName -j NumberJournals BlockDevice
or #mkfs -t gfs -p lock_dlm -t LockTableName -j NumberJournals BlockDevice

OR

#gfs_mkfs -p LockProtoName -t LockTableName -j NumberJournals BlockDevice

#mkfs -t gfs -p LockProtoName -t LockTableName -j NumberJournals BlockDevice

B)At each node, mount the GFS file systems. For more information about mounting a GFS file system.

Command usage:

#mount BlockDevice MountPoint

mount -o acl BlockDevice MountPoint
The -o acl mount option allows manipulating file ACLs. If a file system is mounted without the -o acl mount option, users are allowed to view ACLs (with getfacl), but are not allowed to set them (with setfacl).

Note :

Make sure that you are very familiar with using the LockProtoName and LockTableName parameters. Improper use of the LockProtoName and LockTableName parameters may cause file system or lock space corruption.

LockProtoName :
Specifies the name of the locking protocol to use. The lock protocol for a cluster is lock_dlm. The lock protocol when GFS is acting as a local file system (one node only) is lock_nolock.
LockTableName: This parameter is specified for GFS file system in a cluster configuration. It has two parts separated by a colon (no spaces) as follows: ClusterName:FSName

* ClusterName, the name of the Red Hat cluster for which the GFS file system is being created.
* FSName, the file system name, can be 1 to 16 characters long, and the name must be unique among all file systems in the cluster.

NumberJournals:

Specifies the number of journals to be created by the gfs_mkfs command. One journal is required for each node that mounts the file system. (More journals than are needed can be specified at creation time to allow for future expansion.)

EXAMPLE :http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/Global_File_System/ch-manage.html

[root@me ~]# gfs_mkfs -p lock_dlm -t alpha:mydata1 -j 8 /dev/vg01/lvol0
[root@me ~]# gfs_mkfs -p lock_dlm -t alpha:mydata2 -j 8 /dev/vg01/lvol1

Before you can mount a GFS file system, the file system must exist , the volume where the file system exists must be activated, and the supporting clustering and locking systems must be started. After those requirements have been met, you can mount the GFS file system as you would any Linux file system.

EXAMPLE : mount /dev/vg01/lvol0 /mydata1

Displaying GFS Tunable Parameters : gfs_tool gettune MountPoint

try :)

Sunday, 10 June 2012

PXE-Boot on RHEL-6

. Use Yum utility to Install required rpm
#yum install dhcp tftp-server syslinux httpd nfs-utils system-config-kickstart bind-*

2. Configure Dns server for host name resolution

#cp -p /etc/named.* /var/named/chroot/etc

#cp -p /var/named.* /var/named/chroot/var/named


#rm -rf /etc/named.*

#rm -rf /var/named.*

#cd /var/named/chroot/etc

#vim named.conf
options {
listen-on port 53 { 127.0.0.1; 192.168.0.254; }; <= Define here the ip of dns
listen-on-v6 port 53 { ::1; };
directory
"/var/named";
dump-file
"/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { localhost; any; };
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone “example.com” IN {                 <= Here declare the forward zone
type master;
file “for.zone”;
};
zone “0.168.192.in-addr.arpa” IN {   <= Here declare reverse zone
type master;
file “rev.zone”;
}
:wq

#cd /var/named/chroot/var/named

#cp -p named.localhost f.zone

#cp -p named.loopback r.zone

#vim f.zone
$TTL 1D
@
IN SOA server1.example.com. root.server1.example.com. (
                                                   0;                      serial
                                                   1D;                   refresh
                                                   1H ;                  retry
                                                   1W;                   expire
                                                   3H ) ;                minimum
                     NS      server1.example.com.
server1         A        192.168.0.254
desktop1       A        192.168.0.1
desktop2       A        192.168.0.2
desktop3
:wq

#vim r.zone
$TTL @  IN SOA  server1.example.com. server1.example.com. (
                                                    0;               serial
                                                    1D;            refresh
                                                    1H ;           retry
                                                    1W;           expire
                                                    3H ) ;        minimum
                             NS     server1.example.com.
254                      PTR    server1.example.com.
1                          PTR    desktop1.example.com.
2                          PTR    desktop2.example.com.
3                          PTR    desktop3.example.com.
:wq

#chkconfig named on ; /etc/init.d/named restart
2. Now Configure the dhcp server
#vim /etc/dhcp/dhcpd.conf
default-lease-time 600;
max-lease-time 7200;
allow booting;
allow bootp;
authoritative;
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.1 192.168.0.11;
next-server 192.168.0.254;          <= tftp Server ip
filename "pxelinux.0";
}
:wq

#chkconfig dhcpd on;/etc/init.d/dhcpd restart

3. Configure tftp server
#vim /etc/xinetd.d/tftp
disabled = no
:x

Mount RHEL6 OS dvd on /media directory & copy files required for tftp server
#mount /dev/scd0 /media

#cp -rv /media/isolinux/* /var/lib/tftpboot

#cp -rv /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot

#mkdir /var/lib/tftpboot/pxelinx.cfg

#cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

Make the OS dump
#cp -rv /media/* /var/www/html

#vim /var/lib/tftpboot/pxelinux.cfg/default
                                                                  <= Make this entery at bottom
label RHEL6-32.bit
menu label ^Install RHEL6-32.bit Unattended
menu defult
kernel vmlinuz
append initrd=initrd.img linux ks=http://192.168.0.254/ks.cfg
:wq
4. Create a ks file by the name of ks.cg, to make pxe unattended by using system-config-kickstart & save it in /var/www/html

5. Start the service xinetd, httpd & on the tftp server
#chkconfig xinetd on ; /etc/init.d/xinetd restart

#chkconfig tftp on

#chkconfig httpd on ; /etc/init.d/httpd restart

Now pxe is ready to Install single OS.