Docker 安装 mysql

概念解释

Docker镜像:可以理解成安装操作系统的镜像文件
Docker容器:可以理解为运行的操作系统。也有人比喻docker镜像为类,docker容器为对象

第零步,查看Docker MySQL文档

MySQL文档地址:
https://hub.docker.com/_/mysql/

第一步,拉取MySQL镜像

docker pull mysql

之后docker会自动拉取(下载)MySQL镜像。
拉取成功后我们查看一下:

docker images

第二步,创建并启动一个MySQL容器

输入以下命令:

docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql

–name:给新创建的容器命名,此处命名为mysqlserver
-e:配置信息,此处配置mysql的root用户的登陆密码
-p:端口映射,表示在这个容器中使用3306端口(第二个)映射到本机的端口号也为3306(第一个)
-d:成功启动容器后输出容器的完整ID
最后一个mysql指的是mysql镜像名字
到这里我们查看容器运行状态:

docker ps

上图可以看到容器的简写ID,容器的源镜像,创建时间,状态,端口映射信息,容器名字等。

第三步,测试连接MySQL

1、命令连接

docker exec -it  mysqlserver /bin/bash

docker exec :在运行的容器中执行命令
语法
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
OPTIONS说明:
-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端

2、使用navicat远程连接
我是在本机上使用navicat。另外,我本机上已经安装了mysql,所以要先把本机上安装的mysql关掉,否则会因为端口冲突而导致无法连接。
使用 net stop mysql 关闭本机mysql服务就行了。
然后遇到2059报错

原因是我的navicat不支持mysql新版本的加密规则,mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password, 解决问题方法有两种,一种是升级navicat驱动,一种是把mysql用户登录密码加密规则还原成mysql_native_password. 我用的第二种方式:
操作如下:

ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则 ,'password'改成你的密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户的密码 ,'password'是你的密码
FLUSH PRIVILEGES; #刷新权限


可以看到已经能够正常登陆了。

其他

1.可以启动多个MySQL服务,因为我们启动的是容器,容器可以有多个,只要容器名字映射段端口不一样就可以了,例如:

docker run --name mysqlserver2 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql

下图就是把mysqlserver2的3306绑定到了本机上的3307,使用navicat访问的时候只要填写成3307即可访问。

2.查看所有容器(启动状态或者关闭状态)

docker ps -a

3.启动和关闭容器
启动命令:

docker start mysqlserver   //通过指定容器名字sudo docker start 73f8811f669e  //通过指定容器ID

关闭命令:

docker stop mysqlserver   //通过指定容器名字
docker stop 73f8811f669e  //通过指定容器ID

3.修改MySQL配置文件有两种方法:
一是进入容器,修改容器里的MySQL的配置文件,然后重新启动容器,例如:

docker exec -it mysqlserver /usr/bin/bash

然后可以进入容器的命令行模式,接着修改 /etc/mysql/my.cnf 文件即可
二是挂载主机的mysql配置文件,官方文档如下:
The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

最后添加一点


如果远程连接有问题,可以参考下这个https://www.cnblogs.com/hanxue53/p/5850263.html

参考:https://www.cnblogs.com/pwc1996/p/5425234.htmlhttps://www.w3cschool.cn/docker/docker-install-mysql.html

原文地址:https://www.cnblogs.com/feipeng8848/p/10470655.html