LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

MySQL V8.0.18のDocument Storeを試してみる(TRANSFAC json データ利用のその0)

Last updated at Posted at 2019-10-24

MySQL8をDocker環境で試してみる

MySQLイメージの取得とコンテナの起動

TRANSFACの含まれるMySQLのデータベースを格納するための領域をホスト上に作成する

次の作業はTRANSFAC 2019.3のダウンロード版のjsonデータを利用するためのものなので、MySQL8の利用そのものには不要

$ mkdir ./TRANSFAC
$ cd ./TRANSFAC
$ tar -zxf TFP_2019.3_data.tar.gz
$ cd ./json

MySQLのrootのパスワードを決めて、SECRET_PASSに定義しておく

export SECRET_PASS="秘密のパスワード"

デーモンモードでMySQLのコンテナを起動する

$ docker run -v "${PWD}/mysql":/var/lib/mysql -v ${PWD}:/data --user ${UID}:`id -g ${USER}` --name mysql8_0_18 -e MYSQL_ROOT_PASSWORD=${SECRET_PASS} -d -p 3307:3306 -p 33060:33060  mysql:8.0.18
623b1223df71810ef9aa437463f62c25e77fa6ac6022d92960bc16fa685f5484
オプション パラメーター 意味
-v "${PWD}/mysql":/var/lib/mysql ホストのカレントディレクトリをコンテナ上の/var/lib/mysqlにマウントして、MySQLのDBのファイルをホスト上のディレクトリに直接書き出して永続化する
-v ${PWD}:/data ホストのカレントディレクトリをコンテナ上の/dataにマウント(登録するjsonファイルをコンテナ上から参照できるようにするため)
--user ${UID}:id -g ${USER} ホスト上のユーザーをコンテナ上のユーザーとして指定
--name mysql8_0_18 コンテナに名前を付与
-e MYSQL_ROOT_PASSWORD=${SECRET_PASS} mysqlのrootのパスワードを指定
-d runコマンドから抜けてもコンテナの動作を停止しないようにデーモンモードを指定
-p 3307:3306 ホストの3307ポートでコンテナの3306ポートに接続
-p 33060:33060 ホストの33060 ポートでコンテナの33060ポートに接続 (MySQL X Protocol) 
mysql:8.0.18 コンテナのイメージ

ホスト上のディレクトリに、コンテナからMySQLのデータが書き込まれているか確認

$ ls -a ./mysql/
#innodb_temp        binlog.000002       client-key.pem      ibtmp1          public_key.pem      undo_002
.           binlog.index        ib_buffer_pool      mysql           server-cert.pem
..          ca-key.pem      ib_logfile0     mysql.ibd       server-key.pem
auto.cnf        ca.pem          ib_logfile1     performance_schema  sys
binlog.000001       client-cert.pem     ibdata1         private_key.pem     undo_001

MySQL Shellのインストール

apt-getを利用するので、rootユーザーで稼働中のMySQLコンテナに接続

$ docker exec -it --user root  mysql8_0_18  /bin/bash
root@623b1223df71:/# 

まずは、ベースOSを確認する

root@623b1223df71:/# uname -a
Linux 623b1223df71 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 GNU/Linux
root@623b1223df71:/# cat /etc/debian_version 
9.11

debian 9.11なのでaptを利用して環境を整える

まずは下準備

root@623b1223df71:/# apt-get update -y
root@623b1223df71:/# apt-get install -y curl wget gdebi-core

https://dev.mysql.com/downloads/repo/apt
を確認すると現時点(Oct 24, 2019)での最新のaptレポジトリはgetmysql-apt-config_0.8.14-1_all.debなので
dpkg を利用してgetmysql-apt-config_0.8.14-1_all.debを登録する

aptの設定だけ必要なので、途中の選択肢では4を選択

root@623b1223df71:/# curl -L -O https://dev.mysql.com/get/mysql-apt-config_0.8.14-1_all.deb
root@623b1223df71:/# dpkg --install mysql-apt-config_0.8.14-1_all.deb   
Selecting previously unselected package mysql-apt-config.
(Reading database ... 11637 files and directories currently installed.)
Preparing to unpack mysql-apt-config_0.8.14-1_all.deb ...
Unpacking mysql-apt-config (0.8.14-1) ...
Setting up mysql-apt-config (0.8.14-1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Configuring mysql-apt-config
----------------------------

MySQL APT Repo features MySQL Server along with a variety of MySQL components. You may select the appropriate product to choose the version that
you wish to receive.

Once you are satisfied with the configuration then select last option 'Ok' to save the configuration, then run 'apt-get update' to load package
list. Advanced users can always change the configurations later, depending on their own needs.

  1. MySQL Server & Cluster (Currently selected: mysql-8.0)  3. MySQL Preview Packages (Currently selected: Disabled)
  2. MySQL Tools & Connectors (Currently selected: Enabled)  4. Ok
Which MySQL product do you wish to configure? 4

Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package mysql-apt-config)
OK

aptをupdate

root@623b1223df71:/# apt-get update
Hit:1 http://security-cdn.debian.org/debian-security stretch/updates InRelease
Hit:2 http://repo.mysql.com/apt/debian stretch InRelease                                          
Ign:3 http://cdn-fastly.deb.debian.org/debian stretch InRelease                     
Hit:4 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease             
Hit:5 http://cdn-fastly.deb.debian.org/debian stretch Release            
Get:7 http://repo.mysql.com/apt/debian stretch/mysql-8.0 Sources [946 B]
Get:8 http://repo.mysql.com/apt/debian stretch/mysql-apt-config amd64 Packages [566 B]
Get:9 http://repo.mysql.com/apt/debian stretch/mysql-tools amd64 Packages [4515 B]
Fetched 6027 B in 3s (1909 B/s)    
Reading package lists... Done

mysql-shellのインストール

root@623b1223df71:/# apt-get install -y mysql-shell

MySQL Shellを利用する

MySQL X protocolを利用し、規定のポート(33060)を利用してMySQLサーバーに接続

root@623b1223df71:/# mysqlsh --mysqlx -u root --password=${MYSQL_ROOT_PASSWORD} -h localhost -P 33060

mysql-shellの売りは、javascript, python, sqlを切り替えて利用する事ができること。標準ではjavascriptの環境となっている

プロンプトもかなり派手な見た目となる

Logger: Tried to log to an uninitialized logger.
MySQL Shell 8.0.18

Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.
WARNING: Using a password on the command line interface can be insecure.
Creating an X protocol session to 'root@localhost:33060'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 8 (X protocol)
Server version: 8.0.18 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost:33060+ ssl  JS > 

pythonモードに移行する

pythonモードに移行する必然性は無いけれど、折角サポートされているpythonモードなので利用して見ることにする

\pyで環境を切り替えて、標準で読み込まれているモジュールを確認する

MySQL [localhost+ ssl] JS> \py
Switching to Python mode...
MySQL [localhost+ ssl] Py> [item for item in dir() if not item.startswith('__')]
[
    "db", 
    "dba", 
    "mysql", 
    "mysqlx", 
    "raw_input", 
    "session", 
    "shell", 
    "sys", 
    "util"
]

mysql-shellに固有のモジュールにはヘルプが含まれていて、help()で参照できる

MySQL [localhost+ ssl] Py> util.help()
NAME
      util - Global object that groups miscellaneous tools like upgrade checker
             and JSON import.

DESCRIPTION
      Global object that groups miscellaneous tools like upgrade checker and
      JSON import.

FUNCTIONS
      check_for_server_upgrade([connectionData][, options])
            Performs series of tests on specified MySQL server to check if the
            upgrade process will succeed.

      configure_oci([profile])
            Wizard to create a valid configuration for the OCI SDK.

      help([member])
            Provides help about this object and it's members

      import_json(file[, options])
            Import JSON documents from file to collection or table in MySQL
            Server using X Protocol session.

      import_table(filename[, options])
            Import table dump stored in filename to target table using LOAD
            DATA LOCAL INFILE calls in parallel connections.

接続確認後、\quit でmysql-shellを終了

MySQL  localhost:33060+ ssl  JS > \quit
Bye!
root@623b1223df71:/# 

今回はここまで:smile:

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up