docker pull mysql
拉取成功可以验证一下
docker images
docker run --name ly-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
到这里我们查看容器运行状态:
$ sudo docker ps
可以看到容器的简写ID,容器的源镜像,创建时间,状态,端口映射信息,容器名字等。
使用navicat远程连接,这里碰到几个问题
首先这个IP肯定不是localhost,然后以为是mysql容器的IP
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID>
结果是:172.17.0.2 但是还是连接不上
docker-machine ip
192.168.99.100
这个可以连接
结论:
当使用windows和macOS时,不应该使用localhost而应该使用docker-machine ip
原因:由于myslq8不支持动态修改密码验证方式 解决方案:
docker exec -it ly-mysql bash
mysql -uroot -p
3.修改配置
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new password';
FLUSH PRIVILEGES;
$ sudo docker exec -it ly-mysql /usr/bin/bash
然后可以进入容器的命令行模式,接着修改 /etc/mysql/my.cnf 文件即可
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.