Installing a MySQL Server on CentOS

MySQL is an open-source relational database that is free and widely used. It is a good choice if you know that you need a database but don’t know much about all of the available options.

Note: CentOS 7 has replaced MySQL with MariaDB. To reflect this, instructions for MariaDB procedures are included in this article.

Install the MySQL database through the CentOS package manager (yum) by running the following commands at a command prompt:

#sudo yum install mysql-server
#sudo /sbin/service mysqld start

Run the following command:

sudo /usr/bin/mysql_secure_installation

Press Enter to give no password for root when prompted for it.

To apply some reasonable security to your new MySQL server answer yes to all the prompts. In order, those prompts enable you set the root password, remove anonymous users, disable remote root logins, delete the test database that the installer included, and then reload the privileges so that your changes will take effect.

Install MariaDB

Install the MariaDB server through the CentOS package manager (yum) by running the following command at a command prompt:

sudo yum install mariadb-server mariadb

Allow remote access

If you have iptables enabled and want to connect to the MySQL database from another computer, you must open a port in your server’s firewall (the default port is 3306). You don’t need to do this if the application that uses MySQL is running on the same server.

If you need to open a port, add the following rules in iptables to open port 3306

iptables -I INPUT -p tcpdport 3306 -m state –state NEW,ESTABLISHED -j ACCEPT
iptables -I OUTPUT -p tcp –sport 3306 -m state –state ESTABLISHED -j ACCEPT

Start and stop the database service

After the installation is complete, you can start the database service by using the commands in this section. If the system is already started, a message informs you that the service is already running.

Start and stop MySQL

Use the following command to start MySQL:

#sudo /sbin/service mysqld start

Use the following command to stop MySQL:

sudo /sbin/service mysqld stop

Start and stop MariaDB

Use the following command to start MariaDB:

#sudo systemctl start mariadb.service

Use the following command to stop MariaDB:

#sudo systemctl stop mariadb.service

Launch at reboot

To ensure that the database server launches after a reboot, you must enable the chkconfig utility. Use the following commands to do this.

Enable chkconfig on MySQL

#sudo chkconfig mysqld on

Enable chkconfig on MariaDB

#sudo systemctl enable mariadb.service

Start the mysql shell

There is more than one way to work with a MySQL server, but this article focuses on the most basic and compatible approach: the mysql shell.

At the command prompt, run the following command to launch the mysql shell and enter it as the root user:

#/usr/bin/mysql -u root -p
When you’re prompted for a password, enter the one that you set at installation or, if you haven’t set one, press Enter to submit no password.

The following mysql shell prompt should appear:


Set the root password

Because you have just installed the MySQL database server, the root account within MySQL has no password set yet. If you are logged in to the database server, set the root password by running the following command:

#/usr/bin/mysqladmin -u root password ‘new-password’

If you are not logged in to the database server you can remotely set the root password by specifying the hostname of your database server:

#/usr/bin/mysqladmin -u root –password=’new-password’ -h hostname-of-your-server ‘new-password’

How we get networking on Linux

How we get networking on Linux

just three commands.

Step One: Set Your IP Address

ifconfig eth0 netmask up

Step 2: Set Your Default Gateway

route add default gw

Step 3: Set Your DNS Server

echo “nameserver” > /etc/resolv.conf

That’s it.

Assuming you have valid addresses for yourself and your gateway


Running Process in Background on Linux

Adding & the end of the command

On Linux/Unix based systems you can put a task (such as command or script) in a background by appending a & at the end of the command line. In other words, you can say that easily send any process in the background.

# command &
# script-name &

# find / -name *.c > /root/output.txt &

Using & with nohup
You can use nohup command line-utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell:

# nohup command-name &
# nohup find / -size +1k > /root/output.txt &


Patching using with yum on Linux

Linux has different methods to update all packages.  Before updating the packages we may need to take backup. The exact command depends upon your Linux distribution:

1. Debian/Ubuntu – apt-get command
2. CentOS/RedHat/Fedora – yum command

Debian/Ubuntu – apt-get command
Run the following command to update the packages.

$ sudo apt-get update
$ sudo apt-get upgrade

CentOS/RedHat/Fedora – yum command

Note:  for Kernel Update, some places we may need to disable exclude kernel from /etc/yum.conf

Run yum command to update all the packages in CentOS/RedHat/Fedora.

# yum -y update

Basic Linux Firewall IP Tables

iptables is a command-line firewall utility that uses the policy chains to allow or block the traffic.  iptables is the rule-based firewall and it is pre-installed on most of Linux operating system. By default, it runs without any rules. Iptables uses a set of tables which have chains that contain set of built-in or user-defined rules.

IP tables (Linux Firewall) Tables and Chains:

IPTables has the following 4 types of tables.

a) Filter Table:

The filter table is one of the most widely used tables in iptables. The filter table is used to make decisions about whether packet continues to its destination or to deny its request. Iptables’s filter table has the following built-in chains.

INPUT chain: Incoming to the firewall. For packets coming to the local server.
OUTPUT chain: Outgoing from the firewall. For packets generated locally and going out of the local server.
FORWARD chain: Packet for another NIC on the local server. For packets routed through the local server.

To view the Filter table rules run the following command.

#iptables -t filter –list
# iptables –list

b) NAT Table:
The nat table is used to implement network address translation rules. A table that is consulted when a packet tries to create a new connection. This is often used to route packets to networks when direct access is not possible.

PREROUTING chain: It is used for altering a packet as soon as it’s received. This helps to translate the destination ip address of the packets to something that matches the routing on the local server.
POSTROUTING chain: It is used for altering packets as they are about to go out. This helps to translate the source ip address of the packets to something that might match the routing on the desintation server.
OUTPUT chain: It is used for locally generated packets on the firewall.
To view the NAT table rules run the following command.

# iptables -t nat –list

c) Mangle Table:
The mangle table is used to alter the IP headers of the packet in various ways. This alters QOS bits in the TCP header. Mangle table has the following built-in chains.

OUTPUT chain
INPUT chain
To view the Mangle table rules run the following command.

# iptables -t mangle –list

d) Raw Table:
The raw table has a very narrowly defined function. Its only purpose is to provide a mechanism for marking packets in order to opt-out of connection tracking.

OUTPUT chain
To view, the Raw table rules run the following command.

# iptables -t raw –list
The rules in the iptables list command output contain the following fields:

num: Rule number within the particular chain
target: Special target variable that we discussed above
prot: Protocols. tcp, udp, icmp, etc.,
opt: Special options for that specific rule.
source: Source ip-address of the packet
destination: Destination IP-address for the packet