docker入门请点链接
https://blog.csdn.net/xiaozecheng/article/details/106145593
1、首先检查是否安装docker
查看docker版本:docker -v
[root@luoxiaoshuai ~]# docker -v
Docker version 20.10.2, build 2291f61
查看mysql可用版本:docker search mysql
[root@luoxiaoshuai ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10411 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3864 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 762 [OK]
percona Percona Server is a fork of the MySQL relati… 524 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 47 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 37
prom/mysqld-exporter 37 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
circleci/mysql MySQL is a widely used, open-source relation… 20
arey/mysql-client Run a MySQL client from a docker container 17 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 17
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 10 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [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]
2、拉取 MySQL 镜像
下载mysql最新版本:docker pull mysql:latest
[root@luoxiaoshuai ~]# docker pull mysql:latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
3、查看本地镜像
使用以下命令来查看是否已安装了 mysql:
查看mysql镜像:docker images
[root@luoxiaoshuai ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c8562eaf9d81 4 days ago 546MB
4、运行容器
启动容器:docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
[root@luoxiaoshuai ~]# docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
6f9af33e5c9b0aa7658abb5c63702479d3b36243d43d1f18e4c46b07cc13d1ce
参数说明:
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
5、安装成功
查看当前运行的容器:docker ps
[root@luoxiaoshuai ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c8562eaf9d81 4 days ago 546MB
这个时候我们用SQLlog去连接会发现乱码
乱码请执行以下命令:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U28DTe0q-1611370272700)(C:\Users\Le’novo\AppData\Roaming\Typora\typora-user-images\image-20210123102117218.png)]
本机可以通过 root 和密码 123456 访问 MySQL 服务
进入容器
docker exec -it mysql bash
登录mysql
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
添加远程登录用户
CREATE USER 'luoxiaoshuai'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'luoxiaoshuai'@'%'
[root@luoxiaoshuai ~]# docker exec -it mysql-test bash
root@6f9af33e5c9b:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE USER 'luoxiaoshuai'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'luoxiaoshuai'@'%'
->
6、再次利用SQLlog进入连
连接成功!
注意这个时候我们的用户名不在是root了
而是luoxiaoshuai
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AxcFJmbM-1611370272709)(C:\Users\Le’novo\AppData\Roaming\Typora\typora-user-images\image-20210123101353324.png)]