这个操作在大约2年前做商务智能的时候成功用过,但最近想要一个自由的Mysql用,好几次在parallels desktop上的Ubuntu装都失败了,这次用别的虚拟机试了一下就成了,感觉很可能是虚拟机镜像的问题。
- 一个新的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
| 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"
sudo apt-get install docker-ce docker-ce-cli containerd.io
systemctl start docker
sudo docker sudo docker ps sudo docker run hello-world
|
- 拉取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 sudo systemctl restart docker
|
- 打开防火墙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 sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent sudo firewall-cmd --reload
|
- 跑一下容器试试,按理说此时应该就能连上了,用户名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@'%';
|
- 此时应能用datagrip访问