docker mysql

◾️ how to send env param to container

$ docker images
REPOSITORY        TAG       IMAGE ID       CREATED      SIZE
nginx             latest    35c43ace9216   2 days ago   133MB
jenkins/jenkins   latest    10e33bea4cd2   4 days ago   573MB
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$ docker run -d --name nx -e env_name=test123 --rm nginx
c61cc553a5d0f4b394afde4a80cac794c69dfaee1e36f61b9e00febaa3c1bb1b
$ docker exec -it nx bash
root@c61cc553a5d0:/# printenv
HOSTNAME=c61cc553a5d0
PWD=/
PKG_RELEASE=1~buster
HOME=/root
NJS_VERSION=0.5.1
TERM=xterm
SHLVL=1
env_name=test123
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.19.7
_=/usr/bin/printenv

◾️ how to use mysql image (public site)

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d --rm mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
45b42c59be33: Already exists
b4f790bd91da: Pull complete
325ae51788e9: Pull complete
adcb9439d751: Pull complete
174c7fe16c78: Pull complete
698058ef136c: Pull complete
4690143a669e: Pull complete
f7599a246fd6: Pull complete
35a55bf0c196: Pull complete
790ac54f4c47: Pull complete
18602acc97e1: Pull complete
365caa3500d0: Pull complete
Digest: sha256:b1cc887ed32cc6c2f217b12703bd05f503f2037892c8bb226047fe5dff85a109
Status: Downloaded newer image for mysql:latest
6122e8b87d81501e7722c4c1ce14e080708c0376a7609de96cf6e3a8e0adf70c
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                 NAMES
6122e8b87d81   mysql     "docker-entrypoint.s…"   7 seconds ago   Up 6 seconds   3306/tcp, 33060/tcp   some-mysql

$ mysql -u root -h localhost:3306 -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'localhost:3306' (0)
$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                 NAMES
6122e8b87d81   mysql     "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   3306/tcp, 33060/tcp   some-mysql
$ mysql -u root -h localhost:3306 -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'localhost:3306' (0)
$ mysql -u root -h localhost:33060 -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'localhost:33060' (0)
$ mysql -u root -h localhost -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
$
$
$
$
$ docker exec -it some-mysql mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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>

◾️ connect using host ip

$ docker run --name ms -e MYSQL_ROOT_PASSWORD=ms -d -p 3306:3306 --rm mysql
f6364cff8948b5d66c488af3fc9b8810defcd5a422a2fa07a56018c4ba4648e2
$ mysql -u root -h 0.0.0.0  -P 3306 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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>

◾️ mysql command usage (public site)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> CREATE DATABASE pets;
Query OK, 1 row affected (0.02 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| pets               |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> USE pets;
Database changed
mysql> CREATE TABLE cats
    -> (
    ->   id              INT unsigned NOT NULL AUTO_INCREMENT, # Unique ID for the record
    ->   name            VARCHAR(150) NOT NULL,                # Name of the cat
    ->   owner           VARCHAR(150) NOT NULL,                # Owner of the cat
    ->   birth           DATE NOT NULL,                        # Birthday of the cat
    ->   PRIMARY KEY     (id)                                  # Make the id the primary key
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> SHOW TABLES;
+----------------+
| Tables_in_pets |
+----------------+
| cats           |
+----------------+
1 row in set (0.00 sec)

mysql> DESCRIBE cats;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(150) | NO   |     | NULL    |                |
| owner | varchar(150) | NO   |     | NULL    |                |
| birth | date         | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> INSERT INTO cats ( name, owner, birth) VALUES
    ->   ( 'Sandy', 'Lennon', '2015-01-03' ),
    ->   ( 'Cookie', 'Casey', '2013-11-13' ),
    ->   ( 'Charlie', 'River', '2016-05-21' );
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM cats;
+----+---------+--------+------------+
| id | name    | owner  | birth      |
+----+---------+--------+------------+
|  1 | Sandy   | Lennon | 2015-01-03 |
|  2 | Cookie  | Casey  | 2013-11-13 |
|  3 | Charlie | River  | 2016-05-21 |
+----+---------+--------+------------+
3 rows in set (0.01 sec)

mysql>

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です