这个操作在大约2年前做商务智能的时候成功用过,但最近想要一个自由的Mysql用,好几次在parallels desktop上的Ubuntu装都失败了,这次用别的虚拟机试了一下就成了,感觉很可能是虚拟机镜像的问题。

  1. 一个新的Ubuntu虚拟机,先安装docker,参考https://developer.aliyun.com/article/1323800
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# apt预备
sudo apt update
sudo apt upgrade
# 卸载旧的
sudo apt-get remove docker docker-engine docker.io containerd runc
# 证书
sudo apt-get install ca-certificates curl gnupg lsb-release
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 换源
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 装mysql
sudo apt-get install docker-ce docker-ce-cli containerd.io
# 启动
systemctl start docker
# 验证安装
sudo docker
sudo docker ps
sudo docker run hello-world
  1. 拉取mysql镜像,直接搞latest(2024-09-23)
1
2
3
4
5
6
7
8
# 直接拉取
sudo docker pull mysql
sudo docker images
# 拉取不下来?换源!
cd /etc/docker/
ls # 发现是空的
sudo vi daemon.json # 添加json内容
sudo systemctl restart docker # 重启后再pull
  1. 打开防火墙3306,参考https://blog.csdn.net/weixin_38611617/article/details/115936149
1
2
3
4
sudo apt install firewalld
sudo firewall-cmd --query-port=3306/tcp # 看3306是否能通过防火墙
sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent # 开放3306端口
sudo firewall-cmd --reload
  1. 跑一下容器试试,按理说此时应该就能连上了,用户名root密码123456,参考https://blog.csdn.net/weixin_47937828/article/details/141112933
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 试运行
sudo docker run --name sql_docker -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
# 真运行
docker run \
--name sql_docker \
-d \
-p 3306:3306 \
--restart unless-stopped \
-v ~/Documents/Docker/Mysql/log:/var/log/mysql \
-v ~/Documents/Docker/Mysql/data:/var/lib/mysql \
-v ~/Documents/Docker/Mysql/conf:/etc/mysql/conf.d/ \
-v ~/Documents/Docker/Mysql/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=YOUR_PASSWORD \
mysql
# 进容器看看
sudo docker exec -it sql_docker /bin/bash
bash-5.1$ mysql -uroot -p
# 创建用户
create user 'yourusername'@'%' identified by 'YOURPASSWORD';
flush privileges;
# 赋予权限
grant all privileges on *.* to yourusername@'%';

  1. 此时应能用datagrip访问

datagrip访问