以下のVagrantfileで、Jupyter LabとH2 Databaseをインストールした仮想マシン(Ubuntu16.04)を構築できます。
Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "bento/ubuntu-16.04"
config.vm.hostname = "ub1604jupyterlabh2db"
config.vm.provider :virtualbox do |vbox|
vbox.name = "ub1604jupyterlabh2db"
vbox.cpus = 2
vbox.memory = 2048
vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :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 h2 database
apt-get -y install unzip
wget http://www.h2database.com/h2-2018-03-18.zip
unzip h2-2018-03-18.zip
mv h2 /opt
mkdir /opt/h2/data
# setup console as service
cat << EOF > /opt/h2/bin/h2env
H2CUSTOMJARS=
EOF
cat << EOF > /etc/systemd/system/h2.service
[Unit]
Description=H2 database
After=syslog.target network.target
[Service]
Type=simple
EnvironmentFile=/opt/h2/bin/h2env
WorkingDirectory=/opt/h2
ExecStart=/usr/bin/java -cp "/opt/h2/bin/h2-1.4.197.jar:\\$H2CUSTOMJARS" org.h2.tools.Console -tcp -tcpAllowOthers -web -webAllowOthers -pg -pgAllowOthers -baseDir /opt/h2/data
ExecStop=/usr/bin/kill -3 \\${MAINPID}
[Install]
WantedBy=multi-user.target
EOF
systemctl enable h2.service
systemctl start h2.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
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 '--------------------------------------------------'
echo 'access URL: http://192.168.55.106:8082/'
echo 'username: sa default password: sa'
echo 'Driver class: org.h2.Driver'
echo 'JDBC URL: jdbc:h2:tcp://192.168.55.106/~/test'
echo '--------------------------------------------------'
echo 'access -> http://192.168.55.106:8080/?token=jupyter'
SHELL
end
〇動作検証用コード
import psycopg2
con = psycopg2.connect("dbname=test host=localhost port=5435 user=sa password=sa")
try:
cur= con.cursor()
cur.execute("create table messages (message_id integer, message varchar(100))")
cur.execute("insert into messages values (1, 'Hello world!')")
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 Labの画面
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。
・psycopg2に関する他の記事はこちらを参照してください。
0 件のコメント:
コメントを投稿