ラベル mongodb の投稿を表示しています。 すべての投稿を表示
ラベル mongodb の投稿を表示しています。 すべての投稿を表示

2018年7月21日土曜日

DockerでMetabase、MongoDB、PostgreSQL10.4をインストールしたコンテナを構築する

Metabaseはオープンソースのデータ解析・可視化ツールです。

○Metabaseの画面


○構築方法
1.Metabase、MongoDB、PostgreSQL10.4をインストールしたコンテナを構築するには、以下のコマンドを実行します。
docker-compose up -d

docker-compose.yml
services:
  metabase:
    image: metabase/metabase
    container_name: "metabase"
    volumes:
      - "metabase-data:/metabase-data"
    environment:
      MB_DB_TYPE: postgres
      MB_DB_DBNAME: metabase
      MB_DB_PORT: 5432
      MB_DB_USER: postgres
      MB_DB_PASS: metabase
      MB_DB_HOST: db
    ports:
      - "3000:3000"
    depends_on:
      - db
      - mongodb
  db:
    image: postgres:10.4-alpine
    container_name: "metabase-db"
    ports:
      - "5432:5432"
    volumes:
      - "db-data:/var/lib/postgresql/data"
    environment:
        POSTGRES_DB: metabase
        POSTGRES_PASSWORD: metabase
  mongodb:
    image: mongo:3.7-jessie
    container_name: "mongodb"
    volumes:
      - "mongodb-data:/data/db"
      - "mongodb-configdb:/data/configdb"
    environment:
      MONGO_INITDB_ROOT_USERNAME: mongo
      MONGO_INITDB_ROOT_PASSWORD: mongo
    ports:
      - 27017:27017
volumes:
  mongodb-data:
    driver: local
  mongodb-configdb:
    driver: local
  db-data:
    driver: local
  metabase-data:
    driver: local

2.ブラウザからブラウザからhttp://<DockerホストのIP>:3000/にアクセスします。
ユーザ情報を入力します。


3.データベースのタイプを選択します


4.接続データベース情報を以下のように入力します
Database type: PostgreSQL
Host: db
Port: 5432
Database name: metabase
Database username: postgres
Database password: metabase
これ以降はお好みで選択します。


5.設定完了後、Admin PanelのAdd databaseをクリックして、mongodbを追加します。
Database type: MongoDB
Host: mongodb
Port: 27017
Database name: admin
Database username: mongo
Database password: mongo



○関連情報
・Metabaseに関する他の記事はこちらを参照してください。

2018年7月18日水曜日

PortainerでJupyter LabとMongoDBのスタックを作成する

Jupyter Notebookでインタラクティブなコンピューティング環境を提供する事ができます。Portainerで簡単にwebブラウザからJupyter LabとMongoDBのスタックを作成する事ができます。

○Jupyter Labの画面


〇構築方法
1.Portainer画面横のImagesをクリックして、Image list画面からBuild a new imageボタンをクリック


2.Build image画面nameフィールドにmyjupyter-mongodbを入力

3.Web editorのテキストエリアに以下を貼り付け
FROM jupyter/scipy-notebook
ENV TZ=Asia/Tokyo
USER root
RUN pip install pymongo
USER $NB_UID


貼り付け後、build the image ボタンをクリックします。

4.画面横のStacksをクリック後、Stacks list画面でAdd a stackボタンをクリックします


5.Create stack画面で、Nameフィールドにjupyterlab-mongodb-stackを入力し、Web editorに以下を張り付ける
version: "3"
services:
  myjupyter:
    image: myjupyter-mongodb
    container_name: "myjupyter-mongodb"
    volumes:
      - "myjupyter-data:/home/jovyan/work"
    ports:
      - "8888:8888"
    environment:
      JUPYTER_TOKEN: jupyter
      JUPYTER_ENABLE_LAB: 1
    depends_on:
      - db
  db:
    image: mongo:3.7-jessie
    container_name: "mongodb"
    volumes:
      - "mongodb-data:/data/db"
      - "mongodb-configdb:/data/configdb"
    ports:
      - 27017:27017
volumes:
  mongodb-data:
    driver: local
  mongodb-configdb:
    driver: local
  myjupyter-data:
    driver: local



貼り付け後、deploy stackボタンをクリックします。

6.ブラウザから以下のURLにアクセス
http://<Dockerホスト名またはIP>:8888/?token=jupyter

〇動作検証用コード
import pymongo

client = pymongo.MongoClient('mongodb://db:27017/')
db = client.testdb
result = db.test.insert_one({"message_id":"100", "message":"helo world!"})
print(result)
for data in db.test.find():
  print(data)

○関連情報
・Portainerに関する他の記事はこちらを参照してください。

2018年7月3日火曜日

VagrantでJupyter LabとMongoDBをインストールした仮想マシン(CentOS7.4)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。

〇Jupyter Labの画面


仮想マシンの構築方法

以下のVagrantfileで、Jupyter LabとMongoDBをインストールした仮想マシン(CentOS7.4)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74jupyterlabmongodb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74jupyterlabmongodb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.105", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.105", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8

yum -y install curl

cat << EOF > /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case \\$1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > \\${thp_path}/enabled
    echo 'never' > \\${thp_path}/defrag

    re='^[0-1]+$'
    if [[ \\$(cat \\${thp_path}/khugepaged/defrag) =~ \\$re ]]
    then
      # RHEL 7
      echo 0  > \\${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > \\${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
EOF
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
/etc/init.d/disable-transparent-hugepages start

cat << EOF > /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
EOF
yum install -y mongodb-org

mkdir -p /srv/mongodb/
openssl rand -base64 741 > /srv/mongodb/mongodb-keyfile
chmod 600 /srv/mongodb/mongodb-keyfile
chown mongod:mongod /srv/mongodb/mongodb-keyfile

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF | mongo
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF


echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
echo '  keyFile: /srv/mongodb/mongodb-keyfile' >> /etc/mongod.conf
sed -i -e 's/bindIp: 127.0.0.1/bindIp: 192.168.55.105/' /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test user.
cat << EOF | mongo --host 192.168.55.105 -u "admin" -p "admin" --authenticationDatabase "admin"
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF

# create sample data
cat << EOF | mongo --host 192.168.55.105 -u "test" -p "test" --authenticationDatabase "test"
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find();
EOF



# install anaconda
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab

# install pymongo
pip install pymongo

useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8080
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
ExecStartPre=source /opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter

echo 'access -> http://192.168.55.105:8080/?token=jupyter'
SHELL
end

〇動作確認用コード
import pymongo

# connect to mongodb
client = pymongo.MongoClient('mongodb://test:test@192.168.55.105/test')

db = client.test
products = db.products

for data in products.find():
  print(data)


関連情報

・Jupyterに関する他の情報はこちらを参照してください。

2018年6月20日水曜日

Dockerでmongo-express、MongoDBがインストールされたコンテナを構築する

mongo-expressはMongoDBのwebインターフェイスです

〇mongo-expressの画面

ブラウザからhttp://<Dockerホスト名またはIP>:8081/にアクセスします。

〇構築方法
1.以下のコマンドでmongo-expressとMongoDBのコンテナを構築する事ができます。
docker-compose up -d
※MongoDBより先にmongo-expressが立ち上がってエラーとなった場合は再実行します

docker-compose.yml
version: "2"
services:
  mongoexpress:
    image: mongo-express
    container_name: "mongo-express"
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: mongo
      ME_CONFIG_MONGODB_ADMINPASSWORD: mongo
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_SERVER: db
    ports:
      - 8081:8081
    depends_on:
      - db
  db:
    image: mongo:3.7-jessie
    container_name: "mongodb"
    volumes:
      - "mongodb-data:/data/db"
      - "mongodb-configdb:/data/configdb"
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: mongo
      MONGO_INITDB_ROOT_PASSWORD: mongo
volumes:
  mongodb-data:
    driver: local
  mongodb-configdb:
    driver: local

2018年4月12日木曜日

dockerでJupyter Notebook、MongoDBがインストールされたコンテナのイメージを作成する

Jupyter Notebookでインタラクティブなコンピューティング環境を提供する事ができます。
以下の手順で、Jupyter Notebook、MongoDBのコンテナを構築・実行します。

○構築方法
1. mysqlclientを追加したJupyter Notebookイメージの作成(Dockerfileがあるフォルダで実行)
docker build -t scipy-notebook-mongodb .

Dockerfile
FROM jupyter/scipy-notebook
ENV TZ=Asia/Tokyo
USER root
RUN pip install pymongo
USER $NB_UID

2. Jupyter NotebookとMongoDBコンテナの構築・実行(docker-compose.ymlがあるフォルダで実行)
docker-compose up -d

docker-compose.yml
version: "2"
services:
  scipy-notebook:
    image: scipy-notebook-mongodb
    container_name: "scipy-notebook-mongodb"
    volumes:
      - "scipy-notebook-data:/home/jovyan/work"
    ports:
      - "8888:8888"
    environment:
      JUPYTER_TOKEN: jupyter
    depends_on:
      - db
  db:
    image: mongo:3.7-jessie
    container_name: "mongodb"
    volumes:
      - "mongodb-data:/data/db"
      - "mongodb-configdb:/data/configdb"
    ports:
      - 27017:27017
volumes:
  mongodb-data:
    driver: local
  mongodb-configdb:
    driver: local
  scipy-notebook-data:
    driver: local

※Jupyter Labを使用したい場合は、以下のようにenvironmentにJUPYTER_ENABLE_LAB: 1を追加します。
    environment:
      JUPYTER_TOKEN: jupyter
      JUPYTER_ENABLE_LAB: 1

3.ブラウザから以下のURLにアクセス
http://<Dockerホスト名またはIP>:8888/?token=jupyter

〇動作確認用コード
import pymongo

client = pymongo.MongoClient('mongodb://db:27017/')
db = client.testdb
result = db.test.insert_one({"message_id":"100", "message":"helo world!"})
print(result)
for data in db.test.find():
  print(data)

○Jupyter Notebookの画面

2018年3月17日土曜日

VagrantでJupyter LabとMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。
以下のVagrantfileで、Jupyter LabとMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604jupyterlabmongodb36"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604jupyterlabmongodb36"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.6.list
apt-get update
apt-get -y install mongodb-org
systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done
cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test"  << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js


# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab

# install pymongo
pip install pymongo

useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8080
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
ExecStartPre=source /opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter

echo 'access -> http://192.168.55.101:8080/?token=jupyter'

SHELL
end

〇動作確認用コード
import pymongo

# connect to mongodb
client = pymongo.MongoClient('mongodb://test:test@localhost/test')

db = client.test
products = db.products

for data in products.find():
  print(data)

〇Jupyter Labの画面

2018年1月29日月曜日

VagrantとMongo ExpressとMongoDB3.6がインストールされた仮想マシン(Ubutu16.04)を構築する

以下のVagrantfileを使用して、Mongo ExpressとMongoDB3.6をインストールした仮想マシン(Ubutu16.04)を構築する事ができます。
仮想マシン構築後、ブラウザからhttp://192.168.55.101:8081/にアクセスします。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mongodb36express"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mongodb36express"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.6.list
apt-get update
apt-get -y install mongodb-org
systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done
cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test"  << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js

# install nodejs and mongo-express
apt-get -y install curl git
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash
apt-get -y install nodejs
npm install -g mongo-express

cp /usr/lib/node_modules/mongo-express/config.default.js /usr/lib/node_modules/mongo-express/config.js
cat << EOF > /etc/systemd/system/mongo-express.service
[Unit]
Description=mongo-express

[Service]
Type=simple
Environment="VCAP_APP_HOST=0.0.0.0"
ExecStart=/usr/bin/mongo-express -u test -p test -d test -H localhost

[Install]
WantedBy=multi-user.target
EOF
systemctl enable mongo-express.service
systemctl start mongo-express.service

echo 'access to http://192.168.55.101:8081/';


SHELL
end

〇Mongo Expressの画面


2018年1月28日日曜日

VagrantでadminMongoとMongoDB3.6がインストールした仮想マシン(Ubuntu16.04)を構築する

adminMongoでwebブラウザからMongoDBを管理する事ができます。以下のVagrantfileを使用して、adminMongoとMongoDB3.6がインストールされた仮想マシン(Ubuntu16.04)を構築する事ができます。
仮想マシン後構築後、ブラウザからhttp://192.168.55.101:1234にアクセスします。
testデータベースへの接続文字列にはmongodb://test:test@192.168.55.101/testを指定します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mongodb36adminmongo"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mongodb36adminmongo"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.6.list
apt-get update
apt-get -y install mongodb-org
systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done
cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test"  << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js

# install node
apt-get -y install curl git
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash
apt-get -y install nodejs

cd /opt
git clone https://github.com/mrvautin/adminMongo.git
cd adminMongo
npm install

cat << EOF > /etc/systemd/system/adminmongo.service
[Unit]
Description=adminmongo
Requires=network.target
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/adminMongo
ExecStart=/usr/bin/npm start

[Install]
WantedBy=multi-user.target
EOF
systemctl enable adminmongo.service
systemctl start adminmongo.service

echo 'access http://192.168.55.101:1234';
echo 'add a connection -> mongodb://test:test@localhost/test'

SHELL
end

〇adminMongoの画面


2018年1月22日月曜日

VagrantでadminMongoとMongoDB3.6をインストールした仮想マシン(CentOS7.4)を構築する

adminMongoでwebブラウザからMongoDBを管理する事ができます。以下のVagrantfileを使用して、adminMongoとMongoDB3.6がインストールされた仮想マシン(CentOS7.4)を構築する事ができます。
仮想マシン後構築後、ブラウザからhttp://192.168.55.104:1234にアクセスします。
testデータベースへの接続文字列にはmongodb://test:test@192.168.55.104/testを指定します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74mongodb36adminmongo"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74mongodb36adminmongo"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
yum -y install curl git

cat << EOF > /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case \\$1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > \\${thp_path}/enabled
    echo 'never' > \\${thp_path}/defrag

    re='^[0-1]+$'
    if [[ \\$(cat \\${thp_path}/khugepaged/defrag) =~ \\$re ]]
    then
      # RHEL 7
      echo 0  > \\${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > \\${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
EOF
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
/etc/init.d/disable-transparent-hugepages start

cat << EOF > /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
EOF
yum install -y mongodb-org

mkdir -p /srv/mongodb/
openssl rand -base64 741 > /srv/mongodb/mongodb-keyfile
chmod 600 /srv/mongodb/mongodb-keyfile
chown mongod:mongod /srv/mongodb/mongodb-keyfile

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF | mongo
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF


echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
echo '  keyFile: /srv/mongodb/mongodb-keyfile' >> /etc/mongod.conf
sed -i -e 's/bindIp: 127.0.0.1/bindIp: 192.168.55.104/' /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test user.
cat << EOF | mongo --host 192.168.55.104 -u "admin" -p "admin" --authenticationDatabase "admin"
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF

# create sample data
cat << EOF | mongo --host 192.168.55.104 -u "test" -p "test" --authenticationDatabase "test"
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find();
EOF


curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum -y install nodejs

cd /opt
git clone https://github.com/mrvautin/adminMongo.git
cd adminMongo
npm install

cat << EOF > /etc/systemd/system/adminmongo.service
[Unit]
Description=adminmongo
Requires=network.target
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/adminMongo
ExecStart=/usr/bin/npm start

[Install]
WantedBy=multi-user.target
EOF
systemctl enable adminmongo.service
systemctl start adminmongo.service

echo 'access http://192.168.55.104:1234';
echo 'add a connection -> mongodb://test:test@192.168.55.104/test'

SHELL
end

〇adminMongoの画面


2018年1月15日月曜日

VagrantでMongo ExpressとMongoDB3.6がインストールされた仮想マシン(CentOS7.4)を構築する

以下のVagrantfileを使用して、Mongo ExpressとMongoDB3.6をインストールした仮想マシン(CentOS7.4)を構築する事ができます。
仮想マシン構築後、ブラウザからhttp://192.168.55.104:8081/にアクセスします。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74mongodb36express"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74mongodb36express"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
#yum -y install unzip
#useradd mongod
yum -y install curl

cat << EOF > /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case \\$1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > \\${thp_path}/enabled
    echo 'never' > \\${thp_path}/defrag

    re='^[0-1]+$'
    if [[ \\$(cat \\${thp_path}/khugepaged/defrag) =~ \\$re ]]
    then
      # RHEL 7
      echo 0  > \\${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > \\${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
EOF
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
/etc/init.d/disable-transparent-hugepages start

cat << EOF > /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
EOF
yum install -y mongodb-org

mkdir -p /srv/mongodb/
openssl rand -base64 741 > /srv/mongodb/mongodb-keyfile
chmod 600 /srv/mongodb/mongodb-keyfile
chown mongod:mongod /srv/mongodb/mongodb-keyfile

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF | mongo 
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF


echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
echo '  keyFile: /srv/mongodb/mongodb-keyfile' >> /etc/mongod.conf
sed -i -e 's/bindIp: 127.0.0.1/bindIp: 192.168.55.104/' /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test user.
cat << EOF | mongo --host 192.168.55.104 -u "admin" -p "admin" --authenticationDatabase "admin"
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF

# create sample data
cat << EOF | mongo --host 192.168.55.104 -u "test" -p "test" --authenticationDatabase "test"
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find();
EOF

# install nodejs and mongo-express
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum -y install nodejs
npm install -g mongo-express

cp /usr/lib/node_modules/mongo-express/config.default.js /usr/lib/node_modules/mongo-express/config.js
cat << EOF > /etc/systemd/system/mongo-express.service
[Unit]
Description=mongo-express

[Service]
Type=simple
Environment="VCAP_APP_HOST=0.0.0.0"
ExecStart=/bin/mongo-express -u test -p test -d test -H 192.168.55.104

[Install]
WantedBy=multi-user.target
EOF
systemctl enable mongo-express.service
systemctl start mongo-express.service

echo 'access to http://192.168.55.104:8081/'

SHELL
end

〇Mongo Expressの画面


2018年1月10日水曜日

DockerでadminMongoがインストールされたコンテナ(Alpine 3.7)を構築する

adminMongoはMongoDBに対応するwebフロントエンドです。
以下の手順で、adminMongoとMongoDBのコンテナを構築・実行します。

○構築方法
1. adminMongoイメージの作成(Dockerfileがあるフォルダで実行)
docker build -t adminmongo .

Dockerfile
FROM alpine:3.7
WORKDIR /
RUN  apk update \
  && apk add --no-cache nodejs nodejs-npm git \
  && rm -rf /var/cache/apk/* \
  && mkdir -p /opt \
  && git clone https://github.com/mrvautin/adminMongo.git /opt/adminMongo
WORKDIR /opt/adminMongo
RUN npm install
EXPOSE 1234
VOLUME /opt/adminMongo
CMD ["/usr/bin/npm", "start"]

2. adminMongoとMongoDBコンテナの構築・実行(docker-compose.ymlがあるフォルダで実行)
docker-compose up -d

docker-compose.yml
version: "2"
services:
  adminmongo:
    image: adminmongo
    container_name: "adminmongo"
    volumes:
      - "adminmongo-data:/opt/adminMongo"
    ports:
      - "1234:1234"
    depends_on:
      - db
  db:
    image: mongo:3.6-jessie
    container_name: "mongodb"
    volumes:
      - "mongodb-data:/data/db"
      - "mongodb-configdb:/data/configdb"
    ports:
      - 27017:27017
volumes:
  mongodb-data:
    driver: local
  mongodb-configdb:
    driver: local
  adminmongo-data:
    driver: local

3. ブラウザからhttp://:1234/にアクセス
接続を追加する時に、mongodb://db/を指定します。

○adminmongoの画面


○その他
・adminMongoとMongoDBコンテナの停止・削除(docker-compose.ymlがあるフォルダで実行)
docker-compose down

・永続化ボリュームの削除
docker volume rm adminmongo_adminmongo-data
docker volume rm adminmongo_mongodb-configdb
docker volume rm adminmongo_mongodb-data

2018年1月8日月曜日

VagrantでMongoDB3.6をインストールした仮想マシン(CentOS7.4)を構築する

以下のVagrantfileを使用して、MongoDB3.6がインストールされた仮想マシン(CentOS7.4)を構築する事ができます。
MongoDBのインストールと合わせて、認証の有効化・テストデータの投入も行います。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74mongodb36"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74mongodb36"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
#yum -y install unzip
#useradd mongod
yum -y install curl

cat << EOF > /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case \\$1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > \\${thp_path}/enabled
    echo 'never' > \\${thp_path}/defrag

    re='^[0-1]+$'
    if [[ \\$(cat \\${thp_path}/khugepaged/defrag) =~ \\$re ]]
    then
      # RHEL 7
      echo 0  > \\${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > \\${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
EOF
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
/etc/init.d/disable-transparent-hugepages start

cat << EOF > /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
EOF
yum install -y mongodb-org

mkdir -p /srv/mongodb/
openssl rand -base64 741 > /srv/mongodb/mongodb-keyfile
chmod 600 /srv/mongodb/mongodb-keyfile
chown mongod:mongod /srv/mongodb/mongodb-keyfile

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF | mongo
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF


echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf
echo '  keyFile: /srv/mongodb/mongodb-keyfile' >> /etc/mongod.conf
sed -i -e 's/bindIp: 127.0.0.1/bindIp: 192.168.55.104/' /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test user.
cat << EOF | mongo --host 192.168.55.104 -u "admin" -p "admin" --authenticationDatabase "admin"
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF

# create sample data
cat << EOF | mongo --host 192.168.55.104 -u "test" -p "test" --authenticationDatabase "test"
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find();
EOF

SHELL
end

2018年1月1日月曜日

VagrantでMongoDB3.6をインストールした仮想マシン(Ubuntu16.04)を構築する

以下のVagrantfileを使用して、MongoDB3.6がインストールされた仮想マシン(Ubuntu16.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mongodb36"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mongodb36"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.6.list
apt-get update
apt-get -y install mongodb-org

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test"  << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js

SHELL
end

2017年12月29日金曜日

VagrantでadminMongoとMongoDBがインストールされた仮想マシン(Ubuntu16.04)を構築する

adminMongoでwebブラウザからMongoDBを管理する事ができます。
以下のVagrantfileを使用して、adminMongoとMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築する事ができます。
仮想マシン後構築後、ブラウザからhttp://192.168.55.101:1234にアクセスします。
testデータベースへの接続文字列にはmongodb://test:test@localhost/testを指定します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604adminmongo"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604adminmongo"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.4.list
apt-get update
apt-get -y install mongodb-org

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test"  << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js

# install node
apt-get -y install curl git
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash
apt-get -y install nodejs

cd /opt
git clone https://github.com/mrvautin/adminMongo.git
cd adminMongo
npm install

cat << EOF > /etc/systemd/system/adminmongo.service
[Unit]
Description=adminmongo
Requires=network.target
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/adminMongo
ExecStart=/usr/bin/npm start

[Install]
WantedBy=multi-user.target
EOF
systemctl enable adminmongo.service
systemctl start adminmongo.service

echo 'access http://192.168.55.101:1234'
echo 'add a connection -> mongodb://test:test@localhost/test'
SHELL
end

〇adminMongoの画面1


〇adminMongoの画面2


2017年11月18日土曜日

VagrantでMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築する

以下のVagrantfileを使用して、MongoDBをインストールした仮想マシンを構築する事ができます。
MongoDBのインストールと合わせて、認証の有効化・テストデータの投入も行います。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mongodb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mongodb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.4.list
apt-get update
apt-get -y install mongodb-org

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo '  authorization: enabled' >> /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test"  << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js

SHELL
end

〇関連情報
・Vagrantでmongodbがインストールされた仮想マシン(CentOS7.3)を構築する
http://serverarekore.blogspot.jp/2017/08/vagrantmongodb.html

・Vagrantでmongo-expressがインストールされた仮想マシン(CentOS7.4)を構築する
http://serverarekore.blogspot.jp/2017/11/vagrantmongo-expresscentos74.html

2017年11月13日月曜日

Vagrantでmongo-expressがインストールされた仮想マシン(CentOS7.4)を構築する

以下のVagrantfileを使用して、mongo-expressがインストールされた仮想マシンを構築する事ができます。
仮想マシンが構築された後、ブラウザからhttp://192.168.55.74:8081/でmongo-expressにアクセスします。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74mongodb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74mongodb"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.74", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.74", :netmask => "255.255.255.0"
  config.vm.network "forwarded_port", guest:22, host:19022, id:"ssh"
  config.vm.provision "shell", inline: <<-SHELL
#yum -y install unzip
#useradd mongod
yum -y install curl

cat << EOF > /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
EOF
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
/etc/init.d/disable-transparent-hugepages start

cat << EOF > /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
EOF
yum install -y mongodb-org

mkdir -p /srv/mongodb/
openssl rand -base64 741 > /srv/mongodb/mongodb-keyfile
chmod 600 /srv/mongodb/mongodb-keyfile
chown mongod:mongod /srv/mongodb/mongodb-keyfile
echo 'security:' >> /etc/mongod.conf
echo '  keyFile: /srv/mongodb/mongodb-keyfile' >> /etc/mongod.conf
sed -i -e 's/bindIp: 127.0.0.1/bindIp: 192.168.55.74/' /etc/mongod.conf

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

cat << EOF | mongo
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
echo '  authorization: enabled' >> /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test user.
cat << EOF | mongo -u "admin" -p "admin" --authenticationDatabase "admin"
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF

# create sample data
cat << EOF | mongo -u "test" -p "test" --authenticationDatabase "test"
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find();
EOF

# install nodejs and mongo-express
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum -y install nodejs
npm install -g mongo-express

cp /usr/lib/node_modules/mongo-express/config.default.js /usr/lib/node_modules/mongo-express/config.js
cat << EOF > /etc/systemd/system/mongo-express.service
[Unit]
Description=mongo-express

[Service]
Type=simple
Environment="VCAP_APP_HOST=0.0.0.0"
ExecStart=/bin/mongo-express -u test -p test -d test -H 192.168.55.74

[Install]
WantedBy=multi-user.target
EOF
systemctl enable mongo-express.service
systemctl start mongo-express.service

echo 'access to http://192.168.55.74:8081/';
SHELL
end

〇mongo-expressの画面


〇関連情報
・Vagrantでmongodbがインストールされた仮想マシン(CentOS7.3)を構築する
http://serverarekore.blogspot.jp/2017/08/vagrantmongodb.html

・VagrantでMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築する
http://serverarekore.blogspot.jp/2017/11/vagrantmongodbubuntu1604.html

2017年8月29日火曜日

Vagrantでmongodbがインストールされた仮想マシン(CentOS7.3)を構築する

mongodbがインストールされた仮想マシンを構築するには、以下のVagrantfileを使用します。このスクリプトでは、mongodbのインストールの他に、管理者であるadminユーザ(パスワードはadmin)と、一般ユーザであるtestユーザ(パスワードはtest)も作成し、testデータベースにproductsコレクションにテストデータを登録します。


Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.3"
  config.vm.hostname = "mongodb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "mongodb"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.74", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.74", :netmask => "255.255.255.0"
  config.vm.network "forwarded_port", guest:22, host:19022, id:"ssh"
  config.vm.provision "shell", inline: <<-SHELL
#yum -y install unzip

cp /vagrant/disable-transparent-hugepages /etc/init.d/
chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
/etc/init.d/disable-transparent-hugepages start

cp /vagrant/mongodb-org-3.4.repo /etc/yum.repos.d/mongodb-org-3.4.repo
sudo yum install -y mongodb-org

mkdir -p /srv/mongodb/
openssl rand -base64 741 > /srv/mongodb/mongodb-keyfile
chmod 600 /srv/mongodb/mongodb-keyfile
chown mongod:mongod /srv/mongodb/mongodb-keyfile
echo 'security:' >> /etc/mongod.conf
echo '  keyFile: /srv/mongodb/mongodb-keyfile' >> /etc/mongod.conf

systemctl enable mongod
systemctl start mongod

# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

mongo /vagrant/addusers.js
echo '  authorization: enabled' >> /etc/mongod.conf

systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done

# create a test user.
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /vagrant/createst.js

# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test" /vagrant/sample.js

SHELL
end
mongodb-org-3.4.repo

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
disable-transparent-hugepages

#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
addusers.js

var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
createtest.js

var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
sample.js

var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find();

〇関連情報
・Vagrantでmongo-expressがインストールされた仮想マシン(CentOS7.4)を構築する
http://serverarekore.blogspot.jp/2017/11/vagrantmongo-expresscentos74.html

・VagrantでMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築する
http://serverarekore.blogspot.jp/2017/11/vagrantmongodbubuntu1604.html