天天看點

Linux系統Docker中安裝MySQL并用Navicat連接配接

我的上一篇文章裡有Linux系統中安裝docker的步驟,在這裡就不贅述了,直接在docker中安裝MySQL并通路。

一、檢視MySQL的鏡像

[[email protected] ~]# 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

[[email protected] ~]# 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中的所有鏡像

[[email protected] ~]# 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、設定容器開機啟動
[[email protected] ~]# 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
           

五、檢視已經運作的鏡像

[[email protected] ~]# 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

[[email protected] ~]# docker exec -it first-mysql bash
[email protected]:/# 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
[email protected]:/# 
[email protected]:/# 
[email protected]:/# exit
exit
[[email protected] ~]# 
           

九、Navicat連接配接Linux系統docker中的MySQL

PS:我用的是阿裡雲伺服器,有防火牆設定,需要在背景開通端口号

Linux系統Docker中安裝MySQL并用Navicat連接配接

使用Navicat連接配接成功!