1、查找镜像

[root@k8s ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10084               [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3696                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   737                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   511                 [OK]                
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   83                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   77                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   60                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      45                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                                      
prom/mysqld-exporter                                                              31                                      [OK]
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   30                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         30                                      
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   26                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   19                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   17                                      
arey/mysql-client                 Run a MySQL client from a docker container      15                                      [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   10                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
[root@k8s ~]# 

2、下载镜像

[root@k8s docker.service.d]# docker pull mysql (默认下载最新的版本,docker pull mysql:5.7 下载指定的版本)
Using default tag: latest
latest: Pulling from library/mysql
bb79b6b2107f: Pull complete 
49e22f6fb9f7: Pull complete 
842b1255668c: Pull complete 
9f48d1f43000: Pull complete 
c693f0615bce: Pull complete 
8a621b9dbed2: Pull complete 
0807d32aef13: Pull complete 
a56aca0feb17: Pull complete 
de9d45fd0f07: Pull complete 
1d68a49161cc: Pull complete 
47834b5a7c81: Pull complete 
7b0764b0009c: Pull complete 
Digest: sha256:b30e3c13ab71f51c7951120826671d56586afb8d9e1988c480b8673c8570eb74
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@k8s docker.service.d]# 

3、查看镜像

[root@k8s ~]# docker images mysql
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              b5c10a3624ce        13 hours ago        545MB
[root@k8s ~]# 

4、通过镜像创建容器并运行

[root@k8s data]# docker run --name mysqlserver -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 3306:3306 mysql
6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee
[root@k8s data]# cd /var/lib/docker/containers/
[root@k8s containers]# ls -ltr
total 0
drwx------ 4 root root 237 Oct 21 14:21 3744fe5331d5c898ffe2732e0e5be1d2a615aa8166aaca689631b650b28ea164
drwx------ 4 root root 237 Oct 21 15:07 6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee
[root@k8s containers]# 

-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
-v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 $PWD/conf/ 挂载到容器的 /etc/mysql/conf.d
-v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs。
-v $PWD/data:/var/lib/mysql:将主机当前目录下的data目录挂载到容器的 /var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码

创建容器时也可以不创建映射关系,但是当容器出现问题无法启动时,里面的数据可能无法访问。

映射理解

[root@k8s mysql]# docker exec -it mysqlserver /bin/bash
root@f56ed36626a3:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  logs  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f56ed36626a3:/# cd /logs
root@f56ed36626a3:/logs# ls
root@f56ed36626a3:/logs# ls -ltr
total 0
root@f56ed36626a3:/logs# touch 1.txt
root@f56ed36626a3:/logs# 
root@f56ed36626a3:/logs# 
root@f56ed36626a3:/logs# ls
1.txt
root@f56ed36626a3:/logs# exit
exit
[root@k8s mysql]# 
[root@k8s mysql]# cd
[root@k8s ~]# 
[root@k8s ~]# cd data
[root@k8s data]# ls
conf  data  logs
[root@k8s data]# cd logs
[root@k8s logs]# ls   <======在容器创建的文件,因为创建了映射关系,在容器外也可以看到
1.txt

5、查看容器状态

[root@k8s containers]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
760a57ae8902        mysql:latest        "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes                0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlserver

6、进入mysql容器,并登陆mysql修改连接

[root@k8s containers]# docker exec -it mysqlserver bash
root@6803708096e4:/# 
root@6803708096e4:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  logs  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@6803708096e4:/# 

root@6803708096e4:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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 '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> 
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> 
mysql> exit
Bye
root@f56ed36626a3:/# 
root@f56ed36626a3:/# 
root@f56ed36626a3:/# exit
exit

容器外连接测试

[root@k8s mysql]# mysql -uroot -h 192.168.40.105 -p -P3306
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.22 MySQL Community Server - GPL

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

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

MySQL [(none)]> select host,user from user;
ERROR 1046 (3D000): No database selected
MySQL [(none)]> 
MySQL [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [mysql]> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

7、查看docker日志

[root@k8s containers]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
6803708096e4        mysql               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes                0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlserver
3744fe5331d5        hello-world         "/hello"                 4 hours ago         Exited (0) 48 minutes ago                                       agitated_meninsky
[root@k8s containers]# docker logs -f --tail 10 6803708096e4
2020-10-21 07:07:19+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2020-10-21T07:07:20.229230Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.22) starting as process 1
2020-10-21T07:07:20.237638Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-10-21T07:07:20.373915Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-10-21T07:07:20.458474Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2020-10-21T07:07:20.558123Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-10-21T07:07:20.558304Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-10-21T07:07:20.560401Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2020-10-21T07:07:20.577767Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

或者进入到容器日志查看

[root@k8s containerd]# cd /var/lib/docker/containers/
[root@k8s containers]# ls -ltr
3744fe5331d5c898ffe2732e0e5be1d2a615aa8166aaca689631b650b28ea164  
6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee
[root@k8s containers]# cd 6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee
[root@k8s 6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee]# ls
6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee-json.log 
config.v2.json   
hostname  
mounts       
resolv.conf.hash
checkpoints                                                                
hostconfig.json  
hosts     
resolv.conf
[root@k8s 6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee]# tail -10f 6803708096e4fb44af8b277202d0081b3ab71b49e39ad20b5f4aa2021f013eee-json.log 
{"log":"2020-10-21 07:07:19+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.\n","stream":"stdout","time":"2020-10-21T07:07:19.959786689Z"}
{"log":"\n","stream":"stdout","time":"2020-10-21T07:07:19.959807818Z"}
{"log":"2020-10-21T07:07:20.229230Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.22) starting as process 1\n","stream":"stderr","time":"2020-10-21T07:07:20.236118617Z"}
{"log":"2020-10-21T07:07:20.237638Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.\n","stream":"stderr","time":"2020-10-21T07:07:20.237753469Z"}
{"log":"2020-10-21T07:07:20.373915Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.\n","stream":"stderr","time":"2020-10-21T07:07:20.374122984Z"}
{"log":"2020-10-21T07:07:20.458474Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock\n","stream":"stderr","time":"2020-10-21T07:07:20.458846762Z"}
{"log":"2020-10-21T07:07:20.558123Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.\n","stream":"stderr","time":"2020-10-21T07:07:20.558383207Z"}
{"log":"2020-10-21T07:07:20.558304Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.\n","stream":"stderr","time":"2020-10-21T07:07:20.558470163Z"}
{"log":"2020-10-21T07:07:20.560401Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.\n","stream":"stderr","time":"2020-10-21T07:07:20.560633851Z"}
{"log":"2020-10-21T07:07:20.577767Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.\n","stream":"stderr","time":"2020-10-21T07:07:20.577894931Z"}

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐