Saturday, August 13, 2016

MySQL Server

INTRODUCTION

Many of the applications that a Web developer wants to use can be made
easier by the use of a standardized database to store, organize, and
access information. MySQL is an Open Source (GPL) Standard Query
Language (SQL) database that is fast, reliable, easy to use, and suitable
for applications of any size. SQL is the ANSI-standard database query
language used by most databases (though all have their nonstandard
extensions).
MySQL can easily be integrated into Perl programs by using the Perl
DBI (DataBase Independent interface) module. DBI is an Application Program
Interface (API) that allows Perl to connect to and query a number of
SQL databases (among them MySQL, mSQL, PostgreSQL, Oracle, Sybase,
and Informix).
If you installed Linux as suggested in Chapter 2, MySQL and DBI are
already installed.

****************************************************************************
Install MySQL

yum install mysql-server

Configuration file for MySQL is /etc/my.cnf and Port Number : 3306

****************************************************************************
Start mysql service

service mysqld start    or    /etc/init.d/mysqld start

****************************************************************************
mysql -u root -p password          or     #mysql -u mydbadmin -p       

****************************************************************************
First, try to make a connection to our MySQL server as the root
MySQL user:

$ mysql -u root

If you see the following output:
ERROR 2002: Can’t connect to local MySQL server through socket
´/var/lib/mysql/mysql.sock´(2)
it likely means the MySQL server is not running. If your system is set up
securely, it shouldn’t be running, because you had no reason, before now,
for it to be running. Use chkconfig as root to make sure it starts the next
time the machine boots, and then start it by hand as follows:

# chkconfig mysqld on
# /etc/init.d/mysqld start

****************************************************************************
Set mysql password and secure mysql

[root@localhost ~]# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none):   enter
Set root password? [Y/n] Y
New password: ******
Re-enter new password:  ******
Password updated successfully!
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

****************************************************************************
What if i forgot the root PWD of mysql?  OR how to recover mysql root password?

1) #cd /usr/bin/    (from this path type CMD of mysqld_safe)
2) #/etc/init.d/mysqld stop  (before running below CMD we have to stop mysql )
3) #mysqld_safe --skip-grant-tables &    (grant tables allows you to do password less login into mysql)
4) #mysql -u root (by this CMD we will go to mysql CMD line)
5) >use mysql;
6) > update user set password=password("abc@123") where user='root'; 
 (This is the CMD by which you can change the PWD of the specific user)
7) > flush privileges;  (this CMD will syncall the data from RAM to HDD)
8) > quit;

****************************************************************************
The SHOW DATABASES and CREATE DATABASE Commands

First, we need to create the new database. Check the current databases to
make sure a database of that name doesn’t already exist; then create the
new one, and verify the existence of the new database:

mysql> SHOW DATABASES;

+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)


mysql> CREATE DATABASE people;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| people |
| test |
+----------+
3 rows in set (0.00 sec)


SQL commands and subcommands (in the previous example, CREATE is
a command; DATABASE is its subcommand) are case-insensitive. The name
of the database (and table and field) are case sensitive. It’s a matter of
style whether one uses uppercase or lowercase, but traditionally the SQL
commands are distinguished by uppercase.
One way to think of a database is as a container for related tables. A
table is a collection of rows, each row holding data

****************************************************************************
The USE Command

Before anything can be done with the newly created database, MySQL has
to connect to it. That’s done with the USE command:

mysql> USE people;

****************************************************************************
The CREATE TABLE and SHOW TABLES Commands

Each table within the database must be defined and created. This is done
with the CREATE TABLE command.
Create a table named age information to contain an individual’s first
name, last name, and age. MySQL needs to know what kind of data can
be stored in these fields. In this case, the first name and the last name are
character strings of up to 20 characters each, and the age is an integer:

mysql>CREATE TABLE age_information (lastname CHAR(20),firstname CHAR(20),age INT);
Query OK, 0 rows affected (0.00 sec)


mysql> CREATE TABLE  info (f_name VARCHAR(20),m_name VARCHAR(3),l_name VARCHAR(35),userid VARCHAR(15),username VARCHAR(8),email  VARCHAR(35),phone VARCHAR(25));
Query OK, 0 rows affected (0.06 sec)

It appears that the table was created properly (it says OK after all),
but this can be checked by executing the SHOW TABLES command. If an error
is made, the table can be removed with DROP TABLE.
When a database in MySQL is created, a directory is created with the

same name as the database (people, in this example):

[root@localhost ~]# ls -l /var/lib/mysql
total 20488
-rw-rw----. 1 mysql mysql     10485760 Sep  6 15:35 ibdata1
-rw-rw----. 1 mysql mysql       5242880 Sep  6 15:35 ib_logfile0
-rw-rw----. 1 mysql mysql       5242880 Sep  6 15:35 ib_logfile1
drwx------. 2 mysql mysql              4096 Sep  6 15:35 mysql
srwxrwxrwx. 1 mysql mysql                0 Sep  6 15:35 mysql.sock

drwx------. 2 mysql mysql        4096 Sep  6 16:16 people


Within that directory, each table is implemented with three files:

[root@localhost ~]# ls -l /var/lib/mysql/people
total 20
-rw-rw----. 1 mysql mysql       65 Sep  6 16:03 db.opt
-rw-rw----. 1 mysql mysql   8768 Sep  6 16:16 info.frm
-rw-rw----. 1 mysql mysql         0 Sep  6 16:16 info.MYD
-rw-rw----. 1 mysql mysql   1024 Sep  6 16:16 info.MYI


mysql> SHOW TABLES;
+------------------+
| Tables_in_people |
+------------------+
| age_information |
+------------------+
1 row in set (0.00 sec)

This example shows two MySQL datatypes: character strings and
integers. Other MySQL data types include several types of integers

(for a complete discussion of MySQL’s data types, see www.mysql.com/
documentation/mysql/bychapter/manual Reference.html#Column types):

TINYINT −128 to 127 (signed)
                   or 0 to 255 (unsigned)

SMALLINT −32768 to 32767 (signed)
                    or 0 to 65535 (unsigned)

MEDIUMINT −8388608 to 8388607 (signed)
                        or 0 to 16777215 (unsigned)

INTEGER −2147483648 to 2147483647 (signed)
                  (same as INT) or 0 to 4294967295 (unsigned)

BIGINT −9223372036854775808 to 9223372036854775807 (signed)
                 or 0 to 18446744073709551615 (unsigned)

and floating points:

FLOAT
DOUBLE
REAL (same as DOUBLE)
DECIMAL
NUMERIC (same as DECIMAL)


There are several data types to represent a date:

DATE                  YYYY-MM-DD
DATETIME         YYYY-MM-DD HH:MM:SS
TIMESTAMP       YYYYMMDDHHMMSS
                              or YYMMDDHHMMSS
                               or YYYYMMDD or YYMMDD
TIME                      HH:MM:SS
YEAR                     YYYY or YY

The table age information used the CHAR character data type. The following
are the other character data types. Several have BLOB in their name—
a BLOB is a Binary Large OBject that can hold a variable amount of data.
The types with TEXT in their name are just like their corresponding BLOBs

except when matching is involved: The BLOBs are case-sensitive, and the
TEXTs are case-insensitive.

VARCHAR      variable-length string up to 255 characters

TINYBLOB      maximum length 255 characters
TINYTEXT

BLOB                 maximum length 65535 characters
TEXT

MEDIUMBLOB maximum length 16777215 characters
MEDIUMTEXT

LONGBLOB       maximum length 4294967295 characters
LONGTEXT

****************************************************************************
The DESCRIBE Command

The DESCRIBE command gives information about the fields in a table. The
fields created earlier—lastname, firstname, and age—appear to have been
created correctly

mysql> describe [table name];

mysql> DESCRIBE age;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| f_name     | varchar(20) | YES  |     | NULL    |       |
| m_name   | varchar(3)  | YES  |      | NULL    |       |
| l_name     | varchar(35) | YES  |     | NULL    |       |
| userid       | varchar(15) | YES  |     | NULL    |       |
| username  | varchar(8)  | YES  |      | NULL    |       |
| email        | varchar(35) | YES  |     | NULL    |       |
| phone       | varchar(25) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> DESCRIBE age_information;
+-----------+----------+------+-----+---------+-------+
| Field     | Type     | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| lastname  | char(20) | YES  |     | NULL    |       |
| firstname | char(20) | YES  |     | NULL    |       |
| age           | int(11)   | YES  |     | NULL    |       |
+-----------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

The command SHOW COLUMNS FROM age information; gives the same information
as DESCRIBE age information; but DESCRIBE involves less typing.
(If you’re really trying to save keystrokes, you could abbreviate DESCRIBE
as DESC.)

****************************************************************************
To delete a database.

mysql> drop database [database name];

***************************************************************************
The INSERT Command

For the table to be useful, we need to add information to it. We do so with
the INSERT command:

mysql>INSERT INTO age_information (lastname, firstname, age) VALUES ('Wall', 'Larry','46');
Query OK, 1 row affected (0.00 sec)


mysql> INSERT INTO info (f_name,m_name,l_name,userid,username,email,phone) values ('sachin','M','Walunj','109','rwalunj','rwalunj@gmail.com','123456789');

SELECT * FROM info WHERE f_name = "sachin";

The syntax of the command is INSERT INTO, followed by the table in
which to insert, a list within parentheses of the fields into which information
is to be inserted, and the qualifier VALUES followed by the list of values
in parentheses in the same order as the respective fields.1

****************************************************************************
The SELECT Command

SELECT selects records from the database. When this command is executed
from the command line, MySQL prints all the records that match the query.
The simplest use of SELECT is shown in this example:

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall        | Larry     |    46 |
+----------+-----------+------+
1 row in set (0.00 sec)


mysql> SELECT * FROM info;
+--------+--------+--------+--------+----------+-------------------+-----------+
| f_name | m_name | l_name | userid | username | email             | phone     |
+--------+--------+--------+--------+----------+-------------------+-----------+
| sachin | M      | Walunj | 109    | rwalunj  | rwalunj@gmail.com | 123456789 |
+--------+--------+--------+--------+----------+-------------------+-----------+
1 row in set (0.00 sec)

The * means “show values for all fields in the table”; FROM specifies the
table from which to extract the information.
The previous output shows that the record for Larry Wall was added
successfully. To experiment with the SELECT command, we need to add a
few more records, just to make things interesting:

INSERT INTO age_information (lastname, firstname, age) VALUES ('Torvalds', 'Linus','31');
Query OK, 1 row affected (0.00 sec)


INSERT INTO age_information (lastname, firstname, age) VALUES ('aymond', 'Eric','40');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   46 |
| Torvalds | Linus     |   31 |
| aymond   | Eric      |   40 |
+----------+-----------+------+
3 rows in set (0.00 sec)

1We did extensive research to determine that none of the names used in this chapter belong to
real people

There are many ways to use the SELECT command—it’s very flexible.
First, sort the table based on lastname:

mysql> SELECT * FROM age_information ORDER BY lastname;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| aymond   | Eric      |   40 |
| Torvalds | Linus     |   31 |
| Wall     | Larry     |   46 |
+----------+-----------+------+
3 rows in set (0.00 sec)

Now show only the lastname field, sorted by lastname:

mysql> SELECT lastname FROM age_information ORDER BY lastname;
+----------+
| lastname |
+----------+
| aymond   |
| Torvalds |
| Wall     |
+----------+
3 rows in set (0.00 sec)

Show the ages in descending order:

mysql> SELECT age FROM age_information ORDER BY age DESC;
+------+
| age  |
+------+
|   46 |
|   40 |
|   31 |
+------+
3 rows in set (0.00 sec);

Show all the last names for those who are older than 35:

mysql> SELECT lastname FROM age_information WHERE age > 35;
+----------+
| lastname |
+----------+
| Wall     |
| aymond   |
+----------+
2 rows in set (0.00 sec)


Do the same, but sort by lastname:

mysql> SELECT lastname FROM age_information WHERE age > 35 ORDER BY lastname;
+----------+
| lastname |
+----------+
| aymond   |
| Wall     |
+----------+
2 rows in set (0.00 sec)

****************************************************************************
The UPDATE Command

Since the database is about people, information in it can change (people
are unpredictable like that). For instance, although a person’s birthday is
static, their age changes. To change the value in an existing record,we can
UPDATE the table. Let’s say the fictional Larry Wall has turned 47:

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   46 |
| Torvalds | Linus     |   31 |
| aymond   | Eric      |   40 |
+----------+-----------+------+
3 rows in set (0.00 sec)

mysql> UPDATE age_information SET age = 47 WHERE lastname = 'Wall';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   47 |
| Torvalds | Linus     |   31 |
| aymond   | Eric      |   40 |
+----------+-----------+------+
3 rows in set (0.00 sec)

Be sure to use that WHERE clause; otherwise, if we had only entered UPDATE
age information SET age = 47, all the records in the database would have

been given the age of 47!

Although this might be good news for some people in these records
(how often have the old-timers said “Oh, to be 47 years old again”—OK,
probably not), it might be shocking news to others.
      This method works, but it requires the database to know that Larry is
46, turning 47. Instead of keeping track of this, for Larry’s next birthday
we simply increment his age:

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   47 |
| Torvalds | Linus     |   31 |
| aymond   | Eric      |   40 |
+----------+-----------+------+
3 rows in set (0.00 sec)

mysql> UPDATE age_information SET age = age + 1 WHERE lastname = 'Wall';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   48 |
| Torvalds | Linus     |   31 |
| aymond   | Eric      |   40 |
+----------+-----------+------+

3 rows in set (0.00 sec)


update info set l_name = 'Patil' where f_name = 'sachin';
update info set userid = '100' where phone = '123456789';

mysql> SELECT * FROM info WHERE f_name != "sachin" order by userid;

****************************************************************************
The DELETE Command

Sometimes we need to delete a record from the table (don’t assume the
worst—perhaps the person just asked to be removed from a mailing list,
which was opt-in in the first place, of course). This is done with the DELETE
command:


Delete Multiple Queries,

mysql>select concat('CALL mysql.rds_kill(',id,');') from information_schema.processlist where user='user name';


mysql> drop table [table name];

mysql> DELETE FROM age_information WHERE lastname = 'aymond';
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   48 |
| Torvalds | Linus     |   31 |
+----------+-----------+------+
2 rows in set (0.00 sec)

Eric is in good company here, so put him back:



****************************************************************************

mysql> INSERT INTO age_information (lastname, firstname, age) VALUES ('Raymond', 'Eric', 40);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM age_information;
+----------+-----------+------+
| lastname | firstname | age  |
+----------+-----------+------+
| Wall     | Larry     |   48 |
| Torvalds | Linus     |   31 |
| Raymond  | Eric      |   40 |
+----------+-----------+------+
3 rows in set (0.00 sec)

****************************************************************************
To  show full process list

mysql> show full processlist;
mysql> show processlist; 

****************************************************************************
check the status of the slave


mysql> show slave status\G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.122.1
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 286
               Relay_Log_File: mysqld-relay-bin.000008
                Relay_Log_Pos: 431
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 286
              Relay_Log_Space: 732
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 

1 row in set (0.00 sec)


****************************************************************************
Give Privileges

mysql> FLUSH PRIVILEGES;


***************************************************************************
Show all data in a table.

mysql> SELECT * FROM [table name];

mysql> SELECT * FROM information_schema.user_privileges;


***************************************************************************
mysql> GRANT permission ON database.table TO 'user'@'localhost' IDENTIFIED BY 'mypass';;

    ALL – Allow complete access to a specific database. If a database is not specified, then allow complete access to the entirety of MySQL.
    CREATE – Allow a user to create databases and tables.
    DELETE – Allow a user to delete rows from a table.
    DROP – Allow a user to drop databases and tables.
    EXECUTE – Allow a user to execute stored routines.
    GRANT OPTION – Allow a user to grant or remove another user’s privileges.
    INSERT – Allow a user to insert rows from a table.
    SELECT – Allow a user to select data from a database.
    SHOW DATABASES- Allow a user to view a list of all databases.

    UPDATE – Allow a user to update rows in a table.

****************************************************************************
Create password for root user

mysqladmin -u root password “newpassword”

mysqladmin -u root -h host_name password “newpassword”

****************************************************************************
Create user for client  

CREATE USER 'dev'@'192.168.x.x' IDENTIFIED BY 'dev123';

***************************************************************************
check io status on mysql 

#mysql -u root -p xxxxx

#show slave status \G;

#check Slave_IO_Running: Yes or not

#mysql> show full processlist \G;   ------ check full process list

****************************************************************************
LOADING AND DUMPING A DATABASE

We can load a database or otherwise execute SQL commands from a file.We
simply put the commands or database into a file—let’s call it mystuff.sql—
and load it in with this command:

$ mysql people < mystuff.sql

We can also dump out a database into a file with this command:

$ mysqldump people > entiredb.sql

For fun, try the mysqldump command with the people database (a gentle
reminder: the password is LampIsCool):

$ mysqldump -uapache -p people
Enter password:

$ mysqldump -uapache -p people  > apache.sql
Enter password:

Notice that this outputs all the SQL needed to create the table and
insert all the current records. For more information, see man mysqldump.



****************************************************************************
SUMMARY

MySQL is a powerful, sophisticated, and easy-to-use SQL database program.
Using Perl and DBI, one can easily create programs to automate
database management tasks. With this knowledge, the prospective web designer
should be able to construct a database-based (for lack of a better
term) web site that is portable, sophisticated, easy to manage, and professional
appearing. We have examined only a small subset of all that MySQL
provides (our 80/20 rule in effect).

What is a sticky Bit and how to set it in Linux?



What is Sticky Bit?

Sticky Bit is used mainly on folders in order to avoid deletion of a folder and its content by other user though he is having write permissions. If Sticky bit is enabled on a folder, the folder is deleted by only owner of the folder and super user(root). This is a security measure to suppress deletion of critical folders where it is having full permissions by others.

Use chmod command to set Sticky Bit on Folder: /opt/dump/

Symbolic way:
 
chmod o+t /opt/dump/
or 
chmod +t /opt/dump/
 
Let me explain above command we are setting Sticky Bit(+t) to folder /opt/dump by using chmod command.
 
Numerical way:
 
chmod 1757 /opt/dump/
 
Here in 1757, 1 indicates Sticky Bit set, 7 for full permissions for owner, 5 for read and execute permissions for group, and ful permissions for others.
 
Checking if a folder is set with Sticky Bit or not?
 
Use ls –l to check if the x in others permissions field is replaced by t or T
 
For example: /opt/dump/ listing before and after Sticky Bit set

Before Sticky Bit set:

ls -l
 
total 8
 
drwxr-xr-x 2 root root 4096 Oct 11 20:01 dump




After Sticky Bit set:

ls -l
 
total 8

drwxr-xr-t 2 root root 4096 Oct 11 19:45 dump

Removing Sticky bit

Symbolic way:

# chmod o-t /opt/dump
Or
# chmod –t /opt/dump

Numeric Way

# chmod 0755 /opt/dump

After Sticky Bit remove

drwxr-xr-x 2 root root 4.0K Oct 12 10:24 dump


du (disk usage) Command

The basic syntax for du is:

 du [options] [directories and/or files]

Commands

du -h

For display everything sorted by file size - # du –sk .[A-Z]* *|  sort –n

Display screenful output at a time as du generates more output than can fit on the console / screen:
Command - # du –h | less

To find top 3 directories, enter - # du –sk * | sort –nr | -3

Show summary in bytes - # du –b or du –ah

To get the last time modification - # du –ah –time

To arrange the output items according to size, du can be piped to the sort command, whose -n option tells it to list the output in numeric order with the smallest files first, as follows:
Command - # du | sort –n

As du will often generate more output than can fit on the monitor screen at one time, the output will fly by at high speed and be virtually unreadable. Fortunately, it is easy to display the output one screenful at a time by piping it to the less filter, for example,
Command - # du -h | less

The output of less can be advanced one screenful at a time by pressing the space bar, and it can be moved backward one screenful at a time by pressing the b key.
The output of du can likewise be piped to less after it has been passed through one or more other filters, for example,

Command - # du -h | sort -n | less

There are several other ways of monitoring disk space consumption and reporting file sizes. Although very useful tools, they are generally not good substitutes for du.
Among them is the df command, which is likewise used by system administrators to monitor disk usage. However, unlike du, it can only show the space consumption on entire partitions, and it lacks du's fine-grained ability to track the space usage of individual directories and files.
du is not designed to show the space consumption of partitions. The closest that it can come is to show the sizes of the first tier of directories in the root directory (i.e., the directory which contains all other directories and which is represented by a forward slash), several of which may be on their own partitions (depending on how the system has been set up). This is accomplished by becoming the root user and issuing the following

command:- #  du -h --max-depth=1 /

Linux CentOS 7 Commands

Edit Iptables

# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport [PORT_NUMBER_HERE] -j ACCEPT

…or edit firewalld

firewall-cmd --zone=public --add-port=[PORT_NUMBER_HERE]/tcp --permanent


Start/stop/restart Apache

systemctl stop httpd.service

systemctl start httpd.service

systemctl restart httpd.service

Check for running process

ps aux | grep [httpd|memcached|etc...]

Search for file

# find /search/path -iname *.conf


View files and logs

Browse a file

# more /path/to/file


Show last entry of a file

# tail /path/to/file -n 100


(use -n parameter for number of last rows)

Auto update entry from ex. log-file (use -f parameter)


# tail -f /var/log/httpd/error_log

SELinux

Turn on/off SELinux

# setenforce [0|1]


Turn on/off SELinux config

# setsebool -P httpd_can_network_connect_db=1


List all possible (or search) SELinux config parameters

# getsebool -a [| grep httpd]


Set webserver permissions for file upload with SELinux and user-groups

Edit SELinux

# setsebool –P allow_httpd_anon_write=1


# chcon -R -t public_content_rw_t /var/www/


Add new group

# groupadd www-data


Change group permissions on folder

# chgrp -R www-data /var/www/


Add user to group

# usermod apache --append --groups www-data


View users in group

# grep 'www-data' /etc/group

www-data:x:1000:apache,root

Edit a file

#vi /path/to/file


Push “a” to edit file

ESC + “:wq” for write and quit or just “:q” to quit.



JAVA Port Alerts

Troubleshoot java port alert

1) check running port

netstat -tnlp

ps aux|grep java

2) go to the java path  :  /opt/apache-tomcat-8.0.20

grep -ir port no .

grep -ir 8005 .


3) cd /opt/apache-tomcat-8.0.20/bin

4) start startup.sh

./startup.sh

Zip, Archive, Compress and Extract

Command

Zip :- When you compress file into zip format the keep original as it

To compress all file and directory in ZIP
# zip –r archive file name .
#zip –r archive file name foldername/*

To Compress single file in zip
# zip –r archive file name file name

To Extract file
                # unzip archive file name.zip

gzip :-

To compress all file and directory in ZIP
                # gzip file name  è output is file name.gz
                # gzip –c filename >filename.gz è to keep original file as it
                # gzip –r folder name è to compress folder in gzip format
               
To extract file/directory
                # gungip –r filr/directory name

tar:-

                -c è for Create Archive
                -x è for Extract the Archive
                -f è for archive with giving new filename
                -t è for displays or list in archive file
                -u è for archive & add to an existing archive file
                -v è for Display Verbose Information 
                -Z , -gzip, -unzip è Filter the archive through gzip

To archive file
                # tar –cf archive file name.tar file name
                # tar –cf archive file name.tar file name file name1 file name2 file name3 è for multiple files
                # tar –czf archive file name.tgz file nameè to archive in tar gz (gzip) format
# tar -cf home.tar home/ è       create a tar file named home.tar containing the home directory and place that file in the current director
                # tar –czf filename.tgz filename - - remove  Ã¨ to remove original file and make filename.tgz file

To Extract Fife
                # tar –xzvf filename.tgz è to extract tgz file
                # tar –xvf filename.tar è to extract simple tar file
                # tar – xvzf filename.tgz è to Extract file in special older

bzip

To compress file in .bz2 format 
# bzip2 filename
# bzip2 –c filename >archive file name.bz2 è to keep original file as it

To Extract file

                # bunzip2 archive file name.bz2

CRON – SCHEDULER

https://crontab.guru   --- check con job status 


[root@Tusharjahdav ~]# rpm -qa | grep cron  ------to check install package
cronie-1.4.4-12.el6.x86_64
cronie-anacron-1.4.4-12.el6.x86_64
crontabs-1.10-33.el6.noarch
[root@Tusharjahdav ~]# rpm -qa | grep -i cron
cronie-1.4.4-12.el6.x86_64
cronie-anacron-1.4.4-12.el6.x86_64
crontabs-1.10-33.el6.noarch

[root@Tusharjahdav ~]# /etc/init.d/crond start   ----start cron service
[root@Tusharjahdav ~]#  service crond start   ---- start cron service
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav ~]# vi bckup.sh   ------create backup.sh file

!/bin/bash
rsysc -aprv /usr/local/*  /opt/cron   ------script
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav ~]# vi H.sh

ync;    -----------------------------------------------------------------script
echo 0 > /proc/sys/vm/drop_caches;
sync;
echo 1 > /proc/sys/vm/drop_caches;
sync;
echo 2 > /proc/sys/vm/drop_caches;
sync;
echo 3 > /proc/sys/vm/drop_caches;
sync;
swapoff -av;
swapon -av;
free -m
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav ~]# chmod +X bckup.sh  -------provide permission
[root@Tusharjahdav ~]# chmod +X H.sh         -------provide permission
-----------------------------------------------------------------------------------------------------------------
To edit your crontab file, type the following command:
[root@Tusharjahdav ~]# $ crontab -e ----------------------------current user

[root@Tusharjahdav ~]# crontab –u harry –e  ---------------------------specific user

15 17 * * * /backup.sh

15 17 * * * /H.sh

[root@Tusharjahdav ~]# cd /var/spool/cron/   ------all cron job file stored in this path

[root@Tusharjahdav cron]# ls
Pranay  --------------------------------cron job file created as a user name

[root@Tusharjahdav cron]# cat pranay  ----to check cron job
15 17 * * * /backup.sh
15 17 * * * /H.sh
-----------------------------------------------------------------------------------------------------------------
How cron works

[root@Tusharjahdav cron]# /etc/init.d/crond start   -----start cron

[root@Tusharjahdav cron]# tail -f /var/log/cron   -------check cron log files

Jan  5 03:30:01 Tusharjahdav CROND[6335]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan  5 03:30:01 Tusharjahdav anacron[6023]: Job `cron.weekly' started
Jan  5 03:30:01 Tusharjahdav anacron[6023]: Job `cron.weekly' terminated
Jan  5 03:30:01 Tusharjahdav anacron[6023]: Normal exit (2 jobs run)
Jan  5 03:38:57 Tusharjahdav crontab[6345]: (root) BEGIN EDIT (pranay)
Jan  5 03:40:01 Tusharjahdav CROND[6350]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan  5 03:42:15 Tusharjahdav crontab[6345]: (root) REPLACE (pranay)
Jan  5 03:42:15 Tusharjahdav crontab[6345]: (root) END EDIT (pranay)
Jan  5 03:43:25 Tusharjahdav crontab[6353]: (root) BEGIN EDIT (pranay)
Jan  5 03:44:21 Tusharjahdav crontab[6353]: (root) END EDIT (pranay)
-----------------------------------------------------------------------------------------------------------------
[root@Tusharjahdav cron]# which crond  -----cron main file location
/usr/sbin/crond

#cron -----run every 1M set as default

Can we create in second cron job ---- No because cron by default set as a 1 Minutes

Note: If you want to set in second so we can used for a loop
-----------------------------------------------------------------------------------------------------------------
Syntax of crontab
Your cron job looks like as follows:

1 2 3 4 5 /path/to/command arg1 arg2

Where,
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31) ---Date
4: Month (0-12 [12 == December])
5: Day of the week (0-7 [7 or 0 == sunday])
/path/to/command - Script or command name to schedule
Same above five fields structure can be easily remembered with following
diagram:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)(Date )
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
18th of Friday -----night ----2:21    ---run job

21 02 18 * 05    /backup.sh   |  00 20 * * 3  ---run every Wednesday
-----------------------------------------------------------------------------------------------------------------

Every new year we can run job

00 00 01 01 *  /backup.sh
-----------------------------------------------------------------------------------------------------------------
We can set also

@year    /backup.sh
@mingnigh   /backup.sh
@reboot     /backup.sh
-----------------------------------------------------------------------------------------------------------------
[root@KH_salessvr_4 cron]# tail -f root
0 * * * * /usr/sbin/ntpdate -u time.nist.gov > /dev/null 2>&1 ; /sbin/hwclock --               systohc

0 23 * * * /opt/DatabaseBackUP/databaseBackup.sh

[root@KH_salessvr_4 /]# crontab -l
0 * * * * /usr/sbin/ntpdate -u time.nist.gov > /dev/null 2>&1 ; /sbin/hwclock --systohc

0 23 * * * /opt/DatabaseBackUP/databaseBackup.sh

[root@proxy2 cron]# cat root

0 2 * * * /usr/local/bin/squid-analyzer > /dev/null 2>&1
0 */2 * * * /bin/H
*/10 * * * * /bin/S

[root@proxy2 cron]# crontab -l

0 2 * * * /usr/local/bin/squid-analyzer > /dev/null 2>&1
0 */2 * * * /bin/H
*/10 * * * * /bin/S

[root@proxy2 cron]# cat /bin/S
/etc/init.d/squid reload

[root@proxy2 cron]# cat /bin/H
free -m;
sync;
echo 0 > /proc/sys/vm/drop_caches;
sync;
echo 1 > /proc/sys/vm/drop_caches;
sync;
echo 2 > /proc/sys/vm/drop_caches;
sync;
echo 3 > /proc/sys/vm/drop_caches;
sync;
swapoff -av;
swapon -av;
free –m

[root@Tusharjahdav ~]# cp H /opt/   ----copy H.sh file from root/ location to /opt
[root@Tusharjahdav ~]# cd /opt/
[root@Tusharjahdav opt]# ls
corn  H  rh  software  users


[root@Tusharjahdav opt]# cp H /bin/----copy H.sh file from/opt/  to /bin/

[root@Tusharjahdav opt]# chmod +x /opt/H   -----given to permission        
[root@Tusharjahdav opt]# chmod +x /bin/H

[root@Tusharjahdav opt]# H   ------------------run H command for  memory free
/bin/H: line 1: ync: command not found
swapoff on /dev/sda3
swapon on /dev/sda3
swapon: /dev/sda3: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sda3: pagesize=4096, swapsize=2113929216, devsize=2113929216
             total       used       free     shared    buffers     cached
Mem:           992        188        803          0          0         33
-/+ buffers/cache:        155        837
Swap:         2015          0       2015

[root@Tusharjahdav opt]#cp Cc /bin/
[root@Tusharjahdav opt]#chmod +x Cc
[root@Tusharjahdav opt]#chmod +x /bin/Cc
[root@Tusharjahdav opt]# Cc   --------------shutdown command
----------------------------------------------------------------------------------------------------------------
cd /etc/net/
-rw-r--r-- 1 root root 28971 Mar  5 17:49 netma
-rw-r--r-- 1 root root   325 Mar  4 10:05 none

vi /bin/nonet
cp /etc/net/none /etc/hosts;
squid -k reconfigure;
echo We have successfully added Public IP!!!!!!!!
vi /bin/netmac
cp /etc/net/netma /etc/hosts;
squid -k reconfigure;
echo "We have successfully added Netmagic Private IP"
Special string Meaning
@reboot -----------Run once, at startup.
@yearly -----------Run once a year, "0 0 1 1 *".
@annually ---------(same as @yearly)
@monthly ---------Run once a month, "0 0 1 * *".
@weekly -----------Run once a week, "0 0 * * 0".
@daily -------------Run once a day, "0 0 * * *".
@midnight---------(same as @daily)
@hourly -----------Run once an hour, "0 * * * *".

[root@Tusharjahdav cron]# vi /etc/crontab

Typical /etc/crontab file entries:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
$ run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

Directory Description
/etc/cron.d/ Put all scripts here and call them from /etc/crontab
file.
/etc/cron.daily/       Run all scripts once a day
/etc/cron.hourly/    Run all scripts once an hour
/etc/cron.monthly/ Run all scripts once a month
/etc/cron.weekly/   Run all scripts once a week

[root@Tusharjahdav ~]# ps ---------------to check terminal tty number
  PID TTY          TIME CMD                      
 3272 pts/1    00:00:00 bash
 3287 pts/1    00:00:00 ps

[root@Tusharjahdav ~]# ps ---------------to check terminal tty number
  PID TTY          TIME CMD
 3272 pts/1    00:00:00 bash
 3287 pts/1    00:00:00 ps

# - by default cron run every 1 minutes  

*/1 * * * * echo “Good Morning” > /dev/pts/4   --------- set every 1m cron job

[root@Tusharjahdav cron.weekly]# vi /etc/crontab ---To check all cron jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

30 18 * * * harry   run-parts  /opt  -----------------to run all script cron job from  /opt folder
30 18 * * * harry  /opt/bckup.sh   -------------------------to run only one mansion script
/etc/cron.allow and /etc/cron.deny
If a user is only in /etc/cron.allow, then all others are denied
If a user is only in /etc/cron.deny then all others are allowed/not
affected
If cron.deny is touched, then no users is allowed to create a crontab
If cron.allow is touched, then no users is allowed to create a crontab

vi /etc/cron.deny  -----add user name in this file
sachin
rajesh
sohel

touch /etc/cron.allow  --- to block all users

If /etc/cron.allow  file exist then all others users are block and if you want allow same of user just put user name in this file.

vi / etc/cron.allow  -----allow user
champu
harry

My system will shut down in night & cron job not run on set time in this case anacron will run the job

vi /etc/anacronab  ----------------we will manage

Anacron - Job left by cron will be execute by anacron 
anacron is another tool designed for systems which are not always on, such as home computers.
While cron will not run if the computer is off, anacron will simply run the command when the computer is next on (it catches up with things).

at Scheduler  : at used for one time

[root@Tusharjahdav /]# at -f /opt/H now + 1 hour  -----run after 1 hour
job 2 at 2016-01-06 06:30

[root@Tusharjahdav /]#at –f /opt/H 11:00 next month

[root@Tusharjahdav /]# /etc/init.d/atd status  ---check at status
atd (pid  1976) is running...

'at' executes a command once on a particular day, at a particular time. at
will add a particular command to be executed.
Examples:
$ at 21:30
Command syntax:
$ atrm job_no
Will delete the job "job_no" (use atq to find out the number of the job)
$ at -f myjobs.txt now + 1 hour
$ at -f myjob now + 1 min
$ at 10 am tomorrow
$ at 11:00 next month
$ at 22:00 today
$ at now + 1 week
$ at noon

mins hrs DOM MOY DOW
00-59 00-23 1-31 1-12 0-7 (0=Sun 1=Mon, 2=Tue, 3=Wed,4=Thu, 5=Fri,
6=Sat and 7=Sun)

[root@Tusharjahdav opt]# vi bckup.sh------create backup.sh file

#!/bin/bash
rsysc -aprv /opt/*  /home/data

wq!

[root@Tusharjahdav opt]# chmod +x bckup.sh

[root@Tusharjahdav opt]# tail –f /var/log/cron


 
[root@Tusharjahdav /]# crontab -u harry -e


*/5 * * * * /opt/backup.sh   -----Run every 5 m

cp /var/spool/cron/* /home/data/  ----------------take backup of cron
[root@Tusharjahdav /]# tail -f /var/log/cron
Jan  6 23:46:21 Tusharjahdav crontab[4135]: (root) END EDIT (harry)
Jan  6 23:46:24 Tusharjahdav crontab[4138]: (root) BEGIN EDIT (harry)
Jan  6 23:48:12 Tusharjahdav crontab[4138]: (root) END EDIT (harry)
Jan  6 23:49:22 Tusharjahdav crontab[4150]: (root) BEGIN EDIT (harry)
Jan  6 23:49:38 Tusharjahdav crontab[4150]: (root) REPLACE (harry)
Jan  6 23:49:38 Tusharjahdav crontab[4150]: (root) END EDIT (harry)
Jan  6 23:50:01 Tusharjahdav crond[1965]: (harry) RELOAD (/var/spool/cron/harry)
Jan  6 23:50:01 Tusharjahdav CROND[4162]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan  6 23:50:01 Tusharjahdav CROND[4161]: (harry) CMD (/backup.sh)
Jan  6 23:50:01 Tusharjahdav CROND[4160]: (root) CMD (/bckup.sh)

[root@Tusharjahdav data]# cd /var/spool/cron/  -----all cron job location
[root@Tusharjahdav cron]# ls
harry  pranay  root

[root@Tusharjahdav ~]# vi /etc/anacrontab

# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5                 cron.daily                nice run-parts /etc/cron.daily
7       25              cron.weekly              nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly


At- Scheduler

Task :  used for one time
[root@Tusharjahdav ~]# at -f /opt/H now + 1 hour  ----run after 1 hour
job 3 at 2016-01-11 01:03

[root@Tusharjahdav ~]# at -f /opt/H 11:00 next month   ---run next month
job 4 at 2016-02-11 11:00

[root@Tusharjahdav ~]# /etc/init.d/atd status    ---check at status
atd (pid  1973) is running...

Run cron job alternate week day

In this post we will learn,Run cron job alternate week day.
Today, I met with very good question i.e how to run cron job alternate week day. The user wants to run the script in alternate Saturday.When I read this question first time,it make me to take a pause for a while. Later thinking on some more logic,I ended up with nice way to accomplish this task .

What is cron

Cron is a software utility,basically a job scheduler in Linux and Unix like operating system.
Cron information are saved in crontab which is also know as cron table.The format of writing

crontab is given below
Minute Hour date-Of-Month Month Day-Of-Week /path/of/command-script

Task : We got a task to run cron job alternate week day. For eg. run a cronjob in alternate Monday,Tuesday,thursday etc.

See given below examples

Time to run script : 12.30 AM
Month : Run the script every month
Day of Week : Alternate day of week

 

Day of week represented in Crontab as follows

Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Firday = 5
Saturday = 6

On alternate Monday
30 00 1-2,15-16,29-31 * 1 give-path-of-script-command
On alternate Tuesday
30 00 1-3,15-17,29-31 * 2 give-path-of-script-command
On alternate Wednesday
30 00 1-4,15-18,29-31 * 3 give-path-of-script-command
On alternate Thursday
30 00 1-5,15-19,29-31 * 4 give-path-of-script-command
On alternate Friday
30 00 1-6,15-20,29-31 * 5 give-path-of-script-command
On alternate Saturday
30 00 1-7,15-20,29-31 * 6 give-path-of-script-command
On alternate Sunday
30 00 1-7,15-20,29-31 * 0 give-path-of-script-command


crontab entry for: 2nd and 4th  Saturday of each month
The second Saturday of the month falls on one (and only one) of the dates from the 8th to the 14thinclusive. Likewise, the fourth Saturday falls on one date between the 22nd and the 28th inclusive.

You may think that you could use the day of week field to limit it to Saturdays (the 6 in the line below):
00  01  8-14,22-28  *  6  /path/to/myscript

crontab entry for: 2nd Saturday of each month