linux 安装mysql 通docker
1、安装docker
- 使用国内的
daocloud
一键安装:
curl -sSL https://get.daocloud.io/docker | sh
-
使用阿里云的加速镜像
-
针对Docker客户端版本大于 1.10.0 的用户您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["你自己的加速地址"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
-
2、拉取mysql镜像
- 启动docker
systemctl start docker
- 拉取镜像
docker pull mysql:latest
3、启动mysql
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
4、本地连接是出错,解决问题
-
进入容器
docker exec -it [容器id] /bin/bash
-
登录mysql
mysql -u root -p[密码]
-
修改加密规则
-
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的mysql密码' PASSWORD EXPIRE NEVER; #修改加密规则 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的mysql密码'; #修改密码 FLUSH PRIVILEGES; #刷新数据
5、开机启动docker
systemctl enable docker
6、mysql自动运行
1、创建mysql镜像是启动
docker run -d --restart=always --name 设置容器名 使用的镜像
(上面命令 --name后面两个参数根据实际情况自行修改)
# Docker 容器的重启策略如下:
--restart具体参数值详细信息:
no // 默认策略,容器退出时不重启容器;
on-failure // 在容器非正常退出时(退出状态非0)才重新启动容器;
on-failure:3 // 在容器非正常退出时重启容器,最多重启3次;
always // 无论退出状态是如何,都重启容器;
unless-stopped // 在容器退出时总是重启容器,但是不考虑在 Docker 守护进程启动时就已经停止了的容器。
2、修改已有的容器,使用update
docker update --restart=always 容器ID(或者容器名)
参考文章:
https://blog.csdn.net/chj_1224365967/article/details/109029856
https://www.runoob.com/docker/centos-docker-install.html
https://www.runoob.com/docker/docker-container-usage.html
https://www.runoob.com/docker/docker-install-mysql.html