May 26, 2017

Instalación de MongoDB


Hace poco también me tocó configurar un servicio de base de datos mongodb, un motor que al parecer ya se está comenzando a utilizar bastante. Les dejo algunos pasos a continuación:


Primero crearemos el usuario mongod (como usuario root):
mkdir /home/mongod
chown mongod:mongod /home/mongod
chmod 700  /home/mongod
adduser --system -d /home/mongod mongod
echo secu13re | passwd --stdin mongod
usermod -c 'mongod_database_not_delete' mongod
usermod -s /bin/bash mongod


Ahora nos convertiremos en usuario mongod que acabamos de crear
[root@bci-bluemixdbmongo01 /]# su - mongod
Last login: Fri May 26 11:33:04 CLT 2017 on pts/0

Como usuario mongod descomprimimos el archivo que hemos bajado del sitio web de mongo:

-bash-4.2$ cd /dbprdMongo/
-bash-4.2$ ls -ltr
total 80500
drwxrwx--- 4 mongod root       27 May 23 17:19 bd
-rwxrwxrwx 1 root   root 82431513 May 26 11:22 mongodb-linux-x86_64-rhel70-3.2.13.tgz
-bash-4.2$ tar xvfz mongodb-linux-x86_64-rhel70-3.2.13.tgz
mongodb-linux-x86_64-rhel70-3.2.13/README
mongodb-linux-x86_64-rhel70-3.2.13/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.2.13/MPL-2
mongodb-linux-x86_64-rhel70-3.2.13/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongodump
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongostat
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongotop
mongodb-linux-x86_64-rhel70-3.2.13/bin/bsondump
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongooplog
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongosniff
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongod
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongos
mongodb-linux-x86_64-rhel70-3.2.13/bin/mongo



Crear link simbolico:
-bash-4.2$ ln -s /dbprdMongo/mongodb-linux-x86_64-rhel70-3.2.13 /dbprdMongo/mongodb
-bash-4.2$


Posteriormente añadir como root los siguientes privilegios para la carpeta que contendrá los datafiles y logfiles:
[root@bci-bluemixdbmongo01 dbprdMongo]# pwd
/dbprdMongo
[root@bci-bluemixdbmongo01 dbprdMongo]# chown -R mongod:root bd/
[root@bci-bluemixdbmongo01 dbprdMongo]# chmod -R 770 bd/
[root@bci-bluemixdbmongo01 dbprdMongo]#


Añadir lo siguiente al bash_profile del usuario mongod
export PATH=$PATH:/dbprdMongo/mongodb/bin


Se sugiere dejar el selinux deshabilitado
/etc/selinux/config con SELINUX=disabled.


Ahora se procederá a probar el servicio de mongodb

-bash-4.2$ /dbprdMongo/mongodb/bin/mongod --dbpath /dbprdMongo/bd
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] MongoDB starting : pid=2056 port=27017 dbpath=/dbprdMongo/bd 64-bit host=bci-bluemixdbmongo01
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] db version v3.2.13
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] git version: 23899209cad60aaafe114f6aea6cb83025ff51bc
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] allocator: tcmalloc
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] modules: none
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] build environment:
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten]     distmod: rhel70
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten]     distarch: x86_64
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten]     target_arch: x86_64
2017-05-26T11:57:45.852-0300 I CONTROL  [initandlisten] options: { storage: { dbPath: "/dbprdMongo/bd" } }
2017-05-26T11:57:45.876-0300 I -        [initandlisten] Detected data files in /dbprdMongo/bd created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-05-26T11:57:45.876-0300 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten]
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten]
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-05-26T11:57:46.147-0300 I CONTROL  [initandlisten]
2017-05-26T11:57:46.148-0300 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/dbprdMongo/bd/diagnostic.data'
2017-05-26T11:57:46.148-0300 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-05-26T11:57:46.148-0300 I NETWORK  [initandlisten] waiting for connections on port 27017
^C2017-05-26T11:57:47.703-0300 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2017-05-26T11:57:47.703-0300 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2017-05-26T11:57:47.705-0300 I CONTROL  [signalProcessingThread] now exiting
2017-05-26T11:57:47.705-0300 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2017-05-26T11:57:47.705-0300 I NETWORK  [signalProcessingThread] closing listening socket: 5
2017-05-26T11:57:47.705-0300 I NETWORK  [signalProcessingThread] closing listening socket: 6
2017-05-26T11:57:47.705-0300 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2017-05-26T11:57:47.705-0300 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2017-05-26T11:57:47.705-0300 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2017-05-26T11:57:47.705-0300 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2017-05-26T11:57:47.754-0300 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2017-05-26T11:57:47.755-0300 I CONTROL  [signalProcessingThread] dbexit:  rc: 0


Ahora se procera a crear la automatizaciòn del servicio (como usuario root)

Creamos el siguiente archivo:
vi /etc/systemd/system/mongod.service

Con el siguiente contenido:
[Unit]
Description=Mongo Database
After=network.target

[Service]
User=mongod
Group=mongod
ExecStart=/dbprdMongo/mongodb/bin/mongod --dbpath /dbprdMongo/bd
ExecStop=/dbprdMongo/mongodb/bin/mongod --dbpath /dbprdMongo/bd --shutdown
Restart=always

[Install]
WantedBy=multi-user.target


Como root se procede a automatizar el servicio

[root@bci-bluemixdbmongo01 ~]# sudo systemctl enable mongod
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /etc/systemd/system/mongod.service.

Ahora se procede a probar el servicio
[root@bci-bluemixdbmongo01 ~]# systemctl start mongod
[root@bci-bluemixdbmongo01 ~]# systemctl status mongod
● mongod.service - Mongo Database
  Loaded: loaded (/etc/systemd/system/mongod.service; enabled; vendor preset: disabled)
  Active: active (running) since Fri 2017-05-26 12:11:46 CLT; 2s ago
Main PID: 2192 (mongod)
  CGroup: /system.slice/mongod.service
          └─2192 /dbprdMongo/mongodb/bin/mongod --dbpath /dbprdMongo/bd

May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.408-0300 I CONTROL  [initandlisten]
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.409-0300 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hu...always'.
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.409-0300 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.409-0300 I CONTROL  [initandlisten]
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.409-0300 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hu...always'.
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.409-0300 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.409-0300 I CONTROL  [initandlisten]
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.410-0300 I FTDC     [initandlisten] Initializing full-time diagnostic data ca...ic.data'
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.410-0300 I NETWORK  [initandlisten] waiting for connections on port 27017
May 26 12:11:46 bci-bluemixdbmongo01 mongod[2192]: 2017-05-26T12:11:46.410-0300 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canoni...n worker
Hint: Some lines were ellipsized, use -l to show in full.
[root@bci-bluemixdbmongo01 ~]# ps -fea | grep -i mongo
mongod    2192     1  1 12:11 ?        00:00:00 /dbprdMongo/mongodb/bin/mongod --dbpath /dbprdMongo/bd
root      2218  1903  0 12:12 pts/0    00:00:00 grep --color=auto -i mongo

Ahora se procede a bajar
[root@bci-bluemixdbmongo01 bd]# systemctl stop mongod
[root@bci-bluemixdbmongo01 bd]#

May 22, 2017

Instalación y configuración del motor de base de datos redis

Hace poco me ha tocado realizar la instalación y configuración del motor redis una base de datos orientada a memoria y también ocupada como message broker. Esta instalación la realizaremos con el usuario root. Sólo algunas cosas las haremos con el usuario redis que crearemos más adelante.

Cómo usuario root procedemos a ejecutar (tomando en cuenta que mis instaladores los tengo en la ruta: /dbprdRedis):

[root@bci-bluemixdbredis01 tmp]# cd /dbprdRedis

[root@bci-bluemixdbredis01 dbprdRedis]# ls -lptr
total 1512
drwxr-xr-x 2 usr_dbredis root       6 May  3 04:03 bd/
-rwxr-xr-x 1 root        root 1547237 May 23 10:50 redis-3.2.8.tar.gz
(procedemos a descomprimir el fuente)
[root@bci-bluemixdbredis01 dbprdRedis]# tar xzvf redis-3.2.8.tar.gz

(ingresamos a la nueva carpeta e instalamos)
[root@bci-bluemixdbredis01 dbprdRedis]# cd redis-3.2.8/

[root@bci-bluemixdbredis01 redis-3.2.8]# make
cd src && make all
make[1]: Entering directory `/dbprdRedis/redis-3.2.8/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
(cd ../deps && make distclean)
make[2]: Entering directory `/dbprdRedis/redis-3.2.8/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd geohash-int && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
make[2]: Leaving directory `/dbprdRedis/redis-3.2.8/deps'
(rm -f .make-*)
echo STD=-std=c99 -pedantic -DREDIS_STATIC='' >> .make-settings
echo WARN=-Wall -W >> .make-settings
echo OPT=-O2 >> .make-settings
echo MALLOC=jemalloc >> .make-settings
echo CFLAGS= >> .make-settings
echo LDFLAGS= >> .make-settings
echo REDIS_CFLAGS= >> .make-settings
echo REDIS_LDFLAGS= >> .make-settings
echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -DREDIS_STATIC='' -Wall -W -O2 -g -ggdb   -I../deps/geohash-int -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings
echo PREV_FINAL_LDFLAGS=  -g -ggdb -rdynamic >> .make-settings
(cd ../deps && make hiredis linenoise lua geohash-int jemalloc)
make[2]: Entering directory `/dbprdRedis/redis-3.2.8/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd geohash-int && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
(echo "" > .make-cflags)
(echo "" > .make-ldflags)
(Esto tomará algunos minutos y mostrará bastante output. Luego pedirá ejecutar make test)

[root@bci-bluemixdbredis01 redis-3.2.8]# make test
cd src && make test
make[1]: Entering directory `/dbprdRedis/redis-3.2.8/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/dbprdRedis/redis-3.2.8/src'
make: *** [test] Error 2

Esto requiere tener instalado tcl:
[root@bci-bluemixdbredis01 redis-3.2.8]# yum install tcl*

Una vez instalado se puede proseguir:
(también esto tomará algunos minutos)
[root@bci-bluemixdbredis01 redis-3.2.8]# make test
cd src && make test
make[1]: Entering directory `/dbprdRedis/redis-3.2.8/src'
Cleanup: may take some time... OK
Starting test server at port 11111
[ready]: 58210
Testing unit/printver
[ready]: 58219
Testing unit/dump
[ready]: 58213
Testing unit/auth
[ready]: 58216
Testing unit/protocol
[ready]: 58222
Testing unit/keyspace
[ready]: 58225
Testing unit/scan
[ready]: 58228
Testing unit/type/string
[ready]: 58230
Testing unit/type/incr
[ready]: 58234
Testing unit/type/list
[ready]: 58237

Finalmente se ejecuta el make install para dejar todos los binarios en el sistema:
[root@bci-bluemixdbredis01 redis-3.2.8]# make install
cd src && make install
make[1]: Entering directory `/dbprdRedis/redis-3.2.8/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/dbprdRedis/redis-3.2.8/src'

Creamos el siguiente directorio y copiamos el siguiente archivo para proceder a editar:
[root@bci-bluemixdbredis01 redis-3.2.8]# mkdir /etc/redis
[root@bci-bluemixdbredis01 redis-3.2.8]# cp redis.conf /etc/redis
[root@bci-bluemixdbredis01 redis-3.2.8]# vi  /etc/redis/redis.conf


En dicho archivo modificaremos el parámetro supervised al valor systemd

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

Modificaremos también el parámetro dir para señalar dónde se almacenarán nuestros dumps o archivos de bases de datos (que en mi caso es un disco por si solo)

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /dbprdRedis/bd

Modificaremos también el parámetro requirepass para que nos pida pass al momento de conectarnos al cliente:
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass redis2017



Procederemos a crear luego el siguiente archivo:
[root@bci-bluemixdbredis01 bd]# vi /etc/systemd/system/redis.service
[root@bci-bluemixdbredis01 bd]#

Con el siguiente contenido:
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target


Ahora procederemos a crear el usuario redis:
[root@bci-bluemixdbredis01 bd]# mkdir /home/redis
[root@bci-bluemixdbredis01 bd]# chown redis:redis /home/redis
[root@bci-bluemixdbredis01 bd]# adduser --system -d /home/redis redis
[root@bci-bluemixdbredis01 bd]# echo secu13re | passwd --stdin redis
Changing password for user redis.
passwd: all authentication tokens updated successfully.
[root@bci-bluemixdbredis01 bd]# usermod -c 'redis_database_not_delete' redis
[root@bci-bluemixdbredis01 bd]# usermod -s /bin/bash redis

[root@bci-bluemixdbredis01 ~]# chmod 700  /home/redis
 


Ajustamos los permisos para la carpeta de base de datos:
[root@bci-bluemixdbredis01 bd]# chown redis:redis /dbprdRedis/bd
[root@bci-bluemixdbredis01 bd]# chmod 770 /dbprdRedis/bd
[root@bci-bluemixdbredis01 bd]#


Probamos que el sistema levante sin problemas el servicio redis:
[root@bci-bluemixdbredis01 bd]# systemctl start redis
[root@bci-bluemixdbredis01 bd]# ps -fea | grep -i redis
redis    61755     1  0 11:27 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
root     61759 55077  0 11:27 pts/0    00:00:00 grep --color=auto -i redis
[root@bci-bluemixdbredis01 bd]# systemctl status redis
● redis.service - Redis In-Memory Data Store
  Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled)
  Active: active (running) since Tue 2017-05-23 11:27:06 CLT; 15s ago
Main PID: 61755 (redis-server)
  CGroup: /system.slice/redis.service
          └─61755 /usr/local/bin/redis-server 127.0.0.1:6379

May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: |    `-._`-._        _.-'_.-'    |
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: `-._    `-._`-.__.-'_.-'    _.-'
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: `-._    `-.__.-'    _.-'
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: `-._        _.-'
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: `-.__.-'
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: 61755:M 23 May 11:27:06.589 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: 61755:M 23 May 11:27:06.589 # Server started, Redis version 3.2.8
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: 61755:M 23 May 11:27:06.589 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.ov...o take effect.
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: 61755:M 23 May 11:27:06.589 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Re...
May 23 11:27:06 bci-bluemixdbredis01 redis-server[61755]: 61755:M 23 May 11:27:06.589 * The server is now ready to accept connections on port 6379
Hint: Some lines were ellipsized, use -l to show in full.
[root@bci-bluemixdbredis01 bd]#


Ahora probaremos si funciona convirtiéndonos en el usuario de so redis:
[root@bci-bluemixdbredis01 bd]# su - redis
Last login: Tue May 23 11:33:03 CLT 2017 on pts/0
-bash-4.2$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set test "hola mundo"
OK
127.0.0.1:6379> get test
"hola mundo"
127.0.0.1:6379>

Finalmente dejaremos configurado redis para que inicie (esto lo ejecutamos como usuario root):
[root@bci-bluemixdbredis01 bd]# sudo systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
[root@bci-bluemixdbredis01 bd]#

Con lo anterior ya tendremos nuestro redis configurado.
Saludos. Felipe.