我的上一篇文章里有Linux系统中安装docker的步骤,在这里就不赘述了,直接在docker中安装MySQL并访问。
[root@iZudpsyuwyc6wiZ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8590 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2977 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 631 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 62
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 51
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 34
bitnami/mysql Bitnami MySQL Docker Image 33 [OK]
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 28 [OK]
prom/mysqld-exporter 22 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 21
centos/mysql-56-centos7 MySQL 5.6 SQL database server 16
circleci/mysql MySQL is a widely used, open-source relation… 14
mysql/mysql-router MySQL Router provides transparent routing be… 12
arey/mysql-client Run a MySQL client from a docker container 11 [OK]
imega/mysql-client Size: 36 MB, alpine:3.5, Mysql client: 10.1.… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 6 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 4 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 1 [OK]
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
monasca/mysql-init A minimal decoupled init container for mysql 0
使用命令 docker pull mysql:tag 将镜像从仓库中拉取下来
[root@iZudpsyuwyc6wiZ ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
8f91359f1fff: Pull complete
6bbb1c853362: Pull complete
e6e554c0af6f: Pull complete
f391c1a77330: Pull complete
414a8a88eabc: Pull complete
fee78658f4dd: Pull complete
9568f6bff01b: Pull complete
5a026d8bbe50: Pull complete
07f193b54ae1: Pull complete
1e404375a275: Pull complete
b81b2ef0e430: Pull complete
2f499f36bd40: Pull complete
Digest: sha256:6d95fa56e008425121e24d2c01b76ebbf51ca1df0bafb1edbe1a46937f4a149d
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@iZudpsyuwyc6wiZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest b8fd9553f1f0 4 days ago 445MB
uifd/ui-for-docker latest 965940f98fa5 3 years ago 8.1MB
[root@iZudpsyuwyc6wiZ ~]# docker run --restart=always --name first-mysql -p 3306:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql:latest
953d1080a6bbc15d393088d4c4cc1635f46dc8ef0d3b3cb024f010b16ef9532b
–name first-mysql :容器名字是first-mysql
-p 3306:3306 :Linux上端口号:docker端口号
-e MYSQL_ROOT_PASSWORD=123456 :密码设置为123456
docker start first-mysql
docker stop test-redis
Docker容器中运行的软件所使用的端口,需要映射到当前主机的端口上才能访问。Docker的端口映射通过一个-p参数来实现。例如,我们将Redis容器的6379端口映射到本机的6378端口:
docker run -d -p 6378:6379 --name port-redis redis
docker rm container-id
docker logs container-name/container-id
[root@iZudpsyuwyc6wiZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ae92ece0c4f uifd/ui-for-docker "/ui-for-docker" 2 hours ago Up 2 hours 0.0.0.0:9000->9000/tcp docker-ui
953d1080a6bb mysql:latest "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:3306->3306/tcp, 33060/tcp first-mysql
[root@iZudpsyuwyc6wiZ ~]# docker exec -it first-mysql bash
root@953d1080a6bb:/# 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.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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>
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql>
mysql> exit
Bye
root@953d1080a6bb:/#
root@953d1080a6bb:/#
root@953d1080a6bb:/# exit
exit
[root@iZudpsyuwyc6wiZ ~]#