Linux系统Docker中安装MySQL并用Navicat连接

我的上一篇文章里有Linux系统中安装docker的步骤,在这里就不赘述了,直接在docker中安装MySQL并访问。

一、查看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 将镜像从仓库中拉取下来

PS:tag是可选的,tag表示标签,多为软件的版本,默认是latest
[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

三、查看docker中的所有镜像

[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

四、启动容器

1、设置容器开机启动
[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

2、启动容器
docker start first-mysql
3、停止容器
docker stop test-redis
4、端口映射

Docker容器中运行的软件所使用的端口,需要映射到当前主机的端口上才能访问。Docker的端口映射通过一个-p参数来实现。例如,我们将Redis容器的6379端口映射到本机的6378端口:

docker run -d -p 6378:6379 --name port-redis redis
5、删除容器
docker rm container-id
6、查看容器日志
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

六、进入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 ~]# 

九、Navicat连接Linux系统docker中的MySQL

PS:我用的是阿里云服务器,有防火墙设置,需要在后台开通端口号

Linux系统Docker中安装MySQL并用Navicat连接_第1张图片
使用Navicat连接成功!

你可能感兴趣的:(Linux)