关于Docker和SQL

我稍微研究了一下Docker,简单地总结了一下。(仅供个人使用)

Docker 是什么?

Docker是一个采用容器虚拟化技术的开放平台,用于开发、部署和运行应用程序。通过使用基于操作系统的容器虚拟化技术,Docker将应用程序与开发和运行环境隔离开来,实现快速交付应用程序的能力。

    Dockerを使って作った仮想環境はコンテナと呼ばれる。
命令和操作

用于创建、启动和停止Docker容器的命令

    • docker create (新しいDockerコンテナを作成する)

 

    • docker start (作成済みのDockerコンテナの起動)

 

    • docker run (新しいDockerコンテナを作成&起動する)

 

    • docker stop (作成済みのDockerコンテナを停止する)

 

    docker rm (作成済みのDockerコンテナを削除する)

环境建设

    初めにディレクトリを作成することから始める。ファイルを作成後、(例)docker-compose.yml と作成。
version: '3.1'

services:

  db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    platform: linux/x86_64
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - mysql_data:/var/lib/mysql
    ports:
      - 3308:3306
volumes:
  mysql_data:
Image from Gyazo

如果能够熟练制作,将会附上鲸标志。

启动容器

在包含docker-compose.yml文件的目录中运行下面的命令,即可启动容器并创建。

$ docker compose up
Image from Gyazo

如果能够熟练制作,那就可以了。

当容器消失时,请使用以下命令。

$ docker compose down

连接到SQL

docker compose up -d && docker compose exec db bash

当敲击时,会显示#,请输入下面的命令。

mysql -u root -p 

然后,会要求输入密码。输入PW:example即可。

总的来说,感觉如下。

docker compose up -d && docker compose exec db bash
[+] Running 1/0
  Container sql_basic-db-1  Running                                                 0.0s 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

また、SQLから抜けるにはexitで抜ける。

    MySQL サーバーホスト上のデータベースを一覧表示するコマンド
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)

mysql> 

データベースの作成方法

创建一个名为sample_study的数据库,对于SQL的情况。

mysql> create database sample_study;
Query OK, 1 row affected (0.16 sec)

railsの場合

$ rails db:create

查看数据库后,发现包含 sample_study。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample_study       |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

データベースの選択方法

mysql> use runteq_study
Database changed

※Rails会自动选择使用哪个数据库。

创建表的方法

    テーブルの確認
mysql> show tables;
Empty set (0.02 sec)
    テーブルの設定
mysql> CREATE TABLE user(
    id INT(11) AUTO_INCREMENT NOT NULL, 
    first_name VARCHAR(30) NOT NULL,
    last_name VARCHAR(30) NOT NULL,
    PRIMARY KEY (id));

Query OK, 0 rows affected (0.02 sec)
    設定された後の表示
show tables;
+------------------------+
| Tables_in_sample_study |
+------------------------+
| user                   |
+------------------------+
1 row in set (0.00 sec)

在Rails中,可以按如下方式创建。

$ rails g model user first_name:string last_name:string
$ rails db:migrate

在桌子上添加记录的方法

mysql> insert into user (first_name, last_name) values ('dyson', 'saito');
Query OK, 1 row affected (0.00 sec)
    テーブルを確認
mysql> select * from user;
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | dyson      | saito     |
+----+------------+-----------+
1 rows in set (0.00 sec)

在Rails中添加表格

user = User.new(first_name: 'dyson', last_name: 'saito')
user.save

在哪里进行筛选

mysql> select * from board where user_id = 1;
+----+-------+------------------+---------+
| id | title | body             | user_id |
+----+-------+------------------+---------+
|  1 | hoge  | hogehogehogehoge |       1 |
|  2 | fuga  | fugafugafugafuga |       1 |
+----+-------+------------------+---------+
2 rows in set (0.03 sec)

在Rails框架下

Board.where(user_id: 1)

有关”order”的事项

ASC・・・昇順(小さいもの順)
DESC・・・降順(大きいもの順)

mysql> select * from board order by title desc;
+----+-------+------------------+---------+
| id | title | body             | user_id |
+----+-------+------------------+---------+
|  1 | hoge  | hogehogehogehoge |       1 |
|  2 | fuga  | fugafugafugafuga |       1 |
|  3 | bar   | barbarbarbar     |       2 |
+----+-------+------------------+---------+
3 rows in set (0.01 sec)

在Rails的情况下

Board.all.order(title: :asc)

有关更新方法的讨论

    変更前
mysql> select * from user;
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | dyson      | saito     |
|  2 | hisaju     | kikumoto  |
|  3 | takako     | omoshiro  |
+----+------------+-----------+
3 rows in set (0.00 sec)

更改

mysql> update user set first_name="daichi" where id = 1;
    変更後
mysql> select * from user;
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | daichi     | saito     |
|  2 | hisaju     | kikumoto  |
|  3 | takako     | omoshiro  |
+----+------------+-----------+
3 rows in set (0.00 sec)

→第一个ID已更改为”daichi”。

user.update(first_name: 'daichi') # userはidが1のユーザーという前提

关于删除(削除)。

    レコードを削除する。
mysql> delete from user where id = 3;

id为3的takako被删除。

    変更前
mysql> select * from user;
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | daichi     | saito     |
|  2 | hisaju     | kikumoto  |
|  3 | takako     | omoshiro  |
+----+------------+-----------+
3 rows in set (0.00 sec)
    変更後
mysql> select * from user;
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | daichi     | saito     |
|  2 | hisaju     | kikumoto  |
+----+------------+-----------+
2 rows in set (0.00 sec)
bannerAds