前言
和朋友一起搞一个项目,分了一下工作,但是mysql迟迟安装不上,程序都在一个环境里确实容易出现很多问题,浪费时间和经历在这些配置上,好在有docker了,就在docker里搭建一个Mysql数据库使用吧,顺便出一篇教程记录和分享。
Linux中使用Docker部署Mysql数据库
- 前言
- 1 检查docker是否运行
- 2 检索mysql镜像
- 3 下载mysql镜像
- 下载好后,可以检查是否已经下载成功:
- 4 使用docker启动mysql数据库容器
- 5 查看是否启动mysql数据库成功
- 6 设置mysql数据库的远程链接
- 进入mysql容器:
- 登陆mysql数据库:
- 授权:
- 重新授权表:
- 退出mysql:
- 退出容器
- 7 Navicat远程链接
1 检查docker是否运行
systemctl status docker
k17gc10Z ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-02-23 10:48:05 CST; 23h ago
Docs: https://docs.docker.com
Main PID: 735685 (dockerd)
Tasks: 19
Memory: 119.3M
CGroup: /system.slice/docker.service
├─735685 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
├─752356 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8081 -container-ip 172.17.0.2 -container-port 8080
└─752360 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8081 -container-ip 172.17.0.2 -container-port 8080
.......................
若没有运行则启动docker:
systemctl start docker
2 检索mysql镜像
docker search mysql
k17gc10Z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13847 [OK]
mariadb MariaDB Server is a high performing open sou… 5281 [OK]
percona Percona Server is a fork of the MySQL relati… 599 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 744 [OK]
circleci/mysql MySQL is a widely used, open-source relation… 28
bitnami/mysql Bitnami MySQL Docker Image 80 [OK]
bitnami/mysqld-exporter 4
ubuntu/mysql MySQL open source fast, stable, multi-thread… 41
cimg/mysql 0
rapidfort/mysql RapidFort optimized, hardened image for MySQL 14
google/mysql MySQL server for Google Compute Engine 23 [OK]
ibmcom/mysql-s390x Docker image for mysql-s390x 2
rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 0
hashicorp/mysql-portworx-demo 0
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
rapidfort/mysql-official RapidFort optimized, hardened image for MySQ… 0
databack/mysql-backup Back up mysql databases to... anywhere! 81
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 38
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d… 0
vitess/mysqlctld vitess/mysqlctld 1 [OK]
linuxserver/mysql-workbench 48
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]
NAME:镜像名
DESCRIPTION:说明
STARS:热度
OFFICIAL:是否官方
AUTOMATED:是否自动
3 下载mysql镜像
docker pull mysql:5.7
1k17gc10Z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
e048d0a38742: Pull complete
c7847c8a41cb: Pull complete
351a550f260d: Pull complete
8ce196d9d34f: Pull complete
17febb6f2030: Pull complete
d4e426841fb4: Pull complete
fda41038b9f8: Pull complete
f47aac56b41b: Pull complete
a4a90c369737: Pull complete
97091252395b: Pull complete
84fac29d61e9: Pull complete
Digest: sha256:8cf035b14977b26f4a47d98e85949a7dd35e641f88fc24aa4b466b36beecf9d6
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
下载好后,可以检查是否已经下载成功:
docker images
17gc10Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jenkins/jenkins lts f16216f97fcb 2 weeks ago 467MB
mysql 5.7 be16cf2d832a 3 weeks ago 455MB
hello-world latest feb5d9fea6a5 17 months ago 13.3kB
REPOSITORY:镜像名
TAG:标签,latest表示为最新的
IMAGE ID:镜像id
CREATED:创建时间
SIZE:大小
上图中表示mysql镜像文件已经下载成功
4 使用docker启动mysql数据库容器
docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:5.7
k17gc10Z ~]# docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:5.7
68b7dc6f81b01f3c209a66b519633d1a6e8849bb99036fff951dddde83bd3472
-v :指明挂在卷
–restart:自动重启
–name:给容器命名
-e:设置账号密码
-p: 指明宿主机端口:docker内部端口
-d:指明用什么镜像的id启动
c5244d16078bb224ce77c3e6446ab6466a95d5fe801b2269d4c00b0eb0d65319:启动的mysql容器的id
5 查看是否启动mysql数据库成功
docker ps -l
7gc10Z ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68b7dc6f81b0 mysql:5.7 "docker-entrypoint.s…" 39 seconds ago Up 38 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
CONTAINER ID:容器id
IMAGES:镜像id
CREATED:创建时间
STATUS:状态
NAME:容器名
到此可知,mysql容器创建成功
6 设置mysql数据库的远程链接
进入mysql容器:
docker exec -it mysql bash
7gc10Z ~]# docker exec -it mysql bash
登陆mysql数据库:
mysql -u root -p"root"
bash-4.2# mysql -u root -p"root"
mysql: [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.7.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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.
授权:
GRANT ALL PRIVILEGES ON . TO ‘root’@‘%’ IDENTIFIED BY ‘root’ WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
重新授权表:
FLUSH PRIVILEGES;
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
退出mysql:
exit
mysql> exit
Bye
退出容器
exit
bash-4.2# exit
exit