[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
ラベル Anaconda の投稿を表示しています。 すべての投稿を表示
ラベル Anaconda の投稿を表示しています。 すべての投稿を表示

2021年2月11日木曜日

CentOS 8-StreamにSpyderをインストールする

SpyderはオープンソースのPyton統合開発環境です。

〇Spyderの画面

インストール方法 以下の手順を実行します。
1. Anacondaのインストール
wget https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh

sudo chmod +x Anaconda3-2020.11-Linux-x86_64.sh

sudo ./Anaconda3-2020.11-Linux-x86_64.sh -b -p /opt/anaconda

2. spyderのインストール
sudo bash -c "source /opt/anaconda/bin/activate && pip install --upgrade pip && pip install pyqt5==5.12 PyQtWebEngine==5.12 && pip install spyder"

cat << EOF | sudo tee /usr/share/applications/spyder.desktop
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=bash -c "source /opt/anaconda/bin/activate && /opt/anaconda/bin/spyder"
Icon=/opt/anaconda/share/icons/spyder3.png
Name=Spyder
Categories=Development;
EOF

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

2020年4月27日月曜日

Ubuntu Desktop 20.04にSpyderをインストールする

SpyderはオープンソースのPyton統合開発環境です。

〇Spyderの画面


インストール方法 以下の手順を実行します。
1. Anacondaのインストール
wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh

sudo chmod +x Anaconda3-2020.02-Linux-x86_64.sh

sudo ./Anaconda3-2020.02-Linux-x86_64.sh -b -p /opt/anaconda

2. spyderのインストール
sudo bash -c "source /opt/anaconda/bin/activate && pip install --upgrade pip && pip install spyder"

cat << EOF | sudo tee /usr/share/applications/spyder.desktop
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=bash -c "source /opt/anaconda/bin/activate && /opt/anaconda/bin/spyder"
Icon=/opt/anaconda/share/icons/spyder3.png
Name=Spyder
Categories=Development;
EOF

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

2020年4月12日日曜日

Debian 10(Buster)にSpyderをインストールする

SpyderはオープンソースのPyton統合開発環境です。

〇Spyderの画面

インストール方法 以下の手順を実行します。
1. Anacondaのインストール
wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh

sudo chmod +x Anaconda3-2020.02-Linux-x86_64.sh

sudo ./Anaconda3-2020.02-Linux-x86_64.sh -b -p /opt/anaconda

2. spyderのインストール
sudo bash -c "source /opt/anaconda/bin/activate && pip install --upgrade pip && pip install spyder"

cat << EOF | sudo tee /usr/share/applications/spyder.desktop
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=bash -c "source /opt/anaconda/bin/activate && /opt/anaconda/bin/spyder"
Icon=/opt/anaconda/share/icons/spyder3.png
Name=Spyder
Categories=Development;
EOF

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

2019年5月18日土曜日

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

Jupyter Widgetsで様々なコントロールをnotebookで使用する事ができます。

〇Jupyter Widgetsの画面


構築方法 以下のVagrantfileで、Jupyter LabとJupyter Widgetsをインストールした仮想マシン(CentOS7.6)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.6"
  config.vm.hostname = "co76jupyterlabmariadb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co76jupyterlabmariadb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8
yum install -y epel-release
yum check-update
yum -y update
timedatectl set-timezone Asia/Tokyo

# install node10.x
yum -y install gcc-c++ make

curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs

# install anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh
chmod +x Anaconda3-2018.12-Linux-x86_64.sh
./Anaconda3-2018.12-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab
pip install ipywidgets
jupyter labextension install @jupyter-widgets/jupyterlab-manager

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
c.JupyterApp.allow_remote_access = True
c.JupyterApp.ip = '0.0.0.0'
c.JupyterApp.open_browser = False
c.JupyterApp.port = 8080
c.JupyterApp.token = 'jupyter'
EOF"
cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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.104:8080/?token=jupyter'
SHELL
end

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

2019年5月8日水曜日

VagrantでJupyter Lab、Jupyter Widgetsをインストールした仮想マシン(Debian Stretch/9.6)を構築する

Jupyter Widgetsで様々なコントロールをnotebookで使用する事ができます。

〇Jupyter Widgetsの画面


構築方法 以下のVagrantfileで、Jupyter LabとJupyter Widgetsをインストールした仮想マシン(Debian Stretch/9.6)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.6"
  config.vm.hostname = "db96jupyterwidgets"
config.vm.network "public_network", ip:"192.168.1.107", :netmask => "255.255.255.0"
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db96jupyterwidgets"
     vbox.gui = true
     vbox.cpus = 2
     vbox.memory = 2048
  end
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install task-japanese gawk
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
timedatectl set-timezone Asia/Tokyo

# install node10.x
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt-get install -y nodejs

# install anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh
chmod +x Anaconda3-2018.12-Linux-x86_64.sh
./Anaconda3-2018.12-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab
pip install ipywidgets
jupyter labextension install @jupyter-widgets/jupyterlab-manager

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
c.JupyterApp.allow_remote_access = True
c.JupyterApp.ip = '0.0.0.0'
c.JupyterApp.open_browser = False
c.JupyterApp.port = 8080
c.JupyterApp.token = 'jupyter'
EOF"
cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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.107:8080/?token=jupyter'
SHELL
end

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

2019年3月21日木曜日

VagrantでJupyter LabとPostgreSQL11をインストールした仮想マシン(Debian Stretch/9.6)を構築する

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

〇Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとPostgreSQL11 をインストールした仮想マシン(Debian Stretch/9.6)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.6"
  config.vm.hostname = "db96jupyterpg11"
config.vm.network "public_network", ip:"192.168.1.107", :netmask => "255.255.255.0"
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db96jupyterpg11"
     vbox.gui = true
     vbox.cpus = 2
     vbox.memory = 2048
  end
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install task-japanese gawk
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
timedatectl set-timezone Asia/Tokyo


# install postgresql
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt-get update
apt-get upgrade
apt-get -y install postgresql-11

echo "listen_addresses='*'" >> /etc/postgresql/11/main/postgresql.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/11/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/11/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/11/main/pg_hba.conf

systemctl restart postgresql.service
su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 test
psql -c "
alter user postgres with password 'postgres';
create user test with password 'test';
grant all privileges on database test to test;
"
EOF
export PGPASSWORD=test
psql -h localhost -d test -U test -c "
create table messages (message_id integer not null, message varchar(100));
insert into messages values (1, 'hello world.');
insert into messages values (2, 'test message.');
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service

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

# install psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary

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
c.JupyterApp.allow_remote_access = True
c.JupyterApp.ip = '0.0.0.0'
c.JupyterApp.open_browser = False
c.JupyterApp.port = 8080
c.JupyterApp.token = 'jupyter'
EOF"
cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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.107:8080/?token=jupyter'
SHELL
end

〇動作検証用コード
import psycopg2

con = psycopg2.connect("dbname=test host=localhost user=test password=test")

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except psycopg2.Error as er:
    print('psycopg2.Error:', er)
cur.close
con.close

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

・psycopg2に関する他の記事はこちらを参照してください。

2019年3月13日水曜日

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

Jupyter上でPygalを使用してグラフを描画する事もできます。

〇Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとPygalをインストールした仮想マシン(Ubuntu18.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterpygal"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterpygal"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.109", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.109", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get -y install language-pack-ja
sed -i.bak -e "s#http://archive.ubuntu.com/ubuntu/#http://ftp.riken.jp/pub/Linux/ubuntu/#g" /etc/apt/sources.list
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
timedatectl set-timezone Asia/Tokyo

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

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
c.JupyterApp.allow_remote_access = True
c.JupyterApp.ip = '0.0.0.0'
c.JupyterApp.open_browser = False
c.JupyterApp.port = 8080
c.JupyterApp.token = 'jupyter'
EOF"
cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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.109:8080/?token=jupyter'
SHELL
end

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

2019年3月11日月曜日

VagrantでJupyter Lab、Pygalをインストールした仮想マシン(Debian Stretch/9.6)を構築する

Jupyter上でPygalを使用してグラフを描画する事ができます。

〇Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとPygalをインストールした仮想マシンを構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.6"
  config.vm.hostname = "db96jupyterpygal"
config.vm.network "public_network", ip:"192.168.1.107", :netmask => "255.255.255.0"
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db96jupyterpygal"
     vbox.gui = true
     vbox.cpus = 2
     vbox.memory = 2048
  end
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install task-japanese gawk
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
timedatectl set-timezone Asia/Tokyo


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

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
c.JupyterApp.allow_remote_access = True
c.JupyterApp.ip = '0.0.0.0'
c.JupyterApp.open_browser = False
c.JupyterApp.port = 8080
c.JupyterApp.token = 'jupyter'
EOF"
cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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.107:8080/?token=jupyter'
SHELL
end

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

2018年10月23日火曜日

VagrantでJupyter Lab、foliumとMariaDBをインストールした仮想マシン(Debian Stretch/9.5)を構築する

foliumでJupyter Lab上に地図を簡単に表示できます。

〇Jupyter Labとfoliumの画面


構築方法 以下のVagrantfileで、Jupyter LabとMariaDBをインストールした仮想マシンを構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.5"
  config.vm.hostname = "db95jupyterlabfoliummariadb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db95jupyterlabfoliummariadb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.107", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# install mariadb
echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# 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
pip install folium

# install mysqlclient
apt-get -y install libmariadbclient-dev
pip install mysqlclient

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.107:8080/?token=jupyter'
SHELL
end

○動作確認用コード(MariaDB接続確認)
import MySQLdb

con = MySQLdb.connect(
  user='test',
  passwd='test',
  host='localhost',
  db='test')

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except MySQLdb.Error as er:
    print('MySQLdb.Error:', er)
cur.close
con.close

〇動作確認用コード(folium動作確認)
import folium

fm = folium.Map(location=[35.856999, 139.648849], zoom_start=10)

folium.Marker(location=[35.861729,139.645482], popup='さいたま市').add_to(fm)
folium.Marker(location=[35.975198, 139.752301], popup='春日部市').add_to(fm)
    
fm

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

2018年8月31日金曜日

VagrantでJupyter Lab、wbdata、BokehとMariaDBをインストールした仮想マシン(Ubuntu18.04)を構築する

Jupyter LabでインタラクティブなPython環境、wbdataパッケージで世界銀行へのデータアクセス、Bokehでデータの可視化を行うことができます。

〇Jupyter Labの画面

ブラウザからhttp://<仮想マシンのホスト名またはIP>:8080/?token=jupyterにアクセスします。

構築方法 以下のVagrantfileを使用して、Jupyter Lab、wbdata、BokehとMariaDBをインストールした仮想マシン(Ubuntu18.04)を構築することができます。IPなどは環境に合わせて変更します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabmariadb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabmariadb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :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 mariadb
echo "mariadb-server-10.3 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.3 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

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

# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.2.0-Linux-x86_64.sh
chmod +x Anaconda3-5.2.0-Linux-x86_64.sh
./Anaconda3-5.2.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install jupyterlab
pip install wbdata
jupyter labextension install jupyterlab_bokeh
jupyter labextension list

# install mysqlclient
apt-get -y install libmariadbclient-dev build-essential
pip install mysqlclient

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
EnvironmentFile=/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.104:8080/?token=jupyter'
SHELL
end

関連情報 ・世界銀行のデータを取得・可視化する記事のまとめは以下のページを参照してください。
世界銀行のデータを取得できるwbdataパッケージのまとめ

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月30日土曜日

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

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

○Jupyter Labの画面


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

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74jupyterlabpercona"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74jupyterlabpercona"
     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

# install Percona Server
yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum -y install Percona-Server-server-57
service mysql start

export MYSQL_ROOTPWD='Root123#'
export MYSQL_PWD=`cat /var/log/mysqld.log | awk '/temporary password/ {print $NF}'`
mysql -uroot -p$MYSQL_PWD --connect-expired-password -e "SET PASSWORD = PASSWORD('$MYSQL_ROOTPWD');"
mysql -uroot -p$MYSQL_ROOTPWD --connect-expired-password -e "UNINSTALL PLUGIN validate_password;"
mysql -uroot -p$MYSQL_ROOTPWD --connect-expired-password -e "SET PASSWORD = PASSWORD('root'); FLUSH PRIVILEGES;"
mysql -uroot -e "SET PASSWORD = PASSWORD('root'); FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# 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 mysqlclient
yum -y install epel-release python-devel gcc-c++ openldap-devel openssl-devel mysql-devel
pip install mysqlclient


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 MySQLdb

con = MySQLdb.connect(
  user='test',
  passwd='test',
  host='localhost',
  db='test')

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except MySQLdb.Error as er:
    print('MySQLdb.Error:', er)
cur.close
con.close

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

2018年6月27日水曜日

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

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

〇Jupyter Labの画面


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

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

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

# install mariadb
yum -y install mariadb mariadb-server
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service
mysql -uroot -e "SET PASSWORD = PASSWORD('root'); FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# 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 mysqlclient
yum -y install mariadb-devel
pip install mysqlclient


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.104:8080/?token=jupyter'
SHELL
end

○動作確認用コード
import MySQLdb

con = MySQLdb.connect(
  user='test',
  passwd='test',
  host='localhost',
  db='test')

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except MySQLdb.Error as er:
    print('MySQLdb.Error:', er)
cur.close
con.close

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

2018年6月24日日曜日

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

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

〇Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとOrientDBをインストールした仮想マシン(Ubuntu18.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlaborientdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlaborientdb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :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 java
apt-get -y install openjdk-8-jdk

# download and install orientdb community edition
wget https://s3.us-east-2.amazonaws.com/orientdb3/releases/2.2.35/orientdb-community-importers-2.2.35.tar.gz
tar xvfz orientdb-community-importers-2.2.35.tar.gz
mv orientdb-community-importers-2.2.35 /opt/orientdb

cat << EOF > /etc/systemd/system/orientdb.service
[Unit]
Description=OrientDB

[Service]
Type=simple
ExecStart=/opt/orientdb/bin/server.sh
ExecStop=/opt/orientdb/bin/shutdown.sh
WorkingDirectory=/opt/orientdb
KillMode=process

[Install]
WantedBy=multi-user.target
EOF
sed -i -e 's###' /opt/orientdb/config/orientdb-server-config.xml
systemctl enable orientdb.service
systemctl start orientdb.service
echo 'access http://192.168.55.104:2480'
echo 'user: admin    password: admin'


# 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 pyorient
pip install pyorient


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 = 8888
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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 'jupyter -> http://192.168.55.104:8888/?token=jupyter'
SHELL
end

○動作確認用コード
import pyorient
client = pyorient.OrientDB("192.168.1.104", 2424)
session_id = client.connect("root", "root" )
client.db_create("test", pyorient.DB_TYPE_GRAPH, pyorient.STORAGE_TYPE_MEMORY )
client.db_open("test", "admin", "admin" )
client.command("create class messages extends V")
client.command("create property messages.id Integer" )
client.command("create property messages.message String" )

#insert a row
client.command("insert into messages (id, message) values (10, 'hello world')")
# execute query
records = client.command("select id, message from messages")
for record in records:
    print(str(record.id) + ":" + record.message)

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

2018年6月20日水曜日

VagrantでRabbitMQとManagement Web interface、Anaconda、pikaをインストールした仮想マシン(Debian Stretch/9.4)を構築する

RabbitMQでAMQPプロトコルを使用してメッセージの送受信を行うことができます。

〇RabbitMQの管理画面


構築方法 以下のVagrantfileを使用して、RabbitMQとManagement Web interface、Anaconda、pikaをインストールした仮想マシンを構築することができます。
仮想マシン構築後、ブラウザからhttp://192.168.55.101:15672/にアクセスします。デフォルトユーザはrabbitmq、パスワードはrabbitmqです。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94rabbitmq"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94rabbitmq"
     vbox.cpus = 2
     vbox.memory = 1024
     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
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# install rabbitmq
apt-get -y install rabbitmq-server

rabbitmq-plugins enable rabbitmq_management
systemctl enable rabbitmq-server
systemctl restart rabbitmq-server

# setup management web interface
rabbitmqctl add_user rabbitmq rabbitmq
rabbitmqctl set_user_tags rabbitmq administrator
rabbitmqctl set_permissions -p / rabbitmq ".*" ".*" ".*"

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

# execute a sample program to put a message.
cat << EOF > put-helloworld.py
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
  routing_key='hello',
  body='Hello World!')
print(" put 'Hello World!'")
EOF
python put-helloworld.py

# exeucte a sample program to get a message
cat << EOF > get-helloworld.py
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
method_frame, header_frame, body = channel.basic_get('hello')
if method_frame:
  print(method_frame, header_frame, body)
  channel.basic_ack(method_frame.delivery_tag)
else:
  print('no message')
EOF
python get-helloworld.py

echo 'management web interface : http://192.168.55.101:15672/'
echo 'user : rabbitmq     password rabbitmq'
SHELL
end

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

2018年6月18日月曜日

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

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

〇Jupyter Labの画面

仮想マシン構築後、ブラウザからhttp://192.168.55.104:8888/?token=jupyterにアクセスします。

仮想マシンの構築 以下のVagrantfileで、Jupyter LabとCockroachDBをインストールした仮想マシン(Ubuntu18.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabcockroachdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabcockroachdb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :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 cockroachdb
wget https://binaries.cockroachdb.com/cockroach-v2.0.2.linux-amd64.tgz
tar xvfz cockroach-v2.0.2.linux-amd64.tgz
mv cockroach-v2.0.2.linux-amd64 /opt/cockroach

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

[Service]
Type=forking
WorkingDirectory=/opt/cockroach
ExecStart=/opt/cockroach/cockroach start --insecure --host=192.168.55.104 --background
ExecStop=/opt/cockroach/cockroach quit

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

# execute sample sql statements
/opt/cockroach/cockroach user set maxroach --insecure --host=192.168.55.104
/opt/cockroach/cockroach sql --insecure --host=192.168.55.104 << EOF
CREATE DATABASE test;
CREATE TABLE test.messages (message_id INT PRIMARY KEY, message varchar(100));
INSERT INTO test.messages VALUES (1, 'hello world.');
select * from test.messages;
GRANT ALL ON DATABASE test TO maxroach;
GRANT ALL ON TABLE test.messages TO maxroach;
EOF

# 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 psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary

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 = 8888
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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 'cockroachdb -> http://192.168.55.104:8080'
echo 'jupyter -> http://192.168.55.104:8888/?token=jupyter'
SHELL
end

〇動作検証用コード
import psycopg2

con = psycopg2.connect("dbname=test host=192.168.55.104 user=maxroach port=26257")

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except psycopg2.Error as er:
    print('psycopg2.Error:', er)
cur.close
con.close


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

・psycopg2に関する他の記事はこちらを参照してください。

2018年6月14日木曜日

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

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

○Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとApache Igniteをインストールした仮想マシン(Ubuntu18.04)を構築する事ができます。
Jupyter LabからApache IgniteにODBCプロトコルで接続します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabignite"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabignite"
     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 java
apt-get -y install openjdk-8-jdk

apt-get -y install unzip
wget http://ftp.meisei-u.ac.jp/mirror/apache/dist//ignite/2.4.0/apache-ignite-fabric-2.4.0-bin.zip
unzip apache-ignite-fabric-2.4.0-bin.zip
mv apache-ignite-fabric-2.4.0-bin /opt/ignite

cat << EOF > /etc/systemd/system/ignite.service
[Unit]
Description=Apache Ignite

[Service]
ExecStart=/opt/ignite/bin/ignite.sh
WorkingDirectory=/opt/ignite
KillMode=process
PrivateTmp=true

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

# wait until it starts to listen on 11211 port.
while netstat -lnt | awk '$4 ~ /:11211$/ {exit 1}'; do sleep 10; done
sleep 10

# execute sample statements.
wget http://wiki.metawerx.net/attach/SJSQL/sjsql.java
wget http://wiki.metawerx.net/attach/SJSQL/sjsql.class

cat << EOF >> /home/vagrant/test.sql
create table test1 (test_id integer primary key, test_desc varchar(100));
insert into test1 (test_id, test_desc) values (1, 'hello world!');
insert into test1 (test_id, test_desc) values (2, 'テストだよ');
select * from test1;
EOF
export LC_ALL=ja_JP.UTF-8 && java -cp .:/opt/ignite/libs/ignite-core-2.4.0.jar sjsql org.apache.ignite.IgniteJdbcThinDriver jdbc:ignite:thin://127.0.0.1/ test test /home/vagrant/test.sql


# 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 ignite driver
pip install ignite


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
EnvironmentFile=/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

# build odbc driver and install
apt-get -y install build-essential libtool m4 automake unixodbc unixodbc-dev
cd /opt/ignite/platforms/cpp/
libtoolize && aclocal && autoheader && automake --add-missing && autoreconf
./configure --enable-odbc --disable-node --disable-core
make
make install
odbcinst -i -d -f /opt/ignite/platforms/cpp/odbc/install/ignite-odbc-install.ini
echo '/usr/local/lib' >> /etc/ld.so.conf.d/ignite.conf
ldconfig

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

SHELL
end

〇動作確認用コード
import pyodbc
conn = pyodbc.connect('DRIVER={Apache Ignite};Address=127.0.0.1:10800;CACHE=MyCache',autocommit=True)
conn.setdecoding(pyodbc.SQL_CHAR, 'utf-8')
conn.setdecoding(pyodbc.SQL_WCHAR, 'utf-8')
cur = conn.cursor()
cur.execute("select * from test1")
rows = cur.fetchall()
for row in rows:
  print("%d %s" % (row[0], row[1]))

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

2018年6月12日火曜日

VagrantでJupyter LabとOrientDBをインストールした仮想マシン(Debian Stretch/9.4)を構築する

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

〇Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとOrientDBをインストールした仮想マシン(Debian Stretch/9.4)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94jupyterlaborientdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94jupyterlaborientdb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :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 java
apt-get -y install openjdk-8-jdk

# download and install orientdb community edition
apt-get -y install unzip
wget https://bit.ly/orientdb-ce-2-2-34 -O orientdb-community-2.2.34.zip
unzip orientdb-community-2.2.34.zip
mv orientdb-community-2.2.34 /opt/orientdb

cat << EOF > /etc/systemd/system/orientdb.service
[Unit]
Description=OrientDB

[Service]
Type=simple
ExecStart=/opt/orientdb/bin/server.sh
ExecStop=/opt/orientdb/bin/shutdown.sh
WorkingDirectory=/opt/orientdb
KillMode=process

[Install]
WantedBy=multi-user.target
EOF
sed -i -e 's#<users>#<users><user resources="*" password="root" name="root"/>#' /opt/orientdb/config/orientdb-server-config.xml
systemctl enable orientdb.service
systemctl start orientdb.service

echo 'access http://192.168.55.104:2480'
echo 'user: admin    password: admin'


# 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 pyorient 
pip install pyorient


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 = 8888
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 'jupyter -> http://192.168.55.104:8888/?token=jupyter'
SHELL
end

○動作確認用コード
import pyorient
client = pyorient.OrientDB("192.168.1.104", 2424)
session_id = client.connect("root", "root" )
client.db_create("test", pyorient.DB_TYPE_GRAPH, pyorient.STORAGE_TYPE_MEMORY )
client.db_open("test", "admin", "admin" )
client.command("create class messages extends V")
client.command("create property messages.id Integer" )
client.command("create property messages.message String" )

#insert a row
client.command("insert into messages (id, message) values (10, 'hello world')")
# execute query
records = client.command("select id, message from messages")
for record in records:
    print(str(record.id) + ":" + record.message)

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

2018年5月29日火曜日

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

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

〇Jupyter Labの画面


構築方法 以下のVagrantfileで、Jupyter LabとPostgreSQL をインストールした仮想マシン(Ubuntu18.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabpg"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabpg"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :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 postgresql
apt-get -y install postgresql
echo "listen_addresses='*'" >> /etc/postgresql/9.5/main/postgresql.conf

sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/9.5/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf

su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 test
psql -c "
alter user postgres with password 'postgres';
create user test with password 'test';
grant all privileges on database test to test;
"
export PGPASSWORD=test
psql -h localhost -d test -U test -c "
create table messages (message_id integer not null, message varchar(100));
insert into messages values (1, 'hello world.');
insert into messages values (2, 'test message.');
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service


# 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 psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary

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
EnvironmentFile=/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.104:8080/?token=jupyter'
SHELL
end

〇動作検証用コード
import psycopg2

con = psycopg2.connect("dbname=test host=localhost user=test password=test")

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except psycopg2.Error as er:
    print('psycopg2.Error:', er)
cur.close
con.close


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

・psycopg2に関する他の記事はこちらを参照してください。

2018年5月25日金曜日

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

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

〇Jupyter Labの画面

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

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

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

# install mysql
sudo yum -y remove mariadb-libs
yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum -y install yum-utils
yum -y install mysql mysql-devel mysql-server mysql-utilities
sudo systemctl enable mysqld.service
sudo systemctl start mysqld.service
# change password and create users and databases.
chkconfig mysqld on
service mysqld start
export MYSQL_ROOTPWD='Root123#'
export MYSQL_PWD=`cat /var/log/mysqld.log | awk '/temporary password/ {print $NF}'`
mysql -uroot --connect-expired-password -e "SET PASSWORD = PASSWORD('$MYSQL_ROOTPWD');"
export MYSQL_PWD=$MYSQL_ROOTPWD
export MYSQL_ROOTPWD='root'
mysql -uroot --connect-expired-password -e "UNINSTALL PLUGIN validate_password;"
mysql -uroot --connect-expired-password -e "SET PASSWORD = PASSWORD('$MYSQL_ROOTPWD'); FLUSH PRIVILEGES;"

mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# 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 mysqlclient
pip install mysqlclient


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.104:8080/?token=jupyter'
SHELL
end

○動作確認用コード
import MySQLdb

con = MySQLdb.connect(
  user='test',
  passwd='test',
  host='localhost',
  db='test')

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except MySQLdb.Error as er:
    print('MySQLdb.Error:', er)
cur.close
con.close

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