Docker 安装Mysql
1:查询要安装的mysql版本
docker search mysql
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10534 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3934 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 773 [OK]
percona Percona Server is a fork of the MySQL relati… 527 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
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 48 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 38
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
mysql/mysql-router MySQL Router provides transparent routing be… 18
arey/mysql-client Run a MySQL client from a docker container 17 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 11 [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
2:向镜像仓库 拉取mysql tag版本为:8.0.23 的镜像 这里可以搜索想要的镜像版本 :https://hub.docker.com/_/mysql
docker pull mysql:8.0.23
不指定版本 按照最新版本下载
# 拉取 tag版本为:8.0.23 的mysql 镜像,如果不指定版本,默认拉最新的
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker pull mysql:8.0.23
8.0.23: 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:8.0.23
docker.io/library/mysql:8.0.23
3:查看本地的 docker 镜像
docke images
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.23 c8562eaf9d81 5 weeks ago 546MB
mysql latest c8562eaf9d81 5 weeks ago 546MB
nginx latest f6d0b4767a6c 5 weeks ago 133MB
4:启动mysql容器
docker run -itd --name mysql-996 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
–volume , -v: 数据绑定,这里没有配置, 因为自己搞这玩,然后数据挂载没有配置
-d :后台运行
-it:使用交互方式运行,进入容器查看区分
–name : 类似别名
-p :指定交互端口
-e :设置环境配置
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker run -itd --name mysql-996 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
14fdc50bf992f9e5bae69deb21680f29b1a7804e893481da86c211d2e5ee0115
5:查询运行中的容器
docker ps -a
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14fdc50bf992 mysql "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-996
290c141bc1d4 nginx "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:80->80/tcp nginx-997
6:设置远程连接用户
#进入mysql 容器
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker exec -it mysql-996 bash
#进入mysql
root@14fdc50bf992:/# mysql -u root -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
#切换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.01 sec)
#添加root 用户可以远程连接
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
#刷新mysql 的系统权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
7:测试链接mysql
8:关闭mysql
docker stop [OPTIONS] CONTAINER [CONTAINER…]
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker stop mysql-996
mysql-996
9: 重新启动mysql
docker start mysql-996
[root@iZ2zeixqaqrvghrgvk058mZ ~]# docker start mysql-996
mysql-996