Docker 安裝 Mysql 5.7

Xander發表於2023-01-26

查詢映象

查詢映象的方式如下:

[zxd@localhost seata]$ docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13614     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5197      [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   713       [OK]       
percona                         Percona Server is a fork of the MySQL relati…   597       [OK]       
bitnami/mysql                   Bitnami MySQL Docker Image                      80                   [OK]
databack/mysql-backup           Back up mysql databases to... anywhere!         77                   
linuxserver/mysql-workbench                                                     45                   
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   40                   
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   38                   
circleci/mysql                  MySQL is a widely used, open-source relation…   28                   
google/mysql                    MySQL server for Google Compute Engine          22                   [OK]
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   13                   
bitnami/mysqld-exporter                                                         4                    
ibmcom/mysql-s390x              Docker image for mysql-s390x                    2                    
vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
hashicorp/mysql-portworx-demo                                                   0                    
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   0                    
docksal/mysql                   MySQL service images for Docksal - https://d…   0                    
mirantis/mysql                                                                  0                    
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   0                    
cimg/mysql                                                                      0                    
eclipse/mysql                   Mysql 5.7, curl, rsync                          0                    [OK]
drud/mysql                                                                      0                    
silintl/mysql-backup-restore    Simple docker image to perform mysql backups…   0                    [OK]

獲取映象

docker可以使用下面的命令獲取映象:

docker pull mysql:5.7

查詢映象

可以使用下面的命令獲取當前下載的所有映象:

docker images

個人獲取到的映象內容如下:

[zxd@localhost seata]$ docker images
REPOSITORY                          TAG       IMAGE ID       CREATED         SIZE
redis                               latest    0256c63af7db   5 days ago      117MB
mysql                               5.7       d410f4167eea   2 weeks ago     495MB
redis                               <none>    3e12e2ceb68f   2 weeks ago     117MB
apache/rocketmq                     4.9.4     a2a50ca263c3   5 months ago    548MB
apacherocketmq/rocketmq-dashboard   latest    eae6c5db5d11   14 months ago   738MB
hello-world                         latest    feb5d9fea6a5   15 months ago   13.3kB

本地建立mysql的對映目錄

在正式的啟動Mysql映象之前進行對映目錄的配置。

mkdir -p /opt/mysql/data /opt/mysql/logs /opt/mysql/conf

/root/mysql/conf中建立 *.cnf 檔案(叫什麼都行)。這裡以個人建立的mysql.conf為例:

touch mysql.conf

建立容器,將資料,日誌,配置檔案對映到本機,注意這裡的root密碼為root:

docker run -p 13306:3306 --name mysql -v /opt/mysql/conf:/etc/mysql/conf.d -v /opt/mysql/logs:/logs -v /opt/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

執行之後,我們執行docker ps檢視當前執行的mysql映象:

[zxd@localhost conf]$ docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED              STATUS              PORTS                                                                                                               NAMES
e95a0bc7b4a4   mysql:5.7               "docker-entrypoint.s…"   About a minute ago   Up About a minute   33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp                                                              mysql

相關執行引數如下:

-d: 後臺執行容器
-p 將容器的埠對映到本機的埠
-v 將主機目錄掛載到容器的目錄
-e 設定引數

執行之後我們執行下面的命令檢視:

[zxd@localhost conf]$ mysql -uroot -proot -P 13306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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> 

最後檢視/opt/mysql/data目錄是否有資料檔案:

[zxd@localhost conf]$ sudo ls /opt/mysql/data
auto.cnf    client-cert.pem  ibdata1      ibtmp1      performance_schema  server-cert.pem
ca-key.pem  client-key.pem   ib_logfile0  mysql       private_key.pem      server-key.pem
ca.pem        ib_buffer_pool   ib_logfile1  mysql.sock  public_key.pem      sys

執行容器

docker run -p 13306:3306 --name mysql57 --restart=always -v /opt/mysql/conf:/etc/mysql/conf.d -v /opt/mysql/logs:/logs -v /opt/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

相關文章