How to Export & Import MySQL database in Linux

Export MySQL database

To export mysql database, use the mysqldump command.

# mysqldump -u root -p database-name > backup.sql
e.g
# mysqldump -u test -p abc > abc_backup.sql

Import MySQL database

To import mysql database, use the mysql command.

# mysql -u root -p database-name < backup.sql
e.g
# mysql -u test -p abc < abc_backup.sql

How to Install MySQL Server on CentOS / RHEL / FEDORA

Introduction

MySQL Community Edition is a freely downloadable version of the world’s most popular open source database that is supported by an active community of open source developers and enthusiasts. MySQL is a popular choice of database for use in web applications, and is a central component of the widely used LAMP open source web application software stack.

By Default Port # 3306

Step 1: Prerequisite

# yum install wget

Download the rpm package, which will create a yum repo file for MySQL Server installation.

# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

Now install the downloaded rpm package by using rpm command.

# rpm -ivh mysql-community-release-el7-5.noarch.rpm

Step 2: MySQL Server Installation

Execute the yum command in order to download mysql-server:

# yum install mysql-server

Step 3: Restart MySQL Server

To start MySQL Service, run command

# systemctl start mysqld

Step 4: MySQL Server Secure Installation

In order to reset the root password and make the mysql-server secure execute the following command and follow the steps:

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): [Press Enter]
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Step 5: Create database in MySQL

To create a new database in MySQL, following are the steps we have to follow:

# mysql -u root -p

mysql> CREATE DATABASE test;
mysql> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'new-password';
mysql> GRANT ALL ON test.* TO testuser@localhost IDENTIFIED BY 'new-password';
mysql> FLUSH PRIVILEGES;
mysql> quit

How to Set a Cronjob in Linux

Introduction

A crontab is a simple text file with a list of commands meant to be run at specified times. It is edited with a command-line utility. These commands (and their run times) are then controlled by the cron daemon, which executes them in the system background. Each user has a crontab file which specifies the actions and times at which they should be executed, these jobs will run regardless of whether the user is actually logged into the system. There is also a root crontab for tasks requiring administrative privileges. This system crontab allows scheduling of system wide tasks (such as log rotations and system database updates).

Syntax

Minutes      Hours       Day-of-Month        Month-field         Day-of-Week        /path/to/Command

Set Cronjob

To set the cronjob that runs every 5 minutes

# crontab -e
*/5 * * * * /root/backup.sh

How to Check Memory in Linux

Following are some useful commands to check the memory status of machine:

free Command

Use free command to check the memory, buffers, cache memory

# free -m
             total       used       free     shared    buffers     cached
Mem:          6082       5801        280          0        155       3887
-/+ buffers/cache:       1759       4322
Swap:         1707         33       1674

top Command

Top command tells the detailed information of CPU i.e time, load average, CPU utilization, memory, swap memory.

# top
top - 09:36:16 up 33 days, 16:47,  2 users,  load average: 0.49, 0.47, 0.52
Tasks: 157 total,   1 running, 155 sleeping,   1 stopped,   0 zombie
Cpu(s): 30.0%us,  2.4%sy,  0.0%ni, 67.0%id,  0.5%wa,  0.0%hi,  0.0%si,  0.1%st
Mem:   6228500k total,  5975956k used,   252544k free,   159264k buffers
Swap:  1748984k total,    34100k used,  1714884k free,  3980880k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11774 www-data  20   0 99.8m  56m 6780 S   55  0.9   1:02.52 apache2
21086 www-data  20   0  100m  54m 5620 S   39  0.9   0:04.14 apache2
21075 www-data  20   0 66524  19m 4848 S    4  0.3   0:21.04 apache2

htop Command

Install htop on server, htop command also tells the detailed information of CPU i.e time, load average, CPU utilization, memory, swap memory but in a nice format.

# htop

Check Memory Consumption Processes

Process that are eating more memory

# ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20

How to Change Hostname in Linux

Method 1: Command Line

Step 1: Edit in /etc/sysconfig/network File

In order to change the hostname edit the following file:

# sudo nano /etc/sysconfig/network
HOSTNAME=server.tecdistro.com

Step 2: Modify /etc/hosts File

Edit /etc/hosts File and enter the hostname along with IP Address of server for local use.

# vi /etc/hosts
127.0.0.1 tecdistro.com localhost.localdomain localhost
159.8.18.70 tecdistro.com tecdistro.com

Step 3: Modify the Hostname

# hostname tecdistro.com
# hostname
tecdistro.com

Step 4: Restart Network

Now restart the network:

/etc/init.d/network restart

Method 2: GUI Environment

Step 1: Execute setup Command

# setup

Select Network Configuration Tab
setup1

Step 2: Select DNS Configuration

setup5

Step 3: Modify the Hostname

setup6
Once the changes have made, Select SAVE & QUIT and press ENTER key from keyboard.

How To Install Apache (Web Server) in Linux

For Fedora / RHEL / Cent OS Linux

Step 1: Install apache

# yum install httpd

Step 2: Start apache

To start the Apache/httpd, run:

# /etc/init.d/httpd start
OR
# service httpd start

Also configure httpd service to start on system start.

# chkconfig httpd on

Note: The default directory in Fedora / RHEL / Cent OS Linux operating system is “/var/www/html”

Step 3: Verify apache Port

To verify the httpd port is open or not use the command:

# netstat -antp | grep :80

For Debian / Ubuntu

Step 1: Install apache

Use the apt-get command:

# apt-get install apache2

Step 2: Start apache

# /etc/init.d/apache2 start

Note: The default directory in Debian / Ubuntu Linux operating system is /var/www/

Installation from CD/DVD

We can also install httpd from CDROM with rpm command:

# rpm -ivh httpd*

How To Create FTP Server in Linux

Introduction

The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files from one host to another host over a TCP-based network, such as the Internet.
FTP is built on a client-server architecture and uses separate control and data connections between the client and the server. FTP users may authenticate themselves using a clear-text sign-in protocol, normally in the form of a username and password, but can connect anonymously if the server is configured to allow it. For secure transmission that protects the username and password, and encrypts the content, FTP is often secured with SSL/TLS (FTPS). SSH File Transfer Protocol (SFTP) is sometimes also used instead, but is technologically different.

Package Name:
Linux—–vsftpd
Windows—IIS
Data Connection Port of FTP # 21
Data Transaction Port of FTP # 20

Step 1: Installation

Firstly we have to check the package vsftpd is installed in our machine, if not so then follow the steps:

# yum install -y vsftpd

Step 2: Start the service

# service vsftpd start

To Enable vsftpd in multi-user levels.

# chkconfig vsftpd on

Step 3: Configure vsftpd.conf File

Now edit the /etc/vsftpd/vsftpd.conf file.

vim /etc/vsftpd/vsftpd.conf
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES
# WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log
#xferlog_file=/var/log/xferlog
#
# Switches between logging into vsftpd_log_file and xferlog_file files.
# NO writes to vsftpd_log_file, YES to xferlog_file
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
ascii_upload_enable=YES
ascii_download_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=Welcome to our FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
#chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
use_localtime=YES

Step 4: Restart the service

Now restart the service vsftpd

# service vsftpd restart

Step 5: Create FTP Users

Now create FTP user:

# adduser -c 'FTP USER Test' -m test
# passwd test

To change the home directory of user, use the following command:

# usermod --home /var/www/ username

Note: If selinux is enabled on server, then execute the following command

# setsebool -P ftp_home_dir=1

Conculsion

FTP acoount is working now. You can use your FTP server. 🙂

How to Download Linux CentOS

Version Minor release CD and DVD ISO Images
CentOS-7 7.0.1406 For 32-bit & 64-bit   http://mirrors.nayatel.com/centos/7/isos/x86_64/
CentOS-6 6.6 For 32-bit   http://mirrors.nayatel.com/centos/6.6/isos/i386/
For 64-bit   http://mirrors.nayatel.com/centos/6.6/isos/x86_64/
CentOS-5 5.1 For 32-bit   http://mirrors.nayatel.com/centos/5.11/isos/i386/
For 64-bit   http://mirrors.nayatel.com/centos/5.11/isos/x86_64/