Docker创建MySQL镜像并成功进行远程连接
1.安装
1.1 拉取镜像
[code]docker pull mysql
拉取成功可以验证一下
[code]docker images
1.2 创建并启动一个mysql容器
[code]docker run --name ly-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
- –name:给新创建的容器命名,此处命名为ly-mysql
- -e:配置信息,此处配置mysql的root用户的登陆密码
- -p:端口映射,此处映射主机3306端口到容器pwc-mysql的3306端口
- -d:成功启动容器后输出容器的完整ID.
- 最后一个mysql指的是mysql镜像名字
到这里我们查看容器运行状态:
$ sudo docker ps
可以看到容器的简写ID,容器的源镜像,创建时间,状态,端口映射信息,容器名字等。
1.3 连接测试
使用navicat远程连接,这里碰到几个问题
1.3.1 mysql连接IP问题
首先这个IP肯定不是localhost,然后以为是mysql容器的IP
1.3.1.1 查看mysql容器的ip
[code]docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID>
结果是:172.17.0.2
但是还是连接不上
1.3.1.2 获取docker主机 IP
[code]docker-machine ip
192.168.99.100
这个可以连接
结论:
当使用windows和macOS时,不应该使用localhost而应该使用docker-machine ip
1.3.2 连接mysql 8提示2059 - authentication plugin 'caching_sha2_password...
原因:由于myslq8不支持动态修改密码验证方式
解决方案:
- 进入mysql容器
[code]docker exec -it ly-mysql bash
- 连接mysql
[code]mysql -uroot -p
3.修改配置
[code]use mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new password'; FLUSH PRIVILEGES;
1.4 其他
1.4.1 记录几个命令
1.4.1.1 退出容器
- 如果要正常退出不关闭容器,请按Ctrl+P+Q进行退出容器
- 如果使用exit退出,那么在退出之后会关闭容器,可以使用下面的流程进行恢复
-
[code]使用docker restart命令重启容器
-
[code]使用docker attach命令进入容器
1.4.1.2 修改MySQL配置文件有两种方法:
- 一是进入容器,修改容器里的MySQL的配置文件,然后重新启动容器,例如:
[code]$ sudo docker exec -it ly-mysql /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):
[code]$ 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.
- 点赞
- 收藏
- 分享
- 文章举报
- Docker创建MySQL镜像并成功进行远程连接
- docker 创建mysql镜像,并成功进行远程连接
- Docker-创建mysql容器进行持久化&设置远程连接&导入sql文件(centos7)
- docker mycat创建 镜像,连接mysql
- 在sql server里连接远程服务器,并进行创建和删除新数据库
- Mysql 授权用户进行远程连接
- 给mysql用户进行远程连接权限
- Docker--创建ssh远程镜像
- CentOS 7下yum重装 MySQL 5.7 (远程连接成功)
- Docker创建下载MySQL镜像运行实例
- 在sql server里连接远程服务器,并进行创建和删除新数据库
- 阿里云服务器配置mysql并进行远程连接(Ubuntu)
- mysql 创建远程连接用户
- 在centos 上开启MySQL的远程连接成功后的记录
- 创建含Javaweb环境和MySQL的docker镜像
- Git 使用教程 之 Mac Git 本地创建ssh key并且测试是否远程连接GitHub成功
- 关于“使用VS 2005和SQL Server 2005创建连接时不允许进行远程连接”的问题的解决
- Docker创建Mysql容器并通过命令行连接到容器
- Docker-创建一个mysql容器,并保存为本地镜像
- docker学习笔记3—使用Dockerfile与docker build命令创建一个nginx服务器镜像,并使用浏览器进行访问