0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ubuntu 20.10 から Docker 内の MySQL 5.7 に接続

Last updated at Posted at 2021-02-24

Ubuntu 20.10 に MySQL 5.7 をインストールしようとすると、次のようなメッセージが出ます。

sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb

mysql_aa.png

そこで、Docker を使います。

Docker のインストール

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

動作の確認

sudo docker run hello-world
$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

MySQL5.7 のイメージのダウンロード

sudo docker pull mysql/mysql-server:5.7

イメージの確認

sudo docker images

イメージの起動
root のパスワードを mysql とします。

sudo docker run --name my2021 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mysql -d mysql/mysql-server:5.7

MySQL5.7 に接続

sudo docker exec -it my2021 mysql -uroot -pmysql

接続したら、root のパスワードを変更

ALTER USER 'root'@'localhost' IDENTIFIED BY 'tiger123';

mysql で接続しようとして、エラーを出します。

$ mysql -uscott -psecret -h 127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'scott'@'172.17.0.1' (using password: YES)

172.17.0.1 から接続しようとしたことが分かります。

Docker で、MySQL に接続して ユーザーを作成します。
テスト用のデータベースも作成します。

create user scott@172.17.0.1 identified by 'secret';
create database city;
grant all on city.* to scott@172.17.0.1;

mysql コマンドで接続します。

mysql -uscott -psecret -h 127.0.0.1

結果

$ mysql -uscott -psecret -h 127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?