Jumat, 15 Maret 2019

How to Configure MySQL 8.0 Master Slave Replication on Centos 7.x

This guide to help you Configure MySQL 8.0 Master-Slave Replication on Centos 7.x

This setup will use below Server details:

Master MySQL Server: 10.0.0.98
Slave MySQL Server:  10.0.0.99

Setup Prerequisites:

You need to have MySQL Server installed on all servers before you can continue, refer to the following guides for installation of MySQL Server:

How to Install MySQL 8 Community on CentOS 7


Step 1: Configure the Master Server

The first configuration change to make is setting Server ID for the master database:

# vim /etc/my.cnf

Add the line below under [mysqld] section. Note that the number set needs to be unique, it can not be re-used on any node in the cluster.

server-id = 1

Set  log_bin location, this is where all replication information is located. All the changes made on the master are written to this file. All slaves will copy data from it.

log-bin=mysql-bin.log
binlog_do_db=exampledb
server-id=1
sync_binlog=1
user=mysql
symbolic-links=0
tmpdir = /tmp
binlog_format = ROW
max_binlog_size = 500
expire-logs-days = 7
slow_query_log

A complete simple configuration looks like below:

[mysqld]
log-bin=mysql-bin.log
binlog_do_db=exampledb
server-id=1
sync_binlog=1
user=mysql
symbolic-links=0
tmpdir = /tmp
binlog_format = ROW
max_binlog_size = 500
expire-logs-days = 7
slow_query_log

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[innodb]
innodb_flush_log_at_trx_commit=1

binlog_do_db=exampledb: this is the database that will be replicated

Restart mysql service for changes to take effect:

# systemctl restart mysql


Step 2: Create Replication user on Master database server

We now need to create a database user to be used by slaves when connecting. Login to MySQL database as root user and create the user:

[root@master etc]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create user 'sync'@'10.0.0.99' identified by 'sync1234';
Query OK, 0 rows affected (0.01 sec)

Grant the user REPLICATION SLAVE privileges:

mysql> grant replication slave on *.* to 'sync'@'10.0.0.99';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

Confirm grants for created user:

mysql> show grants for 'sync'@'10.0.0.99';
+------------------------------------------------------+
| Grants for sync@10.0.0.99                            |
+------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `sync`@`10.0.0.99` |
+------------------------------------------------------+
1 row in set (0.00 sec)



Step 3: Create Database and table on Master database server

We now need to create a database and table for example Replication:

mysql> create database exampledb;
Query OK, 1 row affected (0.04 sec)

mysql> use exampledb;
Database changed
mysql> CREATE TABLE exampletb ( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_example primary     key (id) );
Query OK, 0 rows affected (0.02 sec)

mysql> GRANT ALL PRIVILEGES ON exampledb.* to 'sync'@'10.0.0.99';
Query OK, 0 rows affected (0.01 sec)


Step 4: Backup database

From shell terminal :

[root@master ~]# mysqldump -uroot -p --opt exampledb > exampledb.sql
Enter password:

Copy to slave  database server

[root@master ~]# scp exampledb.sql root@10.0.0.98:/root/
root@10.0.0.98's password:
exampledb.sql                                 100% 1875     2.1MB/s   00:00   


Step 5: Install and Configure Slave Server

Install MySQL Server 8.0 on Slave server in a similar process used for the Master server. You can follow steps in the guide How to Install MySQL 8.0 on CentOS 7

When done with the installation, configure slave by editing the file:

[mysqld]
server-id=2
relay-log=mysql-relay-bin.log
log_bin=mysql-bin.log
binlog_do_db=exampledb
user=mysql
symbolic-links=0
read_only = 1
tmpdir = /tmp
binlog_format = ROW
max_binlog_size = 500
expire-logs-days = 7
slow_query_log

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[innodb]
innodb_flush_log_at_trx_commit=1

read_only = 1: This sets the slave to read-only mode. Only users with the SUPER privilege and the replication slave thread will be able to modify data on it. This ensures there are no applications that can accidentally modify data on the slave instead of master.

log_bin = mysql-bin.log:  This enables binary logging. This is required for acting as a MASTER in a replication configuration. You also need the binary log if you need the ability to do point in time recovery from your latest backup.

Restart mysql server after you’ve finished making changes:

# systemctl restart mysqld


Step 6: Restore database

First login to mysql with root privilege create database exampledb:

[root@slave ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> create database exampledb;
Query OK, 1 row affected (0.01 sec)

from shell terminal

[root@slave ~]# mysql -uroot -p exampledb < exampledb.sql
Enter password:


Step 7: Initialize Replication process

We should be ready to start Replication process on the slave server. Start by checking Status on the master databse server:

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000030
         Position: 155
     Binlog_Do_DB: exampledb
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

Take a note of current Master log file and position. Then configure Slave server with details obtained from the master status command:

[root@slave etc]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CHANGE MASTER TO MASTER_HOST='10.0.0.98',
    -> MASTER_USER='sync',
    -> MASTER_PASSWORD='sync1234',
    -> MASTER_LOG_FILE='mysql-bin.000030',
    -> MASTER_LOG_POS=155;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

Then start replication on the slave:

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

To check slave status, use:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.98
                  Master_User: sync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000030
          Read_Master_Log_Pos: 155
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql-bin.000030
             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: 155
              Relay_Log_Space: 530
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: d144000b-3b07-11e9-a246-0800274a8d27
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
1 row in set (0.00 sec)

Slave IO and SQL should indicate running state:

             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

Test transaction replication

on master database server:

[root@master ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use exampledb;
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> show tables;
+---------------------+
| Tables_in_exampledb |
+---------------------+
| exampletb           |
+---------------------+
1 row in set (0.00 sec)

mysql> INSERT INTO exampletb ( id, name ) VALUES ( null, 'uu' );
Query OK, 1 row affected (0.03 sec)

mysql> select * from exampletb;
+----+------+
| id | name |
+----+------+
|  1 | uu   |
+----+------+
1 row in set (0.00 sec)

on slave database server:

[root@slave ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use exampledb;
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> select * from exampletb;
+----+------+
| id | name |
+----+------+
|  1 | uu   |
+----+------+
1 row in set (0.00 sec)


Rabu, 13 Maret 2019

Installing MySQL Server 8.0 Community Edition on CentOS 7

This guide is to help you with Installing MySQL 8.0 Community Edition Server on CentOS 7

To start installing MySQL server on CentOS 7, you need to add the official MySQL community repository to your system. Run below commands to add it,

# cd
# wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
# yum localinstall mysql80-community-release-el7-1.noarch.rpm

Install MySQL 8 on CentOS

Now that repo is added, you can install MysQL 8 without editing repository content since repo for 8 is enabled by default.

# yum --enablerepo=mysql80-community install mysql-community-server

Start MySQL Service

For CentOS 7, use systemd to start mysql service:

# systemctl enable  mysqld
# systemctl start mysqld

Harden MySQL Server / Set MySQL root password

Installation of MySQL on CentOS 7 generates a temporary password for you. You can get it by running:

# grep 'temporary password' /var/log/mysqld.log

It will look like below:

temporary password is generated for root@localhost: si=R&3t#Buy7

Change mysql root user password and Harden MySQL

# mysql_secure_installation

You will be given the choice to change the MySQL root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is recommended that you answer yes to these options.

Configure Firewall

Firewalld:

# firewall-cmd --add-service mysql --permanent
# firewall-cmd --reload

Test your settings:

[root@webapps3 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 173142
Server version: 8.0.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Selasa, 12 Maret 2019

Create a New MySQL User and Database


Creating users and databases

The standard tool for interacting with MySQL is the mysql client which installs with the mysql-server package. The MySQL client is used through a terminal.

To create a MySQL database and user, follow these steps:

  1. At the command line, log in to MySQL as the root user: 

    # mysql -u root -p

  2. Type the MySQL root password, and then press Enter. 

  3. To create a database user, type the following command. Replace username with the user you want to create, and replace password with the user's password:

    mysql> create database testdb;
    mysql> create user 'testuser'@'localhost' identified by 'password';
    mysql> grant all privileges on testdb.* to 'testuser' identified by 'password';

    You can shorten this process by creating the user while assigning database permissions:

    mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';

    Information This command grants the user all permissions. However, you can grant specific permissions to maintain precise control over database access. For example, to explicitly grant the SELECT permission, you would use the following command:

        GRANT SELECT ON *.* TO 'username'@'localhost';

    For more information about setting MySQL database permissions, please visit https://dev.mysql.com/doc/refman/8.0/en/grant.html.

  4. Type \q or exit to exit the mysql program.

  5. To log in to MySQL as the user you just created, type the following command. Replace username with the name of the user you created in step 3:

    mysql> mysql -u username -p

  6. Type the user's password, and then press Enter.

  7. To create a database, type the following command. Replace dbname with the name of the database that you want to create:

    mysql> CREATE DATABASE dbname;

  8. To work with the new database, type the following command. Replace dbname with the name of the database you created in step 7:

  9. You can now work with the database. For example, the following commands demonstrate how to create a basic table named example, and how to insert some data into it:

    mysql> CREATE TABLE example ( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_example primary key (id) );
    mysql>  INSERT INTO example ( id, name ) VALUES ( null, 'Sample data' );


Deleting tables and databases

To delete a table, type the following command from the mysql> prompt. Replace tablename with the name of the table that you want to delete:

mysql>  DROP TABLE tablename;

Information This command assumes that you have already selected a database by using the USE statement.

Similarly, to delete an entire database, type the following command from the mysql> prompt. Replace dbname with the name of the database that you want to delete:

mysql>  DROP DATABASE dbname;

Warning The mysql program does not ask for confirmation when you use this command. As soon as you press Enter, MySQL deletes the database and all of the data it contains..

Deleting users

To view a list of all users, type the following command from the mysql> prompt:

mysql>  SELECT user FROM mysql.user GROUP BY user;

To delete a specific user, type the following command from the mysql> prompt. Replace username with the name of the user that you want to delete:

mysql>  DELETE FROM mysql.user WHERE user = 'username';


Selasa, 05 Maret 2019

How do I turn off the mysql passowrd validation or Your Password does not Satisfy the Current Policy Requirements


Whenever the user tried to set any password in MySQL, he faced following error:

mysql> create user 'testuser'@'localhost' identified by 'password';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

After a while, he really got frustrated by this Current Policy Requirements for a password.
You can check the current variables related to validating password by running the following command:

mysql> show global variables like 'validate%';
+---------------------------------------+-------------+
| Variable_name                         | Value       |
+---------------------------------------+-------------+
| validate_password.check_user_name     | ON          |
| validate_password.dictionary_file     |             |
| validate_password.length              | 8           |
| validate_password.mixed_case_count    | 1           |
| validate_password.number_count        | 1           |
| validate_password.policy              | MEDIUM      |
| validate_password.special_char_count  | 1           |
+---------------------------------------+-------------+
7 rows in set (0.01 sec)

Now let us see how we can resolve our error.

Method 1: Let us see how we can set the password_policy to low login to mysql as root:

mysql> SET GLOBAL validate_password.policy=LOW;
mysql> SET GLOBAL validate_password.length=6;

Method 2: You can also set the same variable in my.cnf file as well edit my.cnd like this.

[mysqld]
validate_password.policy=LOW
validate_password.length=6

Method 3: Uninstall Plugin which validates password if you use MySQL Ver 5.7 below
Run the following sql command:

mysql> uninstall plugin validate_password;

Senin, 04 Maret 2019

How to Install Percona Server for MySQL on CentOS 7


  1. Download the packages of the desired series for your architecture from the download page. The easiest way is to download bundle which contains all the packages. Following example will download Percona Server for MySQL 8.0.13-4 release packages for CentOS 7:

  2. # wget https://www.percona.com/downloads/Percona-Server-8.0/Percona-Server-8.0.13-4/binary/redhat/7/x86_64/Percona-Server-8.0.13-4-rf0a32b8-el7-x86_64-bundle.tar

  3. You should then unpack the bundle to get the packages:

  4. # tar xvf Percona-Server-8.0.13-4-rf0a32b8-el7-x86_64-bundle.tar

    After you unpack the bundle you should see the following packages when running

    # ls *.rpm
    percona-mysql-router-8.0.13-4.1.el7.x86_64.rpm
    percona-server-client-8.0.13-4.1.el7.x86_64.rpm
    percona-server-debuginfo-8.0.13-4.1.el7.x86_64.rpm
    percona-server-devel-8.0.13-4.1.el7.x86_64.rpm
    percona-server-rocksdb-8.0.13-4.1.el7.x86_64.rpm
    percona-server-server-8.0.13-4.1.el7.x86_64.rpm
    percona-server-shared-8.0.13-4.1.el7.x86_64.rpm
    percona-server-shared-compat-8.0.13-4.1.el7.x86_64.rpm
    percona-server-test-8.0.13-4.1.el7.x86_64.rpm
    percona-server-tokudb-8.0.13-4.1.el7.x86_64.rpm

  5. Now you can install Percona Server for MySQL 8.0 by running :

  6. # rpm -ivh percona-server-server-80-8.0.13-4-rf0a32b8.el7.x86_64.rpm \
       percona-server-client-80-8.0.13-4.1.el7.x86_64.rpm \
       percona-server-shared-80-8.0.13-4.1.el7.x86_64.rpm \
       percona-server-shared-compat-80-8.0.13-4.1.el7.x86_64.rpm

    # systemctl start mysqld
    # systemctl enable mysqld

Harden MySQL Server

  1. Run the mysql_secure_installation script to address several security concerns in a default MySQL installation.

  2. # mysql_secure_installation

    You will be given the choice to change the MySQL root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is recommended that you answer yes to these options.

    NOTE
     If MySQL 5.7 was installed, you will need the temporary password that was created during installation. This password is notated in the /var/log/mysql.log file, and can be quickly found using the following command.

    # grep 'temporary password' /var/log/mysqld.log


Rabu, 27 Februari 2019

CentOS 7 : How to setup yum repository using locally mounted iso


1. Copy iso file to some directory. For example /home/iso/

   
# cd /home
# mkdir iso
# cd iso

   Copy iso file to prepare directory, what ever methode you use
   list file on it

# ls
   

2. Mount the CentOS 7 installation media ISO to some directory. For example /media/CentOS

# mkdir -p /media/CentOS

   Edit file /etc/fstab make add line like this
  
/home/iso/CentOS-7-1810.iso    /media/CentOS iso9660    loop,ro        0 0

   Mount iso file to prepare directory

# mount -a

   if No Error you can check on folder /media/CentOS
  
# cd /media/CentOS
# ls

3. remove/backup all repo on /etc/yum.repo.d/

# cd /etc/yum.repo.d
# mv *.repo /root/

   copy back file CentoOS-Media.repo to /etc/yum.repo.d/

# cd
# cp CentOS-Media.repo /etc/yum.repo.d/

   edit fiel /etc/yum.repo.d/CentOS-Media.repo
  
# vi /etc/yum.repo.d/CentOS-Media.repo

   edit like this

[c7-media]
name=CenOS-$releasever - Media
baseurl=file:///media/CentOS/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


4. Make sure you clear the related caches by yum clean all and subscription-manager clean once.
  
# yum clean all


Kamis, 31 Januari 2019

Deploy java API as service in linux CentOS 6


For sample i use uereka microservices

  1. Create user for running API

    # useradd api
    # passwd api

  2. login as api create directory eureka for put jar file, copy jar file to /home/api/eureka

    # mkdir eureka

  3. Create script wrapper for system V init in /home/api/eureka


    #!/bin/sh
    ### BEGIN INIT INFO
    # chkconfig: 345 98 01
    # description: Startup Script for Eureka API
    # Provides:          Eureka
    ### END INIT INFO

    SCRIPT="nohup java -jar -DSpring.profiles.active=cloud1 /home/api/eureka/eureka-service.jar"
    RUNAS=api

    PIDFILE=/home/api/eureka/eureka-api.pid
    LOGFILE=/home/api/eureka/eureka-api.log

    start() {
      if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
        echo 'Eureka already running' >&2
        return 1
      fi
      cd /home/api/eureka
      echo 'Starting service… Eureka' >&2
      local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
      su -c "$CMD" $RUNAS > "$PIDFILE"
        
      # Try with this command line instead of above if not workable
      # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
      # or
      # nohup java -jar -DSpring.profiles.active=cloud1 /home/api/eureka/eureka.jar &> /home/api/eureka/eureka-api.log & echo $! > $PIDFILE

      PID=$(cat $PIDFILE)
        if pgrep -u $RUNAS -f eureka-service.jar > /dev/null
        then
          echo "API Eureka is now running, the PID is $PID"
        else
          echo ''
          echo "Error! Could not start Eureka !"
        fi
    }
      
    stop() {
      if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
        echo 'Service Eureka not running' >&2
        return 1
      fi
      echo 'Stopping service…' >&2
      kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
      echo 'Service Eureka stopped' >&2
    }
      
    uninstall() {
      echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
      local SURE
      read SURE
      if [ "$SURE" = "yes" ]; then
        stop
        rm -f "$PIDFILE"
        echo "Notice: log file was not removed: $LOGFILE" >&2
        update-rc.d -f eureka remove
        rm -fv "$0"
      fi
    }
      
    status() {
        printf "%-50s" "Checking Eureka ..."
        if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
           PID=$(cat $PIDFILE)
                if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
                    printf "%s\n" "The process appears to be dead but pidfile still exists"
                else  
                    echo "Running, the PID is $PID"
                fi
        else
            printf "%s\n" "Service Eureka not running"
        fi
    }
      
      
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      status)
        status
        ;;
      uninstall)
        uninstall
        ;;
      restart)
        stop
        start
        ;;
      *)
        echo "Usage: $0 {start|stop|status|restart|uninstall}"
    esac
      

  4. Enable service in system V and start eureka

    # sudo ln -s /home/api/eureka/eureka /etc/init.d/eureka
    # sudo chkconfig --add eureka
    # sudo chkconfig eureka on
    # sudo service eureka start

  5. Firewall

    # iptables -I INPUT -p tcp --destination-port 8761 -j ACCEPT
    # service iptables save

Rabu, 30 Januari 2019

Install Tomcat 8 on a CentOS 6


In the following tutorial you will learn how to install and set-up Apache Tomcat 8 on your CentOS 6

What is Tomcat?

Apache Tomcat (before known as Jakarta Tomcat) is an application server developed by the Apache Software Foundation that executes Java servlets and renders Web pages that include Java Server Page coding.


UPDATE THE SYSTEM


Make sure you are in a your CentOS 6 based Linux Server is up-to-date by running:

# yum update


INSTALL JAVA 8


Download the latest JAVA 8 from  here or use the following command to download JAVA JDK

# wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
"https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.rpm" \
-O /tmp/jdk-8u201-linux-x64.rpm

once the JAVA package has been downloaded, install it using rpm as follows:

# rpm -Uvh /tmp/jdk-8u201-linux-x64.rpm

CONFIGURE JAVA

configure the newly installed JAVA package using alternatives as in:

# alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_201-amd64/jre/bin/java 20000
# alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_201-amd64/bin/jar 20000
# alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_201-amd64/bin/javac 20000
# alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_201-amd64/jre/bin/javaws 20000
# alternatives --set java /usr/java/jdk1.8.0_201-amd64/jre/bin/java
# alternatives --set javaws /usr/java/jdk1.8.0_201-amd64/jre/bin/javaws
# alternatives --set javac /usr/java/jdk1.8.0_201-amd64/bin/javac
# alternatives --set jar /usr/java/jdk1.8.0_201-amd64/bin/jar

check the JAVA version running on your system:

# java -version

INSTALL TOMCAT 8

Create a separate user which will run the Tomcat server:

# useradd tomcat8

Download the latest Tomcat 8 version from here or use the following command to download Tomcat

# wget https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.37/bin/apache-tomcat-8.5.37.tar.gz -P /tmp

Extract the contents of the Tomcat archive to /opt using the following command:

# tar zxf /tmp/apache-tomcat-8.5.37.tar.gz -C /opt

make symbolic link

# ln -s /opt/apache-tomcat-8.5.37 /opt/tomcat8

change permission

# chown -hR tomcat8: /opt/tomcat8 /opt/apache-tomcat-8.5.37
# cd /opt/tomcat8/bin
# chmod +x *.sh


START THE TOMCAT 8 SERVICE

Create the following init script in /etc/init.d/tomcat8

#!/bin/sh
### BEGIN INIT INFO
# chkconfig: 345 84 16
# description: Tomcat Jakarta JSP Server
# Provides: Tomcat
### END INIT INFO

JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JAVA_HOME

## IF NEEDED 
#JAVA_OPTS="-Dfile.encoding=UTF-8 \
#  -Dnet.sf.ehcache.skipUpdateCheck=true \
#  -XX:+UseConcMarkSweepGC \
#  -XX:+CMSClassUnloadingEnabled \
#  -XX:+UseParNewGC \
#  -XX:MaxPermSize=128m \
#  -Xms512m -Xmx512m"
# export JAVA_OPTS

PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/tomcat8

STARTSCRIPT="/opt/tomcat8/bin/startup.sh"
STOPSCRIPT="/opt/tomcat8/bin/shutdown.sh"
RUNAS=tomcat8

PIDFILE=/opt/tomcat8/tomcat8.pid
LOGFILE=/opt/tomcat8/tomcat8.log

start() {

  TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
  if [ "$TOMCATLIVE" -eq 0 ]; then
    echo 'Starting Jakarta Tomact service …' >&2
    local CMD="$STARTSCRIPT &> \"$LOGFILE\" & echo \$!"
    su -l $RUNAS -c "$CMD" > "$PIDFILE"
  sleep 2
  PID=$(cat $PIDFILE)

    if pgrep -u $RUNAS -f bootstrap.jar > /dev/null
    then
      echo "Jakarta Tomcat is now running, the PID is $PID"
    else
      echo ''
      echo "Error! Could not start Jakarta Tomcat!"
    fi

  else
    echo 'Service Jakarta Tomcat all ready running' >&2
    return 1
  fi
  
}

stop() {

  TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
  if [ "$TOMCATLIVE" -eq 0 ]; then
    echo 'Service Jakarta Tomcat not running' >&2
    return 1
  fi
  echo 'Stopping Jakarta Tomcat service …' >&2
  su -l $RUNAS -c $STOPSCRIPT
  rm -f "$PIDFILE"
  echo 'Service Jakarta Tomcat stopped' >&2

}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file was not removed: $LOGFILE" >&2
    update-rc.d -f tomcat8 remove
    rm -fv "$0"
  fi
}

status() {

TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
TOMCATPIDLIVE=$(pgrep -u tomcat8 -f bootstrap.jar )
if [ "$TOMCATLIVE" -eq 0 ]; then
  echo -n "Jakarta Tomcat Stopped"
  echo
else
  echo "Jakarta Tomcat Running, the PID is $TOMCATPIDLIVE "
fi

}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac

make the script executable using chmod

# chmod +x /etc/init.d/tomcat8

Add the Tomcat 8 service to system startup:

# chkconfig --add tomcat8
# chkconfig tomcat8 on

Start the Tomcat 8 server using:

# service tomcat8 start

Configuring Firewall

# iptables -I INPUT -p tcp --destination-port 8080 -j ACCEPT
# service iptables save
# ip6tables -I INPUT -p tcp --destination-port 8080 -j ACCEPT
# service ip6tables save

Access your newly installed Tomcat at http://YOUR_IP:8080

Minggu, 27 Januari 2019

How to Disable SELinux on CentOS


By default in CentOS 7, SELinux is enabled and in enforcing mode.

It is recommended to keep SELinux in enforcing mode, but in some cases, you may need to set it to permissive mode or disable it completely.

In this tutorial, we will show you how to disable SELinux on CentOS 7 systems.


Disable SELinux

You can temporarily change the SELinux mode from targeted to permissive with the following command:

# setenforce 0

However, this change will be valid for the current runtime session only.

To permanently disable SELinux on your CentOS 7 system, follow the steps below:

Open the /etc/selinux/config file and set the SELINUX mod to disabled:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#       targeted - Targeted processes are protected,
#       mls - Multi Level Security protection.
SELINUXTYPE=targeted

Save the file and reboot your CentOS system with:

# shutdown -r now

Once the system boots up, verify the change with the sestatus command:

# sestatus

The output should look like this:

SELinux status:                 disabled

Conclusion

In this tutorial, you learned how to permanently disable SELinux on a CentOS 7 machine.
You may wish to visit the CentOS SELinux guide and learn more about the powerful features of SELinux.


Kamis, 17 Januari 2019

Quit Bash Shell Without Saving Bash History


Sometimes it is very useful to delete / remove Bash history partially or completely when log out. Here is my favourite methods howto log out / quit / exit Bash shell without saving Bash history.

Remove Only Current Session Bash History and Leave Older History Untouched

  1. Quit Bash Shell Without Saving History: Unset HISTFILE

  2. # unset HISTFILE && exit

  3. Quit Bash by Killing you Current Consolel Without Saving History: Kill Console

  4. # kill -9 $$

  5. Quit Bash Shell Without Saving History: Clear History Option

  6. #history -c && exit

  7. Set your Bash Shelll history to Zero(0):  Set HISTSIZE 0

  8. #HISTSIZE=0 && exit


Remove/Delete Bash History Completely

  1. Quit Bash Shell Without Saving History: Delete HISTFILE and Unset HISTFILE

  2. #rm -f $HISTFILE && unset HISTFILE && exit


If you want make these commands more permanent then these commands could be added on ~/.bash_logout file or used with aliases.

echo "alias nohistory='history -c && exit'" >> ~/.bash_logout


Run the remote linux graphics application locally


Overview

The X Window System (also known as X11, or just X) is a software package and network protocol that lets you interact locally,
using your personal computer's display, mouse, and keyboard, with the graphical user interface (GUI) of an application running on a remote networked computer.

Requirements

For X forwarding in SSH to work your personal computer must be running an X server program.
The X server program manages the interaction between the remote application (the X client) and your computer's graphics hardware and input devices.

Most Linux distributions have the X server installed,
but if your personal computer is running Windows, you will most likely need to install and run an X server application, for example:

  • Xming, download and install Xming. For X forwarding to work, you'll need to start Xming before connecting to the remote system with your SSH client (for example, PuTTY).

  • MobaXTerm, download and install MobaXTerm

but now we will only focus Xming

Additionally, your personal computer's SSH terminal application must have X11 forwarding enabled:

  • In Linux, the SSH terminal supports X forwarding by default.

  • if not you can edit the sshd_config file

vi /etc/ssh/sshd_config

change for the lines below:

X11Forwarding yes
X11UseLocalhost yes

Restart the ssh srever, if you have made changes:

service sshd restart

  • In PuTTY for Windows, you can enable X forwarding new or saved SSH sessions by selecting Enable X11 forwarding in the "PuTTY Configuration" window ( Connection  > SSH > X11).
Also, the remote computer's SSH application must be configured to accept X server connections.


Use SSH with X forwarding

Linux

To use SSH with X forwarding from your Linux personal computer to run an X client application installed on remote server:

  1. Open the SSH terminal client.

  2. On the command line, enter (replacing username with your username):

  3. ssh -Y username@host

    note
    The -Y option turns on trusted X forwarding. You should use it only when connecting to secure systems.

  4. Log in with your password

  5. To test if X forwarding is working, try running xclock; on the command line, enter:

    xclock

    If X forwarding is working, the xclock graphical clock will appear on your personal computer's desktop.

PuTTY for Windows

To use SSH with X forwarding in PuTTY for Windows:

  1. Launch your X server application (for example, Xming).

  2. Make sure your connection settings for the remote system have Enable X11 forwarding selected; in the "PuTTY Configuration" window, see Connection > SSH > X11

     

  3. Open an SSH session to the desired remote system:

  4. Log in normally with your username and password.

To test if X forwarding is working, try running xclock; on the command line, enter:

If X forwarding is working, the xclock graphical clock will appear on your personal computer's desktop.

 How to start X application from SSH [simple way]
 
A short command

ssh -X username@host xapplication

note
The -X  is Enable X11 Forwarding.