MySQL クエリ入門
データベース接続
mysql ユーティリティが接続した時など、MySQL サーバーへの接続にはそれぞれ識別子が割り当てられる。
トラブルシューティング時に、データベース管理者にとって役立つ可能性がある。
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.34 MySQL Community Server (GPL)
クエリの送信
クエリが送信されると、以下の点が確認される。
- そのクエリを実行するパーミッションがあるか
- データにアクセスするためのパーミッションがあるか
- 構文が正しいか
OKの場合、クエリオプティマイザに渡される。
クエリオプティマイザは、そのクエリを効率よく実行するための実行プランを選択する。
クエリの完了
クエリの実行完了後、ユーザーに結果セットが返却される。
結果セットとは、行と列からなる新しいテーブルのこと。
mysql> select * from city where Name = 'Tokyo'; +------+-------+-------------+----------+------------+ | ID | Name | CountryCode | District | Population | +------+-------+-------------+----------+------------+ | 1532 | Tokyo | JPN | Tokyo-to | 7980230 | +------+-------+-------------+----------+------------+ 1 row in set (0.01 sec)
挿入ソート InsertionSort
jdk8 の Arrays sort 内で使われていた Traditional(伝統的な)挿入ソート。
https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/java/util/DualPivotQuicksort.java#L225
シンプルな作りの中でのポイントは
- 2つの要素を比較するため、i と j の2つの変数を用意
- int ai = a[i + 1]; と2つ目の要素を保持するところからスタート(1つ目の要素は、1つ目の要素と比較しソート済みと解釈)
- ひとつ前の要素と比較し、並び替えが必要なら繰り上げを繰り返す
import java.util.Random; public class InsertionSort { public static void main(String args[]) { //int[] a = {3,2,1}; Random rand = new Random(); int[] a = new int[10]; for (int i = 0; i < a.length; i++) { a[i] = rand.nextInt(10) + 1; } System.out.println("---Before sort---"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } int left = 0; int right = a.length - 1; insertionSort(a, left, right); System.out.println("---After sort---"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } /** * Sorts the specified range of the array using insertion sort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted */ private static void insertionSort(int[] a, int left, int right) { for (int i = left, j = i; i < right; j = ++i) { int ai = a[i + 1]; while (ai < a[j]) { a[j + 1] = a[j]; if (j-- == left) { break; } } a[j + 1] = ai; } } }
実行結果例
---Before sort--- 3 8 9 1 1 8 2 6 4 6 ---After sort--- 1 1 2 3 4 6 6 8 8 9
Pygame マウスカーソル に合わせて Paddle が動く
def main(): # Initialize pygame screen pygame.init() screen = pygame.display.set_mode(GAME_RECT.size) pygame.display.set_caption("Game Title") # Create sprite group sprite_group = pygame.sprite.RenderUpdates() Paddle.containers = sprite_group Paddle() # Game loop while True: screen.fill((0,0,0)) # fill with black sprite_group.update() sprite_group.draw(screen) pygame.display.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN and event.key == K_ESCAPE: pygame.quit() sys.exit() # Paddle inheritance Sprite class class Paddle(pygame.sprite.Sprite): def __init__(self): # Default sprite group pygame.sprite.Sprite.__init__(self, self.containers) self.image, self.rect = load_image("paddle.png") self.rect.bottom = GAME_RECT.bottom def update(self): self.rect.centerx = pygame.mouse.get_pos()[0] self.rect.clamp_ip(GAME_RECT) def load_image(filename): filename = os.path.join("resource", filename) try: image = pygame.image.load(filename) except pygame.error: print ("Failed to load image:", filename) raise SystemExit() return image, image.get_rect() if __name__ == "__main__": main()
補足
# Create sprite group
sprite_group = pygame.sprite.RenderUpdates()
Paddle.containers = sprite_group
Paddle()
Paddle のインスタンス生成前に、sprite_group を Paddle.containers メンバ変数に登録。
複数のスプライトをグループにまとめると更新と描画がシンプルになる。
class Paddle(pygame.sprite.Sprite): def __init__(self): # Default sprite group pygame.sprite.Sprite.__init__(self, self.containers) self.image, self.rect = load_image("paddle.png") self.rect.bottom = GAME_RECT.bottom
Paddle インスタンス生成時、init() (コンストラクタ)で pygame.sprite.Sprite.__init__()を呼び出し、Spriteを継承。
引数にグループリスト(containers)を渡す。
(自身のメンバ変数をクラスの外部から定義し、インスタンス化時にそれをコンストラクタ内で使用するような書き方に個人的に慣れない)
Pygame Zero スクリーン表示
#!/usr/bin/env python import os import sys import pygame from pygame.locals import * GAME_RECT = Rect(0, 0, 384, 384) def main(): pygame.init() screen = pygame.display.set_mode(GAME_RECT.size) pygame.display.set_caption("Game Title") while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN and event.key == K_ESCAPE: pygame.quit() sys.exit() if __name__ == "__main__": main()
補足
1
#!/usr/bin/env python
envコマンドの効果で、PATH環境変数の通っている場所から、Pythonインタープリタを探索する。
2
#coding: utf-8
Python 3 の場合、ソースが UTF-8 の時は記載は不要(PEP-8 では記載はむしろ非推奨)
3
pygame.Rect
四角形の描写情報を保持したpygameのオブジェクト
4
(u"Game Title")
リテラル表記に違いがあり、uを文字列の前につけなくてはならなかった「Python2のユニコード文字列」に対して「Python3の文字列」はそれが不要。
CentOS 6 curl: (35) SSL connect error
症状
centos6 上 curl でファイルダウンロードしようとしたらエラー発生。
具体的には elastic filebeat をダウンロードしようとしていた。
--verbose でデバッグ
curl で問題が発生したら、--verbose でデバッグしてみる。
$ curl --verbose -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.8.0-linux-x86_64.tar.gz * About to connect() to artifacts.elastic.co port 443 (#0) * Trying 2a04:4e42:1a::734... connected * Connected to artifacts.elastic.co (2a04:4e42:1a::734) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * NSS error -12190 * Error in TLS handshake, trying SSLv3... > GET /downloads/beats/filebeat/filebeat-7.8.0-linux-x86_64.tar.gz HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: artifacts.elastic.co > Accept: */* > % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connection died, retrying a fresh connect 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Closing connection #0 * Issue another request to this URL: 'https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.8.0-linux-x86_64.tar.gz' * About to connect() to artifacts.elastic.co port 443 (#0) * Trying 2a04:4e42:1a::734... connected * Connected to artifacts.elastic.co (2a04:4e42:1a::734) port 443 (#0) * TLS disabled due to previous handshake failure * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * NSS error -12286 * Closing connection #0 * SSL connect error curl: (35) SSL connect error
以下のメッセージから、TLS handshake に失敗しているのが分かる。
TLS1.2 以上でないとダメだが、nss のバージョンが古い可能性。
* NSS error -12190 * Error in TLS handshake, trying SSLv3...
NSSバージョン確認
CentOS6系では、curlの暗号化ライブラリにNSSが標準で用いられていますが、このバージョンが、3.19.1-6以降であればTLS1.2に標準で対応している。
1) curl や nss のバージョン
$ rpm -qa --queryformat="%{NAME:shescape},%{VERSION:shescape}\n" | egrep -i -w "curl|nss|libcurl" 'nss-softokn','3.14.3' 'libcurl','7.19.7' 'nss-tools','3.18.0' 'nss-softokn-freebl','3.14.3' 'nss-sysinit','3.18.0' 'curl','7.19.7' 'nss-util','3.18.0' 'nss','3.18.0'
2) nss のバージョン
現在のバージョンと最新バージョンはこのコマンドで調べた方が便利。
$ yum info nss (省略) Installed Packages Name : nss Arch : x86_64 Version : 3.18.0 Release : 5.3.el6_6 Size : 2.6 M Repo : installed From repo : updates Summary : Network Security Services URL : http://www.mozilla.org/projects/security/pki/nss/ License : MPLv2.0 Description : Network Security Services (NSS) is a set of libraries designed to : support cross-platform development of security-enabled client and : server applications. Applications built with NSS can support SSL v2 : and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509 : v3 certificates, and other security standards. Available Packages Name : nss Arch : i686 Version : 3.44.0 Release : 7.el6_10 Size : 889 k Repo : updates Summary : Network Security Services URL : http://www.mozilla.org/projects/security/pki/nss/ License : MPLv2.0 Description : Network Security Services (NSS) is a set of libraries designed to : support cross-platform development of security-enabled client and : server applications. Applications built with NSS can support SSL v2 : and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509 : v3 certificates, and other security standards.
NSSバージョンアップ
$ yum update nss (省略) Updated: nss.x86_64 0:3.44.0-7.el6_10 Dependency Updated: nspr.x86_64 0:4.21.0-1.el6_10 nss-softokn.x86_64 0:3.44.0-6.el6_10 nss-softokn-freebl.x86_64 0:3.44.0-6.el6_10 nss-sysinit.x86_64 0:3.44.0-7.el6_10 nss-tools.x86_64 0:3.44.0-7.el6_10 nss-util.x86_64 0:3.44.0-1.el6_10 Complete!
$ yum info nss Installed Packages Name : nss Arch : x86_64 Version : 3.44.0 Release : 7.el6_10 Size : 2.6 M Repo : installed From repo : updates Summary : Network Security Services URL : http://www.mozilla.org/projects/security/pki/nss/ License : MPLv2.0 Description : Network Security Services (NSS) is a set of libraries designed to : support cross-platform development of security-enabled client and : server applications. Applications built with NSS can support SSL v2 : and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509 : v3 certificates, and other security standards. Available Packages Name : nss Arch : i686 Version : 3.44.0 Release : 7.el6_10 Size : 889 k Repo : updates Summary : Network Security Services URL : http://www.mozilla.org/projects/security/pki/nss/ License : MPLv2.0 Description : Network Security Services (NSS) is a set of libraries designed to : support cross-platform development of security-enabled client and : server applications. Applications built with NSS can support SSL v2 : and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509 : v3 certificates, and other security standards. ------------------------------ $ rpm -qa --queryformat="%{NAME:shescape},%{VERSION:shescape}\n" | egrep -i -w "curl|nss|libcurl" 'nss','3.44.0' 'libcurl','7.19.7' 'nss-util','3.44.0' 'nss-softokn','3.44.0' 'nss-sysinit','3.44.0' 'curl','7.19.7' 'nss-softokn-freebl','3.44.0' 'nss-tools','3.44.0'
あらためてダウンロード
エラーなく動作します。
$ curl --verbose -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.8.0-linux-x86_64.tar.gz * About to connect() to artifacts.elastic.co port 443 (#0) * Trying 2a04:4e42:1a::734... connected * Connected to artifacts.elastic.co (2a04:4e42:1a::734) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 * Server certificate: * subject: CN=*.elastic.co,O="Elasticsearch, Inc.",L=Mountain View,ST=California,C=US * start date: Mar 07 00:00:00 2019 GMT * expire date: Apr 22 12:00:00 2021 GMT * common name: *.elastic.co * issuer: CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US > GET /downloads/beats/filebeat/filebeat-7.8.0-linux-x86_64.tar.gz HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: artifacts.elastic.co > Accept: */* > % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0< HTTP/1.1 200 OK < Connection: keep-alive < Content-Length: 28738425 < Last-Modified: Thu, 18 Jun 2020 12:38:25 GMT < ETag: "8e22cc92de1c2d832feed6e6a5642b08" < Content-Type: application/x-gzip < Accept-Ranges: bytes < Age: 102 < Accept-Ranges: bytes < Date: Mon, 20 Jul 2020 01:19:34 GMT < Via: 1.1 varnish < X-Served-By: cache-tyo19925-TYO < X-Cache: HIT < X-Cache-Hits: 0 < X-Timer: S1595207975.571326,VS0,VE1 < server: ElasticInfrastructure < { [data not shown] 100 27.4M 100 27.4M 0 0 10.1M 0 0:00:02 0:00:02 --:--:-- 11.0M* Connection #0 to host artifacts.elastic.co left intact * Closing connection #0
補足
curl
サーバとのデータの転送のやりとりに使われるツール。
NSS
https://ja.wikipedia.org/wiki/Network_Security_Services
rpm - RPM パッケージマネージャ
レッドハットが開発したソフトウェアのパッケージを管理するためのシステム (パッケージ管理システム)、及びコマンド
SSLと『TLS』
https://www.idcf.jp/rentalserver/aossl/basic/ssl-tls/
CentOS 7 に Docker, docker-compose インストール
Install docker
公式ドキュメント
https://docs.docker.com/engine/install/centos/
Set up the repository
1. yum-utils のインストール (未インストールの場合)
sudo yum install -y yum-utils
2. docker のリポジトリが登録されていないことの確認
yum repolist all | grep docker ->(empty)
3. docker のリポジトリを追加
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
4. docker のリポジトリが追加されたことの確認
docker-ce-stable/x86_64 以外はデフォルトで disable になっている。
yum repolist all | grep docker docker-ce-edge/x86_64 Docker CE Edge - x86_64 disabled docker-ce-edge-debuginfo/x86_64 Docker CE Edge - Debuginfo x disabled docker-ce-edge-source Docker CE Edge - Sources disabled docker-ce-nightly/x86_64 Docker CE Nightly - x86_64 disabled docker-ce-nightly-debuginfo/x86_64 Docker CE Nightly - Debuginf disabled docker-ce-nightly-source Docker CE Nightly - Sources disabled docker-ce-stable/x86_64 Docker CE Stable - x86_64 enabled: 79 docker-ce-stable-debuginfo/x86_64 Docker CE Stable - Debuginfo disabled docker-ce-stable-source Docker CE Stable - Sources disabled docker-ce-test/x86_64 Docker CE Test - x86_64 disabled docker-ce-test-debuginfo/x86_64 Docker CE Test - Debuginfo x disabled docker-ce-test-source Docker CE Test - Sources disabled
5. docker engine のインストール
sudo yum install docker-ce docker-ce-cli containerd.io
6. インストールされたパッケージ(docker-ce)確認
yum list docker-ce --showduplicates | sort -r * updates: ty1.mirror.newmediaexpress.com Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror, langpacks Installed Packages * extras: ty1.mirror.newmediaexpress.com * epel: nrt.edge.kernel.org docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.12-3.el7 @docker-ce-stable docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: ty1.mirror.newmediaexpress.com Available Packages
Install docker-compose
公式ドキュメント
https://docs.docker.com/engine/install/centos/https://docs.docker.com/compose/install/
1. download package
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2. 実行権限を付与
sudo chmod +x /usr/local/bin/docker-compose
3. バージョン確認
$ docker-compose --version docker-compose version 1.26.2, build eefe0d31
Kibana UI から Index へ document の投入
Indexの作成
Index 名: products
shardsの数: 2
replicasの数: 2
Document の投入
実行するクエリ
/index名/_doc に続いて Json 形式で指定する。
レスポンス
shards がトータルで3つ(メイン1つ、レプリカ2つ)出来ていることが分かる。
_id は自動的に生成される。
Document の投入(_id 指定)
実行するクエリ
REST API の慣習として、_id を指定する場合は、PUT にする必要がある。
レスポンス
_id が指定した番号になっていることが分かる。
Document の投入(存在しない index に対して)
存在しない index(ここでは books) に対して document を投入しようとした場合。
実行するクエリ
レスポンス
自動的に index が生成され、ドキュメントが投入される機能がデフォルトで有効になっている。
この時に作成される shards の数は設定ファイルで定義されているデフォルト数になる。